+ 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; } } }
8 Answers
+ 5
Aamir Mehmood That's exactly what you described
+ 2
First, remove your duplicate post:
https://www.sololearn.com/Discuss/1616258/this-is-looking-impossible-to-me-why-can-t-i-get-the-min-value
+ 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
+ 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
0
Could you give us an example of what you want your code to do?
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
0
nope its still wrong
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;
}