hi, i am new in C++, and i have a problem in building my scrabble problem.
i was asked to make a program that will count specific words from the user imput, in this case, e,d,c,h,j,k.
however, my loop does not seems to work, and the program wont stop. since the program wont stop, i dont know if my program actually count the words. hope you guys can help me with this.
> so that's how I should put in the while condition..
That is not the only way one could write the while loop.
The popular idiom among members of cplusplus.com seems to be what is called 'a bool controlled while loop'.
It goes something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
bool done = false ;
while( !done ) // while we are not yet done
{
cin >> check ; // try to read the next character
// if the read was unsuccessful,
// or the character read was either a '.' or a '!'
if( cin.fail() || check == '.' || check == '!' )
{
done = true ; // we are done; signal exit from the loop
}
elseif ( (check == 'e') || (check =='E' ) )
{
// etc
}
Is there any problem with just putting break in place of the bool = true?
No.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
while( true ) // forever; till we explicitly break from the loop
{
cin >> check ; // try to read the next character
// if the read was unsuccessful,
// or the character read was either a '.' or a '!'
if( cin.fail() || check == '.' || check == '!' )
{
break ; // we are done; exit from the loop
}
elseif ( (check == 'e') || (check =='E' ) )
{
// etc
}
process_next_char:
{
cin >> check ; // try to read the next character
// if the read was unsuccessful,
// or the character read was either a '.' or a '!'
if( cin.fail() || check == '.' || check == '!' )
{
goto we_are_done ; // we are done; quit the loop
}
elseif ( (check == 'e') || (check =='E' ) )
{
// etc
goto process_next_char ;
}
we_are_done:
// carry on with the rest of the function