PDA

View Full Version : C++ help?

Sirena400
Feb 7, 2005, 09:15 PM
I'm a beginner at C++ and I'm having a really hard time with the problem that my teacher gave me to do. The program is about a game of scrabble in which we have to make a program that will add up the number of points for each word is the the sum of the values of the letters in the word.

I'll be given one or more words, one per line. The # sign will represent blanks. And the end of the sequence of words will be marked by one line of input containing \$. The end result should be a statement stating the player's score. The letter values are as follows: # = 0 A,E,I,L,N,O,R,S,T,U = 1
D,G = 2 B,C,M,P = 3 F,H,V,W,Y = 4 K=5 J,X = 8 Q,Z = 10

The input will be a sequence of 2 or more lines w/the last line having the \$. And the output will have the statement showing the players score.

example: Input: DAY#TIME
\$

Output: The player's score is 13.

I know that the basic setup is:
#include<iostream.h>
int main()
{

return0;
}
And that you include this: while(letter!='\$') to tell the program when to stop, but as for everything else like setting up the letter values and the code for the actually add itself I'm so lost on. Can anybody help me please? :(

Zeke
Feb 7, 2005, 10:05 PM
Edit: I forgot the character signs ' ' around the characters, also you'll have to use the upper and lower case because they're different for C.

Is this input by typing or by file? Probably typing...do you know how to use cin >> ? Do you understanding the elements of a string? I wouldn't use while( letter!=\$)...when we were being taught the teacher always said using while was sloppy programming...not sure if that's true but whatever...

I would do something like:

#define MAX_LETTERS 256 /*you could make this dynamic allocation if you really wanted to but...whatever*/

char * string[MAX_LETTERS]
int score = 0;
for( i=0, string[i] !=\$, i++)
{
switch( string[i] )
{
case D:
case G: score+=2;
break;
case B:
case C:
case M:
case P: score+=3;
break;

etc...
}
}

Do the same for the other characters...it's hard not knowing what you know...this is probably not included in that though...you can also just use if then else statements for each possible character making sure you always advance through your string. Good luck.

I'm a beginner at C++ and I'm having a really hard time with the problem that my teacher gave me to do. The program is about a game of scrabble in which we have to make a program that will add up the number of points for each word is the the sum of the values of the letters in the word.

I'll be given one or more words, one per line. The # sign will represent blanks. And the end of the sequence of words will be marked by one line of input containing \$. The end result should be a statement stating the player's score. The letter values are as follows: # = 0 A,E,I,L,N,O,R,S,T,U = 1
D,G = 2 B,C,M,P = 3 F,H,V,W,Y = 4 K=5 J,X = 8 Q,Z = 10

The input will be a sequence of 2 or more lines w/the last line having the \$. And the output will have the statement showing the players score.

example: Input: DAY#TIME
\$

Output: The player's score is 13.

I know that the basic setup is:
#include<iostream.h>
int main()
{

return0;
}
And that you include this: while(letter!='\$') to tell the program when to stop, but as for everything else like setting up the letter values and the code for the actually add itself I'm so lost on. Can anybody help me please? :(

Sirena400
Feb 8, 2005, 11:19 AM
Thanks Zeke, that helps quite a bit. Sorry I forgot some info to help you on what I know, typical newbie behavior for ya, sorry. The input is by typing, yes I do know how to use cin>>. I really don't understand the elements of a string or switch statements very well, an explination of these two would be very appreciated if it's not to much trouble for you. And what's does the define MAX_LETTERS statement mean? Thanks for helping Zeke, I really, really appreciate it. :)

Zeke
Feb 8, 2005, 12:45 PM
So since you don't know switch, you should probably use if else stuff...so basically, format would look like...

if (string[i] =='D' || string[i] == 'G')
score+=2;
else if (string[i] == 'B' || string[i] == 'C' || string[i] == blah blah etc...)
score +=3;

if you wanted to really look slick you could do...

score += string[i] == 'D'? 2 : 0 (with conditions for every option)

