user3361175
user3361175

Reputation: 11

Program is not returning specified values?

I made a game where the player types in the scrambled word. Take for example, I have the word 'wall' which is then jumbled up to saying wlal. For a correct answer I multiply the length of the given word to the user by 1000, then report the score at the end.

However, I also have a hint feature set up, so when they type in hint they get a hint. As a penalty, I'd like it so the user get's their score cut in half. Also, if the player answers incorrectly, there is a 1000 point reduction to the score.

My program always sets the score to 0. What's the problem here.

EDITED CODE:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
using namespace std;

int main()
{

enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =    //5x2 array
{
    {"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 what the game is all about."}
};

srand(static_cast<unsigned int>(time(0)));
int choice = rand() % NUM_WORDS;
//Choice value in array, than area in array where word and hint are
string theWord = WORDS[choice][WORD]; //word to guess
string theHint = WORDS[choice][HINT]; //hint for word

string jumble = theWord;  //jumbled version of word
int length = jumble.size();
//Index1 and index2 are random locations in the string theWord
//last two lines swaps areas, ending the for function with a different
//jumble variable every time.
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\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "\n\n\nReady? (y/n)";

//I'm asking here if the player is ready
string isready;
cin >> isready;

if ((isready == "y") || (isready == "Y"))
{
    cout << "Ok this is how the scoring works\n";
    cout << "The length of the word you will guess is times by 5000.\n";
    cout << "If you ask for a hint, your score will go down by half.\n";
    cout << "If you get the wrong answer, your score will go down by 1000.";
    cout << "\nOk, lets start!\n";
    int counter = 3;
    for(int i = 0; i < 3; ++i)
    {
        sleep(1);
        cout << counter << "..." << endl;
        counter--;
    }

    sleep(1);
}
else 
{
cout << endl;
return 0;
}
cout << "Enter 'quit' to quit the game.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "The jumble is: " << jumble;

//Score system
double score;
double amount_of_guesses, amount_of_wrong = 0;




string guess;
cout << "\n\nYour guess: ";
cin >> guess;
double wrong = 0;
while ((guess != theWord) && (guess != "quit"))
{
    if (guess == "hint")
    {
        cout << theHint;
        amount_of_guesses++;

    }


    else if (guess != theWord)
    {
        cout << "Sorry, that's not it.";
        wrong++;
    }

    cout << "\n\nYour guess: ";
    cin >> guess;
}


if (guess == theWord)
{
    score = (theWord.length() * 1000);
}

if (amount_of_guesses != 0)
{
        score = score / (amount_of_guesses * 2);
}

if( wrong != 0)
{
        score = score - (wrong * 1000);
}


cout << "Your score is: " << score;
cout << "\nThanks for playing.\n";
return 0;

}

Upvotes: 0

Views: 71

Answers (3)

Code_Jamer
Code_Jamer

Reputation: 913

Your code works fine for me when double amount_of_guesses, amount_of_wrong = 0; is changed as: double amount_of_guesses=0; double amount_of_wrong = 0;

Additionally, the code score = score / (amount_of_guesses * 2); does not correctly penalize the player by cutting his or her score in half each time they ask for a hint.

You could replace that line with the following code segment to correctly penalize by cutting the score in half every time:

if (amount_of_guesses != 0)
{   while(amount_of_guesses!=0)
   {
        score = (score / 2);
        amount_of_guesses--;
   }
}

Upvotes: 2

Oliver Dain
Oliver Dain

Reputation: 9963

I believe the issue is that you never initialize amount_of_guesses. This declaration

int amount_of_guesses, amount_of_wrong = 0;

initializes amount_of_wrong to 0, but amount_of_guesses will just hold some random value that depends on what happens to be in memory. If that's a large value, then this:

if (amount_of_guesses != 0)
{
        score = score / (amount_of_guesses * 2);
}

will end up making score == 0 (note that since score is an integer score / (amount_of_guesses * 2) ends up being the floor of that division)

Upvotes: 0

Eric Fortin
Eric Fortin

Reputation: 7603

The code

int amount_of_guesses, amount_of_wrong = 0;

does not initialize amount_of_guesses to 0. It is likely to be a huge number since it will be what is in memory when program is running. If score is lower than amount_of_guesses, result will be 0 using integer division.

Upvotes: 1

Related Questions