Hey guys!
I recently started programming as a course in college and I am a beginner in programming.I have got an assignment question where I am suppose to write a C++ program that prompts the user to enter a set of numbers ending with -99 and then the program finds and prints the maximum and the minimum of the entered numbers.
The problem is when i enter only -99 it gives me wrong max and min my ques is How do I get an output of "no input" if i enter -99
but get max and min when i input nums including -99 to stop the program.
I know i have to put condition outside the while loop but it dint really work.Also I know i am making a very silly mistake but I am really not able to figure out.
I am at a very basic level of loops here
heres my code
#include <iostream>
usingnamespace std;
int main()
{
int num, max=0, min;
cout<<"Enter a set of integers ending with -99 to find their maximum and minimum :";
cin>>num;
while(num != -99)
{
cin>>num;
if(num>max)
max=num;
if(num<min)
min=num;
}
if(num==-99)
cout<<"INVALID";
else
cout<<"max"<<max<<endl<<"min"<<min;
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int num, max = 0, min = 99; //remember to initialize min.
cout << "Enter a set of integers ending with -99 to find their maximum and minimum :";
cin >> num;
while (num != -99)
{
cin >> num;
if (num>max)
max = num;
if (num<min && num != -99)
min = num; // don't let -99 be assigned to min
}
//if (num == -99)
//cout << "INVALID"; //take these out
//else
cout << "max" << max << endl << "min" << min;
cin.ignore();
cin.get(); //just to pause the screen
return 0;
}
I tried changing the code it shows the max and min even if i just add input -99
my issue is when i enter -99 i dont want max and min just no input but with other digits i want it to show max and min
My output should be :-
eg1:-Enter a set of integers ending with -99 to find max and min: -99
*NO INPUT*
eg2:-Enter a set of integers ending with -99 to find max and min:
102 346 875 0 -4 8 -99
max : 875
min : -4
#include <iostream>
usingnamespace std;
int main()
{
int num, max = 0, min = 99;
cout << "Enter a set of integers ending with -99 to find their maximum and minimum :";
cin >> num;
while (num != -99)
{
//cin >> num;
if (num>max)
max = num;
if (num<min && num != -99)
min = num;
cin >> num;
}
//if (num == -99)
//cout << "INVALID";
//else
if(max>0 || min < 99) cout << "max" << max << endl << "min" << min;
cin.ignore();
cin.get();
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int num, max = 0, min = 99;
cout << "Enter a set of integers ending with -99 to find their maximum and minimum :";
cin >> num;
while (num != -99)
{
//cin >> num;
if (num>max)
max = num;
if (num<min && num != -99)
min = num;
cin >> num;
}
if (max == 0 && min == 99)
cout << "INVALID";
//else
if(max>0 || min < 99) cout << "max" << max << endl << "min" << min;
cin.ignore();
cin.get();
return 0;
}