C++ help?

Discussion in 'Mac Programming' started by Sirena400, Feb 7, 2005.

  1. macrumors newbie

    Joined:
    Feb 7, 2005
    #1
    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? :(
     
  2. macrumors 6502

    Joined:
    Oct 5, 2002
    Location:
    Greenville, SC
    #2
    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++)
    {
    switch( string )
    {
    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.


     
  3. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #3
    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. :)
     
  4. macrumors 6502

    Joined:
    Oct 5, 2002
    Location:
    Greenville, SC
    #4
    So since you don't know switch, you should probably use if else stuff...so basically, format would look like...

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

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

    score += string == '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.

     
  5. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #5
    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! :)
     
  6. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #6
    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.
     
  7. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #7
    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.
     
  8. macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #8
    Characters, 'A', are basically integers and can be used in a switch. Strings, "ABC", can not be used in a switch statement.
     
  9. macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #9
    // 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];
    }
     
  10. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #10
    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];
    }
     
  11. macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #11


    #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 == '#')
    value += 1;
    else
    {
    std::cout << "Letter " << example << "\n";
    value += (int)array[example-60];
    std::cout << "Value "<< (int)array[example-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
     
  12. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #12
    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! ;)
     
  13. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #13
    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?
     
  14. macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #14
    Replace

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

    with

    for (int x=0; word[x] != '$'; x++)
     
  15. macrumors 6502

    Joined:
    May 12, 2003
    #15
    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.
     
  16. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #16
    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?
     
  17. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #17
    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
     
  18. macrumors 6502a

    bbarnhart

    Joined:
    Jan 16, 2002
    Location:
    Stilwell, Kansas
    #18
    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
     
  19. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #19
    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?
     
  20. macrumors 6502a

    tutubibi

    Joined:
    Sep 18, 2003
    Location:
    localhost
    #20

    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;
    }
     
  21. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #21
    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! :)
     

Share This Page