Program won't work !

I don't know why is it not working. The program seem fine to me.
All it has to do is keep saying " You didn't select the required number " until the user enters 5.

#include <iostream>

using namespace std;

int main()
{

int number;

cout << "Enter a number 5";
cin >> number;

if (number==5)

{cout << "You have selected the correct number.\n"}


while (number != 5)

{cout << "You didn't select the required number.\n";}



return 0;
}




First, please use code tags... like this:

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
 #include <iostream>

 using namespace std;

 int main()
 {

 int number;

 cout << "Enter a number 5";
 cin >> number;

 if (number==5)

 {cout << "You have selected the correct number.\n"}


 while (number != 5)

 {cout << "You didn't select the required number.\n";}



 return 0;
 }


OK, now, you get an input, test it, and if it isn't 5 then you print (forever) you didn't enter 5... it never asks again, I think what you want is something like this:

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
 #include <iostream>

 using namespace std;

 int main()
 {

     int number;

     while(true)
     {
         cout << "Enter a number 5";
         cin >> number;

        /* 
         * Test if the input was '5', if it is then 
         * let the user know and break out of 
         * the loop.
         */
        if (number==5) 
        {
            cout << "You have selected the correct number.\n";
            break;
        }

        /* 
         * If the number is other than '5' then
         * pester the user about it and jump 
         * to the start of the loop.
         */
        else
       {
            cout << "You didn't select the required number.\n";
            continue;
        }
    }

    return 0;
}
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Look at your loop:

1
2
3
while (number != 5)

{cout << "You didn't select the required number.\n";}


This will simply loop forever, because nothing in the loop changes the value of number. Once number != 5 is true, it will remain true for ever, so the loop will continue iterating forever.
The way you wrote the code is syntactically correct but:

1) If the program should reiterate that the number is incorrect then let it check if the number is not equal to 5 first by using the while, then if the user inputs the correct number it ends the while and then outputs "You have selected the correct number.

2) Writing your while without another cin with-in it will cause a infinite loop.

Try:
#include <iostream>

using namespace std;

int main()
{

int number;

cout << "Enter a number 5:\t";
cin >> number;

while (number != 5)
{
cout << "You didn't select the required number.\n";
cout << "Enter a number 5:\t";
cin >> number;
}
cout<<"You have selected the correct number.\n";
return 0;
}


I tried all the programs you guys made to explain it to me. But non of them if giving the cout of when the user actually enters the number 5.

Is there something wrong with my compiler?

I tried all the programs you guys made to explain it to me. But non of them if giving the cout of when the user actually enters the number 5.

Is there something wrong with my compiler?


Yes.

Ok, that was a simple answer... it works on my Linux box using GCC 4.2.1...

I guess that's not much more use... hmmmm, well, I suppose I could say that compilers tend not to have issues with things as basic as this, so probably not the compiler.

Can you tell us exactly what code you used (copy and paste again please) and exactly what keys you press and what compiler you're using.
Im using code blocks. It has Mingw GCC libraries, I guess. I new to this that why i not know much about compilers either.

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

#include <iostream>

using namespace std;

int main()
{
 
 {
  int number;

  cout << "Enter a number 5";
  cin >> number;

  if (number==5)

   {cout << "You have selected the correct number.\n"}



  else

   {cout << "You didn't select the required number.\n";continue;}

    return 0;
}


I pressed the build and run button. It runs prefectly. Gives the correct cout when 5 is not pressed. But When I press 5 it just skips a blank line there :/
OK, so there doesn't seem to be a while loop there... and you need a break if you want to program to end when you input 5.

It's your code...
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
#include <iostream>

using namespace std;

int main()
{
 /* No loop? or is that a copy-paste error?*/
 {
  int number;

  cout << "Enter a number 5";
  cin >> number;

  if (number==5)

   {cout << "You have selected the correct number.\n"} /* Missing a ';' here.*/



  else

   {cout << "You didn't select the required number.\n";continue;}

    return 0;
}


Try copy and pasteing this code into a blank file and compile and run this. Then come back and let us know if it works and ask questions if you still don't understand.

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
 #include <iostream>

 using namespace std;

 int main()
 {

     int number;

     while(true)
     {
         cout << "Enter a number 5";
         cin >> number;

        /* 
         * Test if the input was '5', if it is then 
         * let the user know and break out of 
         * the loop.
         */
        if (number==5) 
        {
            cout << "You have selected the correct number.\n";
            break;
        }

        /* 
         * If the number is other than '5' then
         * pester the user about it and jump 
         * to the start of the loop.
         */
        else
       {
            cout << "You didn't select the required number.\n";
            continue;
        }
    }

    return 0;
}
Same problem. It gives the correct cout when user doesn't enter 5 but gives a blank space if 5 is entered.
Did you fix the missing ';' and break statement?

Did you try the code I gave you (copy and paste)?

Does your compiler give you any warnings?

What compiler are you using?

We can't help you if all you tell us is "it doesn't work".
Yes. I tried with the break statement too.

Yes I copy pasted your code as it is.

No warnings. This is what is says.

(Checking for existence: C:\Users\Talha\Desktop\C++ Test Project\bin\Debug\C++ Test Project.exe
Executing: "C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe" "C:\Users\Talha\Desktop\C++ Test Project\bin\Debug\C++ Test Project.exe" (in C:\Users\Talha\Desktop\C++ Test Project\.)
Process terminated with status -1073741510 (0 minute(s), 3 second(s))

Im usng code blocks 13.12



I just downloaded codeblocks 13.12, pasted my code in, ran it, pressed 5 and got "you have selected the correct number".

Double check again, make sure it really did compile (could be running an old copy... maybe?), make sure it really did run the latest version.

Try creating a whole new project and pasting my code in. All I can tell you is this doesn't look like a C++ issue, but an IT issue.

What OS are you running, Windows or Linux?
Hi @talhabhatti5

I use CodeBlocks too, same version, you can check which compiler you are using if you go to Settings>>Compiler, I am using GNU GCC Compiler.

I can compile and run the program @ValliusDax gave you and its working, it must be a copy-paste error or your compiler is not working properly.
Its working just fine now. I removed continue from the code added a while loop to make the program keep asking until the user enters 5.

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

#include <iostream>

using namespace std;

int main()
{
while(true)
{
  int number;

  cout << "Enter a number 5\n";
  cin >> number;

  if (number==5)

   {cout << "You have selected the correct number.\n";}



  else

   {cout << "You didn't select the required number.\n";}

}

    return 0;
}



Anyway, thank you so much for your help everyone :)
Topic archived. No new replies allowed.