Also, I tried making line 13 say:
Code:
while(cout << "Selection: ");
But that just made the program keep displaying Selection over and over again in an infinite loop? can someone explain why this is please?
A while loop tests a condition inside the parentheses and evaluates it to true or false, since its evaluating it to true or false, it expects a boolean expression.
so the following expression
Code:
int x = 5;
while(x == 5)
{
cout << "Hello" << endl;
x = 6;
}
will on only run while x eqals 5.
note that if you put a semi colon after the while loop like you did
the program will run into an infinite loop as x will always remain 5.
this translates to while x equals five, do nothing!
Also, here is a really simple way of testing for the default case
Code:
#include <iostream>
using namespace std;
int main()
{
int Selection = 0;
while(Selection < 1 || Selection > 3)
{
cout << "Welcome to BlackJack! Choose an option below to continue:\n";
cout << "\n";
cout << "1)New Game\n";
cout << "2)Restore Game\n";
cout << "3)Exit\n";
cout << "\n";
cout << "Selection: ";
cin >> Selection;
switch (Selection)
{
case 1:
system("cls");
break;
case 2:
system("cls");
break;
case 3:
system("cls");
break;
default:
system("cls");
cout << "Invalid Selection\n";
break;
}
}
system("pause");
return 0;
}
the line
Code:
while(Selection < 1 || Selection > 3)
translates to while the selection is less than 1 or greater than 3, do what is inside the curly braces. This code is guaranteed to run once as Selection is assigned to 0 zero in the start and will only execute again if an invalid input is entered.
There are other ways of testing for invalid input, but this is a really simple one.
Hope this helps.