+ 1

this is looking impossible to me......?????Why can't i get the min value.....????

i am trying to do this for more then 3 days but still i am so far from my target......i have tried it with many differrent method but still uselesss???????? #include<iostream> using namespace std; int main(){ float i=1,no,max=0,min=no; while(i>0){ cout<<"Enter a number:"; cin>>no; if(no!=0) { if(no>max){ max=no; } if(no<min){ min=no; } } if(no==0){ cout<<"max:"<<max<<endl; cout<<"min:"<<min<<endl; return 0; } } }

10th Dec 2018, 3:08 PM
Aamir Mehmood
8 odpowiedzi
+ 5
Aamir Mehmood That's exactly what you described
10th Dec 2018, 7:22 PM
Babak
Babak - avatar
+ 2
Revised version: #include <iostream> using namespace std; int main() { int i = 1, no = 0, max = 0, min = 0; while(i > 0) { // infinit loop cout << "Enter a number:"; cin >> no; if(no != 0) { if(no > max) { max = no; } if(no < min) { min = no; } } else if(no == 0) { cout << "max:" << max << endl; cout << "min:" << min << endl; break; // breaks the loop } } } Sample output: Enter a number:4 Enter a number:5 Enter a number:6 Enter a number:4 Enter a number:-9 Enter a number:0 max:6 min:-9
10th Dec 2018, 6:26 PM
Babak
Babak - avatar
+ 1
Possible fix: #include <iostream> using namespace std; int main(){ int i = 1, no = 0, max = 0, min = 0; while(i > 0) { // infinit loop cout << "Enter a number:"; cin >> no; if(no != 0) { if(no > max) { max = no; } if(no < min) { min = no; } cout << "max:" << max << endl; cout << "min:" << min << endl; break; // breaks the loop } else if(no == 0) { cout << "max == min == " << max << endl; break; // breaks the loop } } } If you enter a non-zero integer, then you would have something like this in output: Enter a number:8 max:8 min:0 Otherwise, Enter a number:0 max == min == 0
10th Dec 2018, 3:38 PM
Babak
Babak - avatar
0
Could you give us an example of what you want your code to do?
10th Dec 2018, 3:32 PM
Diego
Diego - avatar
0
sir actually i want user to input numbers as many as he wants and if he enter zero 0 then i want to show minimum values out of all those which were entered before entering zer 0. thats what i want
10th Dec 2018, 4:53 PM
Aamir Mehmood
0
nope its still wrong
10th Dec 2018, 7:10 PM
Aamir Mehmood
0
i have done this...puzzle..... #include <iostream> using namespace std; int main(){ cout<< "Enter a number- Enter 0 to see result: "; int num = 0, max = 0, min=0; for( int i = 1; i <= 100; ++i){ cin>> num; if(i==1){ max=num; min=num; } else{ if( num > max ) max = num; else if( num == 0 ) break; else if( num < min ) min = num; } } cout<<"min:"<< min <<" max:"<< max; return 0; }
11th Dec 2018, 5:46 PM
Aamir Mehmood