A Scrabble Problem

Hey everyone. I am new to C++ and I am working on some homework from a class of mine. The program is supposed to prompt the user to enter a sentence followed by a '.' or '!'. The program will then count the relevant letter (e,d,c,h,k,and j) in the sentence and display the count of each letter. Along with this, each relevant letter is given a value and the program will sum up the total value and display it at the end. This is what I have come up with so far. It compiles fine but gets stuck in an input loop. Any suggestions?

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
58
59
60
  
#include <iostream>
using namespace std;

int main()
{
  char text;                          // The string being entered
  int tot_score(0);                   // Total Score
  int e(0),d(0),c(0),h(0),k(0),j(0);  // Initiation of each letter counter

  cout << "Enter text: " << endl;
  cin >> text;

  while ( (text != '!') || (text != '.') )
    { 
      if( (text == 'e') || (text == 'E') )
	{ 
	  e++;
	  tot_score ++;
	}
      else if( (text == 'd') || (text =='D') )
	{ 
	  d++;
	  tot_score += 2;
	}
      else if( (text == 'c') || (text =='C') )
	{ 
	  c++;
	  tot_score += 3;
	}
      else if( (text == 'h') || (text == 'H') )
	{ 
	  h++;
	  tot_score += 4;
	}
      else if( (text == 'k') || (text == 'K') )
	{ 
	  k++;
	  tot_score += 5;
	}
      else if( (text == 'j') || (text == 'J') )
	{ 
	  j++;
	  tot_score += 8;
	}
    }

      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;
      cout << endl;
      cout << "Total score: " << tot_score << endl;

      

      return(0);
}
So. You want the user to enter an entire sentence. If so, non of your code makes any sense.

If you want a sentence. Then it has to be string text;

And you will have to use getline(cin,text); instead of cin >>.

Now. if text is say "Hey there". The reason non of your code will make any sense is that, non of your if statements nor the while loop does anything related to that sentence. Do you understand what I mean?

Edit:

For example. Text = "Hey there".

1
2
3
4
5
6
7
8
9
10
11
12
13
while ( (text != '!') || (text != '.') )
    { 
      if( (text == 'e') || (text == 'E') )
	{ 
	  e++;
	  tot_score ++;
	}
      else if( (text == 'd') || (text =='D') )
	{ 
	  d++;
	  tot_score += 2;
	}
      else if( (text == 'c') || (text =='C') )


text will never be equal to any of these. Because text is a sentence and not a character.
Last edited on
So is there anyway using only <iostream> that I can read each character of the sentence individually? For example, using "Hey there!" the user should enter this and then the prgram should check each character of the string and run accordingly in the while loops. Displaying the count of each letter and the total score tallied up.
Last edited on
As far as I know, There is only 1 way,but its dumb as hell. Basically just ask the user how many characters his sentence is gonna be. Then create an array, then tell the user to type in each character of that sentence one at a time. Basically what Im trying to say is, No.

Why can you only use <iostream> ?
We are expected to complete this assignment with only the directories we have learned thus far, and using a while loop and not for loops. Being new to C++ don't know any other way to do any of this unless I use outside source. Which I can do, but it would totally bogus to expect us as students to implement things we have yet to learn in lecture. I am out of ideas.
You've learned about chars, ints, loops. But not about strings? what? Im sorry my friend but I really dont know how to help you
Last edited on
closed account (D80DSL3A)
Reading the input one char at a time should work, but you need a cin >> text; within the while loop to continue processing them.

You'll still have a problem though. Neither '!' or '.' will terminate the while loop because the logical test on line 14 is incorrect.
Could I use a Boolean operator to break the while loop when the '.' or '!' appears? Such as:

1
2
3
4
5
6
7
8
while( !end )
{
....
else if( (text == '.') || (text == '!'))
     {
      end = true
     }
}

closed account (D80DSL3A)
You could, but wouldn't it be better to just fix the incorrect test?
Hint: Try &&
Okay I figured it out using a do-while loop. Like fin2code said I just needed to prompt for the phrase in the loop and use && relation instead of ||. Thanks everyone for the help. Here is the final code if anyone is curious:
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
58
59
60
61

#include <iostream>
using namespace std;

int main()
{
  char text;                          // The string being entered
  int tot_score(0);                   // Total Score
  int e(0),d(0),c(0),h(0),k(0),j(0);  // Initiation of each letter counter
  

  cout << "Enter text: " << endl;

  do
    { 
      cin >> text;
      if( (text == 'e') || (text == 'E') )
	{ 
	  e++;
	  tot_score ++;
	}
      else if( (text == 'd') || (text =='D') )
	{ 
	  d++;
	  tot_score += 2;
	}
      else if( (text == 'c') || (text =='C') )
	{ 
	  c++;
	  tot_score += 3;
	}
      else if( (text == 'h') || (text == 'H') )
	{ 
	  h++;
	  tot_score += 4;
	}
      else if( (text == 'k') || (text == 'K') )
	{ 
	  k++;
	  tot_score += 5;
	}
      else if( (text == 'j') || (text == 'J') )
	{ 
	  j++;
	  tot_score += 8;
	}
    }while(( text != '!' ) && ( text != '.' ));

      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;
      cout << endl;
      cout << "Total score: " << tot_score << endl;

      

      return(0);
}
Topic archived. No new replies allowed.