Hi there i am having trouble with my program, what the logic is implying is that every time i enter a big number, it should save as the biggest integer, if i enter a smaller number than the biggest, it will save in my second biggest integer slow. this works, but what i don't get how to do is do the same with the storing the low and the lowest numbers . it will save the low and lowest, but if i enter a number higher than what is already low, it will change that value when it shouldn't. i know how it should work i just can't apply that logic in the program. help please!
p.s. everything works, all brackets work, fully functioning code
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
//give names to all of your integers
int highest = 0;
int high = 0;
int low = 0;
int lowest = 0;
int number_from_KB;
//telling the user to enter a number between 0 and 10000 between specifications
cout<<"enter a number between 0 and 10000"<<endl<<endl;
cout<<"If your number falls to -1, the program will end"<<endl<<endl;
//beginning of the loop
for (int loop=0;loop<15;loop++)
{
cout<<" Enter a valued number "<<endl;
cin>>number_from_KB;
if (number_from_KB==-1)
break;
if (number_from_KB<0 || number_from_KB>10000)
cout<<"This is wrong. Do it again "<<endl;
else
{
cout<<"Good choice. "<<endl<<endl;
if (number_from_KB>=highest)
{
high=highest;
highest=number_from_KB;
}
else
{
if (number_from_KB>=high)
{
high=number_from_KB;
}
else
{
if (number_from_KB>=low)
{
lowest=low;
low=number_from_KB;
}
else
{
if (number_from_KB>=lowest && number_from_KB<=low)
{
low=number_from_KB;
}
else
{
if (number_from_KB>=0)
{
lowest=number_from_KB;
}
}
}
}
}
cout<<"Your numbers are "<<highest<<" and "<<high<<" and "<<low<<" and "<<lowest<<endl<<endl;
cout<<"The highest number that you entered was: "<<highest<<endl;
cout<<"The second highest number that you entered was: "<<high<<endl;
cout<<"The second lowest number that you entered was: "<<low<<endl;
cout<<"The lowest number that you entered was: "<<lowest<<endl;
}
}
}
When using ‘if’ and ‘else’ commands while writing your code it is important to remember to end the statement properly using semicolons. Upon adding lines in the code it is necessary to add {braces} around the extra lines of code you input, so the ‘if’ or ‘else’ command will go through all of your extra code at once. If these lines are not in a brace under the ‘if’ or ‘else’ statement the program will not know where this code goes in the loop.
Start but typing the first part which would be:
If (condition = TRUE) then you may add something for it to do if the condition is met correctly
{Something to do ;} make sure to end the line using ‘;’ then add the else prompt
Else (condition=FALSE) then add something to do
{Something to do ;} the command will execute as long as this info rejects the first if statement end with a semicolon so the line finishes