Hello, new to the forum and was wondering if someone would be kind enough to help me.
I need the user to enter 10 numbers. If the numbers they enter are in ascending order (previous number has to be smaller than what the user enters next) it allows the user to keep on going until they enter the 10th number which the output would be a success. If the user enters a number that is smaller than the previous, I need it to break and the output to be failed.
#include <iostream>
usingnamespace std;
int main(){
int num;
int g = -1;
// Count
for (int i = 0; i < 10; i++)
{
cout << "Please Enter Your Number: ";
cin >> num;
// Store
if (g < num)
{
g = num;
cout << "Success!\n";
}
else {
cout << "Failure!\n";
break;
}
}
}
My problem here is that it keeps saying success after the user presses enter and I need the final number to output success only. Any help would be awesome.
#include <iostream>
usingnamespace std;
int main(){
int num;
int g = -1;
// Count
for (int i = 0; i < 10; i++)
{ // For Loop Begins
cout << "Please Enter Your Number: ";
cin >> num;
// Store
if (g < num)
{
g = num;
}
else {
cout << "Failure!\n";
break;
}
} // For Loop Ends
if (g <= num)
{
cout << "Success!\n";
}
}
This is the closest I have come to getting it to work, but when I type in two of the same name numbers (ex. 10 and 10). I get Success and Fail. I'm pretty sure I mixed up a sign somewhere, but I can't figure out where.
Stop trying random things and think carefully. Think about what line 13 does, and then think about how it relates to line 24. You currently only need to change (not remove) line 24.
#include <iostream>
usingnamespace std;
int main(){
int num;
int g = -1;
// Count
for (int i = 0; i < 10; i++)
{ // For Loop Begins
cout << "Please Enter Your Number: ";
cin >> num;
// Store
if (g < num)
{
g = num;
}
else {
cout << "Failure!\n";
break;
}
} // For Loop Ends
if (g < num)
{
cout << "Success!\n";
}
system("PAUSE");
return 0;
}
I tried that, but it doesn't output success once I actually input 10,20,30....100.
You should use a boolean for this. Start with the boolean being true, and when you reach your fail state (lines 17-20) set it to false. After the loop just use it in the if statement (line 24) to know whether you successfully entered all the data.