Greetings and hello! + Loop issue

Hey everyone!

First off, awesome site!
I'm one of the many that recently got hooked on attempting some C++.
While I am busy following tutorials and video guides, I sometimes find that I can learn a lot by messing around with some code myself. rather than just reading and watching.

I am still a absolute beginner and just started practicing with the while,for and do while loops.
I believe it was this very site where I found some beginner exercises.
While I am slowly getting better at recognizing errors and what the compiler is trying to tell me, I kinda ran into a small issue here.

Which is the fact that the program compiles and runs fine, but does not completely do what I wrote, so I figured now would be a great time to interact with some like minded people and hopefully get a helping hand here.

The problem statement is listed as comments in the code below.
I also commented above the part of the code that doesn't seem to execute, so hopefully it will be convenient for people to find.

Any other tips, tricks or comments about the formatting, structure or anything really, are more than welcome and highly appreciated.
(Working my way trough the usage of function prototypes and header files + header guards atm)


Thanks in advance for your time!
Regards, Justin.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;



//Write a program that continues to asks the user to enter any number other than 5 until the user enters the number 5.
//Then tell the user "Hey! you were not 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.


 int main()
{
    int number = 0;
    int counter = 0;


    do
    {

    cout << "Enter a number that is not " << counter + 1 << endl;
    cin >> number;

    counter++;
    }while (number != counter);



        if (counter == number)
        {
            cout << "You were not supposed to enter " << counter << "\n The program will now exit!" << endl;

            return 0;
        }

        // Program runs fine, but this part is not functional
        if (counter >= 10);
        {
            cout << "You have more patience than I do. \n Bye." << endl;

            return 0;
        }






}
1
2
3
4
5
6
7
8
//include this inside your while loop =)
        if (counter >= 10)
        {
            cout << "You have more patience than I do. \n Bye." << endl;

            return 0;
        }
// As long as you don't type in the wrong number your program will stay inside the while loop forever and this code won't be executed 
Last edited on
Hey Glandy, thanks for the assistance.

I don't think i've included it properly in the while loop though, as it now immediately closes as soon as I enter any number.

Here is what I did.
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
 int main()
{
    int number = 0;
    int counter = 0;


    do
    {

    cout << "Enter a number that is not " << counter + 1 << endl;
    cin >> number;

    counter++;


        if (counter >= 10);
        {
            cout << "You have more patience than I do. \n Bye." << endl;

            return 0;
        }

            if (counter == number)
        {
            cout << "You were not supposed to enter " << counter << "\n The program will now exit!" << endl;

            return 0;
        }


    }while (number != counter);

}
1
2
3
4
5
if (counter >= 10); //remove the semicolon, sorry missed that before
// =>
if (counter == 10) { //btw counter == 10 is enough to check
// your code
}
Removing that semicolon did indeed fixed it.

Thanks for the help! <3

Not quite sure what you ment by counter == 10 is enough to check your code?
As part of the problem statement I was required to include a if with the return included in the body.
Last edited on
Topic archived. No new replies allowed.