Public Group

# Having trouble with scoring in a game

This topic is 4485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I am still a C++ noob and I have been progressivly learning and I have a book that is teaching me. The last activity at th end of the chapter I am on is Word Jumble. it gives me the code and I put it in manually and it explains it to me. One of the activites at the chapter's end was to put a scoring system in the word jumble. So this is what i did. I scored the player based on how many letters were in the word. Now that worked out find and it told you your score. This is what i can't get to work. I want to subtract points if you ask for a hint. i can't seem to do it. Here's the code:
//Word Jumble
//The classic word jumble game where the player can ask for a hint

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"labored", "Going slowly is it?"},
{"persistent", "Keep at it"},
{"jumble", "It's the name of the game"},

};

srand(time(0));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; //word to guess
string theHint = WORDS[choice][HINT]; // hint for word

string jumble = theWord; // jumbled version of the word
int length = jumble.size();
for (int i = 0; i < length; ++ i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);

char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}

cout << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " <<jumble;

string guess;
cin >> guess;
{
int score = length;
while((guess != theWord) && (guess !="quit"))
{
if (guess =="hint")
cout << theHint;

else
cout << "Sorry, that's not it.";

cin >> guess;
}

if (guess ==theWord)
cout << "\nThat's it! You guessed it!\n";

cout << "\nYou scored " << score << " points!" << endl;

}
cout << "\nThanks for playing.\n";

cin.get();
system("pause");
return 0;

}


I tried numerous things. I'm sure one of you can help me out?

