what could i have done differently?

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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*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>
using namespace 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.

-Albatross
Last edited on
1
2
3
4
    if (number=5)
{
    cout<<"Hey! you weren't supposed to enter 5!"<<endl;
}
This is the wrong operator supposed to be == but either way this will always be true because the only way to leave the loop is when number == 5:P

Another thing to mention is you can use break instead of the goto on line 31/39 (Albatross mentioned as well)

You should avoid using namespace as it will bring everything even the unwanted stuff. Especially in the global scope.


Another thing I would suggest is to indent.

Your code is pretty hard to read IMO.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
Last edited on
ohh thanks for the help @therabbitologist I completely forgot that assigned means =, and equal means ==.

do you mean attempt=0; , what does that do?
Use indentation to reflect the block level of the code.

Line 34, it would be clearer to write the condition as number != 5

Kudos for asking people to look at the code. It's a great way to learn. Also, try reading other people's code.
yeah I should make the code more readable, but if I do not use using namespace std;

is their something else I can use or would I have to use std::
for the code? @giblit
oh right I didn't think about the number !=5

it is much more simpler, I think the way I did it was a little unnecessary but thanks for giving me a new way to write my code the next time.

@dhayden
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.
Last edited on
You could do
1
2
using std::cout;
using std::endl;
if you really don't want to type std:: in front of those two.
Topic archived. No new replies allowed.