Hey fellow programmers, I have started doing some problems and I feel very giddy and happy when I am able to solve an issue. I am a beginner but I feel like I can progress and become better. Take a look at my code and tell me if there is anything I can do to improve. I have heard that people do not like to use goto statements but I couldn't think of anything else to get out of the nested loop.
Well here it is please give me any advice, Thank you!
/*While( user == gullible )
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)
*/
#include <iostream>
usingnamespace std;
int number; //ask user to assign a number to variable
int attempt;//assign amount of attempts user can input the "number" variable
int main()
{
do
{cout<<"Enter any number except 5."<<endl;
cin>>number;
attempt++;//to get out of loop
if (attempt>10)
{
cout<<"Wow, you're more patient then I am, you win."<<endl;
goto endprogram;// goto statement
}
}
while (number<5||number>5);
if (number=5)
{
cout<<"Hey! you weren't supposed to enter 5!"<<endl;
}
endprogram://statement label
return 0;
}
Except this loop isn't nested. A break statement would work fine here.
Line 35 uses the wrong ==.
Why are your variables global @ lines 19 and 20? There's literally no reason for them to be. Best to have them in main.
EDIT: attempt isn't zero-initialized.
Also, for fun, extra brownie points for restructuring your code such that the positions of your if statements are switched (namely the number == 5 one is in the loop, and the attempt > 10 is outside). What you didn't isn't wrong, but let's see if you can do it the other way.
#include <iostream>
int main()
{
int number = 0;
int attempt = 0;
do
{
std::cout<<"Enter any number except 5."<<std::endl;
std::cin>>number;
++attempt;//to get out of loop
if (attempt>10)
{
std::cout<<"Wow, you're more patient then I am, you win."<< std::endl;
break;
}
} while (number != 5);
if (number=5)
{
cout<<"Hey! you weren't supposed to enter 5!"<<endl;
}
return 0;
}
Though I personally would use a for loop instead of a do/while somethign like
1 2 3 4 5 6 7 8
int i = 0;
for(; i < 12 && number != 5; ++i)
{
std::cout << "Enter any number except 5." << std::endl;
std::cin >> number;
}
if(i == 11) std::cout << "Wow, you're more patient than I am, you win." << std::endl;
if(number == 5) std::cout << "You weren't supposed to enter 5." << std::endl;
We all prefix everything with std:: - it is the normal way to write code. It also makes it easier when reading code to distinguish between what is from the standard library and what is something that you wrote.