yeah, you could put everything into an infinite loop, i do this often. take a look:
Code:
#include <iostream>
using namespace std; //so you dont haave to always type std::(cout/cin)
int main()
{
while(1) //This is a while() loop, it runs what is in the {}
//until its condition () is false or it is terminated,
//while(1) is always true, we call this an infinite loop.
{
cout << "Please Enter 1 or 2(Type 0 to Exit): ";
int a;
cin >> a;
cin.ignore();
if (a == 1)
{
cout << "You Typed: 1";
cin.ignore(); //pauses
continue; //this will continue the loop
}
if (a == 2)
{
cout << "You Typed: 2";
cin.ignore();
continue;
}
//i added the exit option in, a while loop like this will go on forever
if(a==0)
{
cout<<"Press Enter to Exit...";
cin.ignore();
break; //break does as it implies, breaks a loop
}
if (a > 2)
{
cout << "You Didn't Type 1 or 2!";
cin.ignore();
continue;
}
if (a < 1)
{
cout << "You Didn't Type 1 or 2!";
cin.ignore();
continue;
}
}
cin.get();
return 0;
}
Good Luck!
PS-Try to type in 1.5 or 1.2 What happens? Nothing! You could do a couple things here. Heres what i would do, replace:
Code:
...
if (a > 2)
{
cout << "You Didn't Type 1 or 2!";
cin.ignore();
continue;
}
if (a < 1)
{
cout << "You Didn't Type 1 or 2!";
cin.ignore();
continue;
}
...
With:
Code:
...
if(a!=1 || a!=2)
{
cout<<"You didnt type 1 or 2!"
cin.ignore();
continue;
}
...
The != means doesnt equal, and the || means or.
Good Luck!
Check out the link, there are a couple other kinds of loops.