Scrabble problem

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.

here's my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

int main ()
{ 
 	char check;
	int e,d,c,h,k,j;

	cout<< "Enter Text:"<< endl;
	cin >> check;
	
while ( check != '.' || check !='!')
{
	if ( (check == 'e') || (check =='E'))
	{
		e++;
	}
	else if ( (check == 'd') || (check =='D'))
	{
		d++;
	}
	else if ( (check == 'c') || (check =='C'))
	{
		c++;
	}
	else if ( (check == 'h') || (check =='H'))
	{
		h++;
	}
	else if ( (check == 'k') || (check =='K'))
	{
		k++;
	}
	else if ( (check == j') || (check =='J'))
	{
		j++;
	}

}

 	cout<< "Number of e's (worth 1 point each): "<<e<< endl;
	cout<< "Number of d's (worth 2 point each): "<<d<< endl;
	cout<< "Number of c's (worth 3 point each): "<<c<< endl;
	cout<< "Number of h's (worth 4 point each): "<<h<< endl;
	cout<< "Number of k's (worth 5 point each): "<<k<< endl;
	cout<< "Number of j's (worth 8 point each): "<<j<< endl;
	

return 0;
} 
Last edited on
Edit never worked with operators and chars. My mistake
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

int main ()
{
    char check;

    // int e,d,c,h,k,j;
    // **** initialize these variables
    int e = 0, d = 0, c = 0, h = 0, k = 0, j = 0 ;

    cout<< "Enter Text:"<< endl;

    // **** move this; you need to read a new character
    // **** each time through the loop
    // cin >> check;

    // **** this ( check != '.' || check !='!' ) will always be true
    // **** change to ( check != '.' && check !='!' )
    // while we have read a character and it is not  '.' and it is not '!'
    while( cin >> check && check != '.' && check !='!' )
    {
        if ( (check == 'e') || (check =='E' ) )
        {
            e++;
        }
        else if ( (check == 'd') || (check =='D' ) )
        {
            d++;
        }
        else if ( (check == 'c') || (check =='C' ) )
        {
            c++;
        }
        else if ( (check == 'h') || (check =='H' ) )
        {
            h++;
        }
        else if ( (check == 'k') || (check =='K' ) )
        {
            k++;
        }
        //else if ( (check == j') || (check =='J' ) ) // **** missing opening '
        else if ( (check == 'j') || (check =='J' ) )
        {
            j++;
        }
    }

    cout<< "Number of e's (worth 1 point each): "<<e<< endl;
    cout<< "Number of d's (worth 2 point each): "<<d<< endl;
    cout<< "Number of c's (worth 3 point each): "<<c<< endl;
    cout<< "Number of h's (worth 4 point each): "<<h<< endl;
    cout<< "Number of k's (worth 5 point each): "<<k<< endl;
    cout<< "Number of j's (worth 8 point each): "<<j<< endl;
    return 0;
}
Last edited on
so i should add

1
2
3
4
      else if ( (check == '.') || (check =='!'))
	{
		break; 
        }


like that?

I am really running out of ideas right now, and i've been struggling because my professor ask me to use while loop only.
wow, that works..

so that's how I should put in the while condition..

thank you so much JLBorges

I really appreciated it.

> 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
    }
    
    else if ( (check == 'e') || (check =='E' ) )
    {
        
    // etc    
}

It is a bit clumsy, but it works.
Is there any problem with just putting break in place of the bool = true?
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
    }
    
    else if ( (check == 'e') || (check =='E' ) )
    {
        
    // etc    
}


Essentially equivalent to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
        }

        else if ( (check == 'e') || (check =='E' ) )
        {

        // etc

        goto process_next_char ;
    }

we_are_done:
    // carry on with the rest of the function 


Topic archived. No new replies allowed.