I'm writing a program that asks for a number and until that number is 99 it keeps asking the user to enter another number... I have this code and it runs but I'm having trouble getting it to accept the answer 99 if it is guessed and to stop the program from running. It's in C++
This should point you in the right direction and show you what errors you made so far. This is just a working version, not an ideal version -- there's still plenty of room for improvement. If you have more questions, feel free to ask.
#include<iostream>
usingnamespace std;
int main()
{
int i=99;
int n;
cout <<" Please enter a number" << endl;
cin >> n;
while (n!=i); // This ; was messing things up and making the while loop just run infinitely without doing anything.
{
cout <<" enter another number" << endl;
cin >> n;
if (n==i) // If we exit the loop, the number must be correct anyway; no need to check here.
{cout << "correct number" << endl;}
}
cout << "correct number" << endl; // We exited the loop, so i must be equal to n
return 0; //Main is declared as int main() so it must return an integer.
}