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";}
#include <iostream>
usingnamespace 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:
#include <iostream>
usingnamespace 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;
}
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?
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.
#include <iostream>
usingnamespace 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 :/
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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;
}
(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))