Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 7, 2005, 10:15 PM   #1
Sirena400
macrumors newbie
 
Join Date: Feb 2005
C++ help?

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 is offline   0 Reply With Quote
Old Feb 7, 2005, 11:05 PM   #2
Zeke
macrumors 6502
 
Join Date: Oct 2002
Location: Greenville, SC
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.


Quote:
Originally Posted by Sirena400
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?

Last edited by Zeke; Feb 7, 2005 at 11:08 PM.
Zeke is offline   0 Reply With Quote
Old Feb 8, 2005, 12:19 PM   #3
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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 is offline   0 Reply With Quote
Old Feb 8, 2005, 01:45 PM   #4
Zeke
macrumors 6502
 
Join Date: Oct 2002
Location: Greenville, SC
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.

Quote:
Originally Posted by Sirena400
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 is offline   0 Reply With Quote
Old Feb 8, 2005, 10:20 PM   #5
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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!
Sirena400 is offline   0 Reply With Quote
Old Feb 8, 2005, 10:31 PM   #6
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: Andover, MA
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:
Code:
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.
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)
jsw is offline   0 Reply With Quote
Old Feb 8, 2005, 10:33 PM   #7
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: Andover, MA
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.
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)
jsw is offline   0 Reply With Quote
Old Feb 8, 2005, 11:15 PM   #8
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
Quote:
Originally Posted by jsw
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 is offline   0 Reply With Quote
Old Feb 8, 2005, 11:25 PM   #9
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
// 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;

for (all your letters)
{
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];
}

Last edited by bbarnhart; Feb 8, 2005 at 11:30 PM.
bbarnhart is offline   0 Reply With Quote
Old Feb 8, 2005, 11:51 PM   #10
jsw
Moderator emeritus
 
jsw's Avatar
 
Join Date: Mar 2004
Location: Andover, MA
Quote:
Originally Posted by bbarnhart
Code:
// 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;

for (all your letters)
{
  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:

Code:
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:

Code:
for (...each letter...)
{
  total += value[letter];
}
__________________
You'll be the one moaning for me to give you some. - THC(taken out of context)
jsw is offline   0 Reply With Quote
Old Feb 9, 2005, 07:19 AM   #11
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
[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

Last edited by bbarnhart; Feb 9, 2005 at 07:28 AM.
bbarnhart is offline   0 Reply With Quote
Old Feb 9, 2005, 12:18 PM   #12
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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 is offline   0 Reply With Quote
Old Feb 9, 2005, 12:50 PM   #13
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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?
Sirena400 is offline   0 Reply With Quote
Old Feb 9, 2005, 02:01 PM   #14
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
Replace

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

with

for (int x=0; word[x] != '$'; x++)
bbarnhart is offline   0 Reply With Quote
Old Feb 9, 2005, 02:26 PM   #15
therevolution
macrumors 6502
 
Join Date: May 2003
Quote:
Originally Posted by Sirena400
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.
therevolution is offline   0 Reply With Quote
Old Feb 9, 2005, 07:41 PM   #16
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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 is offline   0 Reply With Quote
Old Feb 10, 2005, 12:36 PM   #17
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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
Sirena400 is offline   0 Reply With Quote
Old Feb 10, 2005, 05:05 PM   #18
bbarnhart
macrumors 6502a
 
bbarnhart's Avatar
 
Join Date: Jan 2002
Location: Stilwell, Kansas
Send a message via Skype™ to bbarnhart
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
bbarnhart is offline   0 Reply With Quote
Old Feb 11, 2005, 12:35 PM   #19
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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?
Sirena400 is offline   0 Reply With Quote
Old Feb 11, 2005, 02:15 PM   #20
tutubibi
macrumors 6502a
 
tutubibi's Avatar
 
Join Date: Sep 2003
Location: localhost
Quote:
Originally Posted by Sirena400

....

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;
}
tutubibi is offline   0 Reply With Quote
Old Feb 14, 2005, 12:30 PM   #21
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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!
Sirena400 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 05:13 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC