0
why 737 is printed so many times ?input=2 2 a g 1 737 586
#include <bits/stdc++.h> using namespace std; int max(int a,int b){ if(a>b){ return a; } else return b; } char max(char a,char b){ if(a>b){ return a; } else return b; } double max(double a,double b){ if(a>b){ return a; } else return b; } int main() { int n; cin>>n; while(n>0){ int x,y; char a,b; double m,n; int p; cin>>p; if(p==1){ cin>>x>>y; cout<<max(x,y); } if(p==2){ cin>>a>>b; cout<<max(a,b); } if(p==3){ cin>>m>>n; cout<<max(m,n); } n--; cout<<endl; }
2 Answers
+ 3
Because within the while loop you have a second n being defined. Therefore, that is the n that has changed by the n-- statement, not the one being tested in the while. Once your inputs are used up, the cin statements do nothing so your infinite loop of a while repeats the last max call each time.
+ 1
thank u sir
Hot today
I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.
2 Votes
Python palindrome challenge.
1 Votes
Java
0 Votes
Number of Ones ( C++ ) question!
1 Votes