##### Share on other sites
        int score = length;    while((guess != theWord) && (guess !="quit"))    {                if (guess =="hint")                   cout << theHint;                  //Here you change the score, by subtracting three points if asked for a hint                  score -= 3;                else                    cout << "Sorry, that's not it.";                                cout << "\n\nYour guess: ";                cin >> guess;}        if (guess ==theWord)    cout << "\nThat's it! You guessed it!\n";        cout << "\nYou scored " << score << " points!" << endl;

I added score -= 3; which subtracts '3' from int score. You can also write it as, and should get in the habit of reading it as, score = score - 3; or score = (score - 3); they are all the same thing. You can also use '+=' to add to it. '=' assigns a number, so score = 3 makes score be equal to 3, but score -= 3 negatives three points from score, and score += 3 adds three points to score.

##### Share on other sites
Thanks, I guess I just forgot the basics of arithemetic operation lol.

##### Share on other sites
I'm getting a weird error. It says it expects primary expression before else. This iso nly when I put the score -3 in and when I take it out the game runs again....

##### Share on other sites
Heh, put it in '{''}' brackets, so it recognizes what your trying to do easier.

Here, see if this works.
//Word Jumble//The classic word jumble game where the player can ask for a hint#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int main(){    enum fields {WORD, HINT, NUM_FIELDS};    const int NUM_WORDS = 5;    const string WORDS[NUM_WORDS][NUM_FIELDS] =     {          {"wall", "Do you feel you're banging your head against something?"},          {"glasses", "These might help you see the answer"},          {"labored", "Going slowly is it?"},          {"persistent", "Keep at it"},          {"jumble", "It's the name of the game"},              };        srand(time(0));    int choice = (rand() % NUM_WORDS);    string theWord = WORDS[choice][WORD]; //word to guess    string theHint = WORDS[choice][HINT]; // hint for word        string jumble = theWord; // jumbled version of the word    int length = jumble.size();    for (int i = 0; i < length; ++ i)    {        int index1 = (rand() % length);        int index2 = (rand() % length);                char temp = jumble[index1];        jumble[index1] = jumble[index2];        jumble[index2] = temp;    }        cout << "\t\t\tWelcome to Word Jumble!\n\n";    cout << "Unscramble the letters to make a word.\n";    cout << "Enter 'hint' for a hint.\n";    cout << "Enter 'quit' to quit the game.\n\n";    cout << "The jumble is: " <<jumble;        string guess;    cout << "\n\nYour guess: ";    cin >> guess;    {        int score = length;        while((guess != theWord) && (guess !="quit"))        {            if (guess =="hint"){cout << theHint; score -= 3;}                              else            cout << "Sorry, that's not it.";                            cout << "\n\nYour guess: ";            cin >> guess;        }            if(guess ==theWord)        cout << "\nThat's it! You guessed it!\n";            if(guess != "quit")        cout << "\nYou scored " << score << " points!" << endl;              }    cout << "\nThanks for playing.\n";        cin.get();    system("pause");    return 0;}

Also, when quiting, it tells you your score also, so there is a line to block that, too.

##### Share on other sites
Yea you have to use brackets if there is more than one line inside the if. For example

if (guess == "hint")
cout << theHint;

works fine, but when you want to add in the score -= 3 along with the cout << theHint, it is now more than one line long and the compiler requires it to be inside of a pair of '{'. so

if (guess == "hint")
cout << theHint;
score -= 3;

will not work, but

if (guess == "hint")
{
cout << theHint;
score -= 3;
}

will.

##### Share on other sites
Quote:
 Original post by Cyncro cin.get(); system("pause");

[opinion]
That seems redundant to me seeing as your arent really using the input for anything... the system pause will automatically cause it to wait for a key to be pressed.
[/opinion]

##### Share on other sites
No but it won't display any messages after u make a right guess. Ok I am trying to make the score display as 0 if you quit. I got it to do that but it also displays your score as if you played the game.... I tried a lot of different things but I can't get it to work. Can anyone help me?

##### Share on other sites
//Word Jumble//The classic word jumble game where the player can ask for a hint#include <iostream>#include <string>#include <cstdlib>#include <ctime>using namespace std;int main(){    enum fields {WORD, HINT, NUM_FIELDS};        const int NUM_WORDS = 5;    const string WORDS[NUM_WORDS][NUM_FIELDS] =     {          {"wall", "Do you feel you're banging your head against something?"},          {"glasses", "These might help you see the answer"},          {"labored", "Going slowly is it?"},          {"persistent", "Keep at it"},          {"jumble", "It's the name of the game"},              };        srand(time(0));    int choice = (rand() % NUM_WORDS);    string theWord = WORDS[choice][WORD]; //word to guess    string theHint = WORDS[choice][HINT]; // hint for word        string jumble = theWord; // jumbled version of the word    int length = jumble.size();    for (int i = 0; i < length; ++ i)    {        int index1 = (rand() % length);        int index2 = (rand() % length);                char temp = jumble[index1];        jumble[index1] = jumble[index2];        jumble[index2] = temp;    }        cout << "\t\t\tWelcome to Word Jumble!\n\n";    cout << "Unscramble the letters to make a word.\n";    cout << "Enter 'hint' for a hint.\n";    cout << "Enter 'quit' to quit the game.\n\n";        string guess;    int score = length;    while(true)    {        cout << "The jumble is: " <<jumble;        cout << "\n\nYour guess: ";        cin >> guess;        if(quess == "quit")            break;        else if(guess == "hint")        {            cout << theHint;            score -=3;        }        else if(guess == theWord)        {            cout << "\nThat's it! You guessed it!\n";            cout << "\nYou scored " << score << " points!" << endl;            break;        }        else cout << "Sorry, that's not it.";    }             cout << "\nThanks for playing.\n";        system("pause");    return 0;}

You should always work out the flow of the program by thinking it out and writing it down on paper before putting together any code so you come up with a cleaner solution requiring less case checks.

Also I dont recommend you start adding brackets where they arent needed. I saw you wrapped quite a few lines of code in uneeded brackets which could be a source for errors later on if you are not careful. Add brackets where they belong only... defining a function body, multi line if statements, loops, etc.

##### Share on other sites
The second source I posted eliminates the score if you quit. Or to be more specific, shows the score if you didn't quit.

Here's the line I used:
if(guess != "quit")        cout << "\nYou scored " << score << " points!" << endl;

Look at this line:
if(guess != "quit")

A quick view of comparison operaters:

'==' This means 'equal to' so if(X == 2) means 'if X is equal to 2'.
'>' This means 'greater then' so if(X > 2) will only work if X is a greater number than 2.
'<' Same as above, but 'less then' instead of greater.
'>=' This means 'greater than or eqaul to' So if a line looks like if(X >= 2) It will use the line after that if X is a number larger than 2 or is 2 itself.
'<=' Again, this is 'less then or equal to' So in the if(X <= 2) line, it will only work if X is equal to or less than 2
'!=' The '!' exclamation mark is read as 'not' so the line if(X != 2) is to be read as 'do this if X does not eqaul 2' So if X is any number above or below 2 it will spring and do the line(s) following the if statement.

So in the line:
if(guess != "quit")        cout << "\nYou scored " << score << " points!" << endl;

It will only display the score if you didn't type in 'quit'.

There are other comparison marks which are of great use as well, such as the '||' and '&&' which stand for 'or' and 'and' respectively. But this should help you understand the basics.

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 9
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009388
• ### Who's Online (See full list)

There are no registered users currently online

×