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; }

9th May 2018, 6:27 AM
Bahubali - avatar
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.
9th May 2018, 9:04 AM
John Wells
John Wells - avatar
+ 1
thank u sir
9th May 2018, 9:18 AM
Bahubali - avatar