gpt4 book ai didi

c++ - 程序没有返回指定值?

转载 作者:行者123 更新时间:2023-11-28 00:32:35 26 4
gpt4 key购买 nike

我制作了一个游戏,玩家可以在其中输入乱序词。举个例子,我有单词“wall”,然后混杂成 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;

最佳答案

double amount_of_guesses, amount_of_wrong = 0; 更改为:double amount_of_guesses=0; 时,您的代码对我来说工作正常;双 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--;
}
}

关于c++ - 程序没有返回指定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22257584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com