The MAX_CHARACTERS thing was just to make the string big enough to accept any reasonable sequence of words (whatever the maximum number of characters you'll allow is). For example, if the words input totalled 300 characters the string wouldn't be able to take that many and would end before you were done typing.

The string is an array of characters (actually it's a pointer to an array of characters but you don't need to know that). So when you type

cin >> string

If you type blahblah
string[0] = 'b', string[1] = 'l' and so on. Then when you pull up each element of the string, or character if you will, then you can manipulate it as one piece of data which is what you want. Since this is C++ and not C your teacher may want you to define a class that is your word and implement some specialized object oriented functions that can only deal with your string class. I'm not sure...good luck.

Thanks Zeke, that helps quite a bit. Sorry I forgot some info to help you on what I know, typical newbie behavior for ya, sorry. The input is by typing, yes I do know how to use cin>>. I really don't understand the elements of a string or switch statements very well, an explination of these two would be very appreciated if it's not to much trouble for you. And what's does the define MAX_LETTERS statement mean? Thanks for helping Zeke, I really, really appreciate it. :)

Sirena400
Feb 8, 2005, 09:20 PM
Thanks again for answering my quesitons Zeke! I hate to be more of a trouble, but could you explain switch statements and walk me through how to set up one? The only reason I ask is because our teacher told us that we'd be using switch statements in the future and I don't know how to do them at all unfortunately. :( Really, I'm a rank beginner, I barley know the basics. Thanks so much for all the help you're giving me! :)

jsw
Feb 8, 2005, 09:31 PM
In a nutshell, the switch statement takes the value given to it (i.e., in "switch (x)" it uses the current value of variable "x") and proceeds down the list of cases until it finds a match. It then executes every command from then on until it hits a "break" statement. If there is a "default" line, and nothing else has matched yet, any commands after the "default" are executed. If there is no "default", and none of the cases match, nothing happens.

For example, if we have a case where x='D', then for the following:

switch (x)
{
case 'A':
y = z + 1;
break;
case 'B':
y = z - 1;
break;
case 'C':
y = z + 3;
break;
case 'D':
y = z + 4;
case 'E':
y = y + 6;
break;
case 'F':
y = z + 1;
break;
default:
y = 0;
break;
}

In the case of x='D', y would end up equaling z + 10, because the first statement that matched (yielding "y = z + 4") has no "break", causing execution to continue to "y = y + 6" before breaking.

In the case of x='Z', the default statement ("y=0") would be executed.

jsw
Feb 8, 2005, 09:33 PM
It's been a while since I've used C++, but I'm pretty sure that C++ switch statements are limited to - I think - chars, bytes, ints, shorts, etc. - not strings or non-integral numbers.

bbarnhart
Feb 8, 2005, 10:15 PM
It's been a while since I've used C++, but I'm pretty sure that C++ switch statements are limited to - I think - chars, bytes, ints, shorts, etc. - not strings or non-integral numbers.

Characters, 'A', are basically integers and can be used in a switch. Strings, "ABC", can not be used in a switch statement.

bbarnhart
Feb 8, 2005, 10:25 PM
// setup the array

// whatever the scrabble letters are arraigned from 'A' to 'Z' then 'a' to 'z'
// I don't know if there are lower case letters in Scrabble, but your
// assignment used lowercase letters

int array[52] = {1, 2, 3, 1, 2, 3, 4...8, 10};

int value = 0;

{
if (each_letter >= 'A' && each_letter <= 'Z')
num_letter = (int)each_letter - 60;
else if (each_letter >= 'a' && each_letter <= 'z')
num_letter = (int)each_letter - 97;
else
you letter sucks error

value += array[num_letter];
}

jsw
Feb 8, 2005, 10:51 PM
// setup the array

// whatever the scrabble letters are arraigned from 'A' to 'Z' then 'a' to 'z'
// I don't know if there are lower case letters in Scrabble, but your
// assignment used lowercase letters

int array[52] = {1, 2, 3, 1, 2, 3, 4...8, 10};

int value = 0;

{
if (each_letter >= 'A' && each_letter <= 'Z')
num_letter = (int)each_letter - 60;
else if (each_letter >= 'a' && each_letter <= 'z')
num_letter = (int)each_letter - 97;
else
you letter sucks error

value += array[num_letter];
}
The assignment only had upper-case, and your code ignores the crucial '#' and '\$' characters, but otherwise the logic is fine. A switch is faster, but that's unlikely to matter for this assignment. Again, it's been a while since I've done C++, but I assume you could do something like this to set up the array values if you don't want to hard-code them:

int value[128];

// I forget if C++ zeros arrays by default - don't do this part if it does
for (loop = 0; loop < 128; ++loop)
{
value[loop] = 0;
}

value['A'] = value['E'] = value['I'] = value['L'] = value['N'] =
value['O'] = value['R'] = value['S'] = value['T'] = value['U'] = 1;

value['D'] = value['G'] = 2;

value['B'] = value['C'] = value['M'] = value['P'] = 3;

value['F'] = value['H'] = value['V'] = value['W'] = value['Y'] = 4;

value['K'] = 5;

value['J'] = value['X'] = 8;

value['Q'] = value['Z'] = 10;

Then you could just do something like:

for (...each letter...)
{
total += value[letter];
}

bbarnhart
Feb 9, 2005, 06:19 AM
[FONT=Courier New]

#include <iostream>

int main (int argc, const char * argv[]) {

int array[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

char example[] = "DAY#TIME";

int value = 0;

for (int i=0; i < strlen (example); i++)
{
if (example[i] == '#')
value += 1;
else
{
std::cout << "Letter " << example[i] << "\n";
value += (int)array[example[i]-60];
std::cout << "Value "<< (int)array[example[i]-60] << "\n";
}
}

// insert code here...
std::cout << "Total is " << value << "\n";
return 0;
}

I'll leave it to the student to put in the '\$' where needed

Sirena400
Feb 9, 2005, 11:18 AM
Oh wow! Thanks you so much jsw and bbarnhart! I can't believe all the help you've given me! Thanks guys, I think I finally understand how to do this, I'm gonna try it out and see how it goes. Thanks again for the help, you guys are awesome! ;)

Sirena400
Feb 9, 2005, 11:50 AM
Shoot, I thought I could figure it out. Here's what I got so far:

#include <iostream>

int main()
{
char word[x];
cin>>word;
int score=0;
while(word[x]!='\$')
for(x=0;word[x]!='\0';x++)
{
switch(word[x])
{
case '#': score+=0;
break;
case 'A': case 'E': case 'I': case 'L': case 'N': case 'O':
case 'R': case 'S': case 'T': case 'U': score+=1;
break;
case 'D': case 'G': score+=2;
break;
case 'B': case 'C': case 'M': case 'P': score+=3;
break;
case 'F': case 'H': case 'V': case 'W': case 'Y': score+=4;
break;
case 'K': score+=5;
break;
case 'J': case 'X': score+=8;
break;
case 'Q': case 'Z': score+=10;
break;
}
}
return 0;
}

And somewhere in there I have 6 errors that I can't seem to find and I don't know how I should have the program add up the total. Any suggestions?

bbarnhart
Feb 9, 2005, 01:01 PM
Replace

while(word[x]!='\$')
for(x=0;word[x]!='\0';x++)

with

for (int x=0; word[x] != '\$'; x++)

therevolution
Feb 9, 2005, 01:26 PM
And somewhere in there I have 6 errors that I can't seem to find and I don't know how I should have the program add up the total. Any suggestions?
First thing I notice: what is 'x'? In the first line, when you have "char word[x];", x is undefined. You need to say "int x = 80;" or something like that before you use it.

Sirena400
Feb 9, 2005, 06:41 PM
Thanks bbarnheart, I think that's where my errors were. Thanks so much! therevolution: Oh I do? I didn't know that, my teacher said that we should use x so it could accept any value, although I may have either misunderstood or misheard him. What value would be best to set it to? 26 maybe?

Sirena400
Feb 10, 2005, 11:36 AM
Well I made some progress on the program. I fixed it with the suggestions you guys gave me but I still have 5 errors. It looks like this now:

#include <iostream>

int main()
{
int x=26;
char word[26];
cin>>word;
int score=0;
for (int x=0;word[x]!='\$';x++)
switch(word[x])
{
case '#': score+=0;
break;
case 'A': case 'E': case 'I': case 'L': case 'N': case 'O':
case 'R': case 'S': case 'T': case 'U': score+=1;
break;
case 'D': case 'G': score+=2;
break;
case 'B': case 'C': case 'M': case 'P': score+=3;
break;
case 'F': case 'H': case 'V': case 'W': case 'Y': score+=4;
break;
case 'K': score+=5;
break;
case 'J': case 'X': score+=8;
break;
case 'Q': case 'Z': score+=10;
break;
}
{
cout<<"The player's score is" <<score<<".";
}
return 0;
}

I also tried to add in the last statement that will show the result, though I'm not sure if it's right. Any and all help and suggestions to help fix this program would be very appreciated, and thanks for all the help so far. You guys have seriously great :D

bbarnhart
Feb 10, 2005, 04:05 PM
Still 5 errors? I'm at work now so I brought you code up in MS VC++ and I only got two errors. Maybe Project Builder for the Mac has more.

You need to put

std::

in front of cin and cout or

using namespace std;

at the top before you use cin or cout.

Also, you have a logic error with your '#'. Isn't it supposed to add a score of 1.

Also, scratch

int x=26;

Make word[26] something like word[100] because that is an array of characters to hold the 'scrabble letters'.

Also, check your brackets. You don't need one around cout. You should put one in the for loop (good style).

Good luck

Sirena400
Feb 11, 2005, 11:35 AM
Oh man, it's so close but not quite ther yet. :( The program actually works now and all errors are gone which is good! Thanks for the much needed help bbarnhart! But the problem is that the program isn't adding things up right. When I type in the first example I gave in the first post, DAY#TIME, the score comes up as either 74 or 78 and not 13 like it's supposed to be. Another problem I see is that it leaves no space for me to enter the \$ below it, do I need to put in another cin>> to fix that? The code currently looks like this:
#include <iostream>

int main()
{
char word[100];
std::cin>>word;
int score=0;
for (int x=0;word[x]!='\$';x++)
switch(word[x])
{
{
case '#': score=0;
break;
case 'A': case 'E': case 'I': case 'L': case 'N': case 'O':
case 'R': case 'S': case 'T': case 'U': score+=1;
break;
case 'D': case 'G': score+=2;
break;
case 'B': case 'C': case 'M': case 'P': score+=3;
break;
case 'F': case 'H': case 'V': case 'W': case 'Y': score+=4;
break;
case 'K': score+=5;
break;
case 'J': case 'X': score+=8;
break;
case 'Q': case 'Z': score+=10;
break;
}
}

std::cout<<"The player's score is "<<score<< ".";

return 0;
}

Something's wrong with my math, I just don't know what. Can any of you guys see the problem?

tutubibi
Feb 11, 2005, 01:15 PM
....

Something's wrong with my math, I just don't know what. Can any of you guys see the problem?

You are resetting score on #. Remove score=0.
Something like this should work:

#include <iostream>

using namespace std;

int main()
{
char word[100];
bool done = false;

int score = 0;

while( !done )
{
cin >> word;
for( int x=0; x < strlen(word); x++)
{
switch(word[x])
{
case '\$':
done = true;
break;
case 'A':
case 'E':
case 'I':
case 'L':
case 'N':
case 'O':
case 'R':
case 'S':
case 'T':
case 'U':
score+=1;
break;
case 'D':
case 'G':
score+=2;
break;
case 'B':
case 'C':
case 'M':
case 'P':
score+=3;
break;
case 'F':
case 'H':
case 'V':
case 'W':
case 'Y':
score+=4;
break;
case 'K':
score+=5;
break;
case 'J':
case 'X':
score+=8;
break;
case 'Q':
case 'Z':
score+=10;
break;
default:
break;
}
}
}
cout << "The player's score is " << score << ".";

return 0;
}

Sirena400
Feb 14, 2005, 11:30 AM
Thanks for your help tutubibi! I finally figured out my problems and fixed it and now my program works correctly! And a big, big thanks to everybody for all the help that you guys gave me, you guys are great! Thanks again! :)