程序没有返回指定的值?

时间:2014-03-07 18:11:15

标签: c++ if-statement

我做了一个游戏,其中玩家键入了扰乱的单词。举个例子,我有“墙”这个词,然后混淆说是wlal。为了得到正确的答案,我将给定单词的长度乘以1000,然后在结尾处报告分数。

但是,我也有一个提示功能设置,所以当他们输入提示时,他们会得到一个提示。作为一个惩罚,我喜欢它,所以用户得到他们的分数减半。此外,如果玩家回答错误,则得分会减少1000分。

我的程序总是将分数设置为0.这里有什么问题。

编辑代码:

#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;

}

3 个答案:

答案 0 :(得分:2)

double amount_of_guesses, amount_of_wrong = 0;更改为double amount_of_guesses=0; double amount_of_wrong = 0;

时,您的代码可以正常使用

此外,代码score = score / (amount_of_guesses * 2);并未通过每次要求提示时将其分数减半来正确惩罚玩家。

您可以使用以下代码段替换该行,以便通过每次将分数减半来正确惩罚:

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

答案 1 :(得分:1)

代码

int amount_of_guesses, amount_of_wrong = 0;

没有将amount_of_guesses初始化为0.它可能是一个很大的数字,因为它将是程序运行时的内存。如果score低于amount_of_guesses,则使用整数除法将结果为0。

答案 2 :(得分:0)

我认为问题在于你永远不会初始化amount_of_guesses。这个声明

int amount_of_guesses, amount_of_wrong = 0;

将amount_of_wrong初始化为0,但amount_of_guesses将只保留一些随机值,该值取决于内存中发生的情况。如果这是一个很大的值,那么:

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

最终会得分== 0(请注意,因为得分是一个整数score / (amount_of_guesses * 2)最终成为该部门的最低点)