Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序没有返回指定的值?_C++_If Statement - Fatal编程技术网

C++ 程序没有返回指定的值?

C++ 程序没有返回指定的值?,c++,if-statement,C++,If Statement,我做了一个游戏,玩家在游戏中输入加扰的单词。举个例子,我有一个单词“wall”,这个词后来被混淆成wlal。为了得到正确答案,我将给定单词的长度乘以1000,然后在最后报告分数 不过,我还设置了提示功能,所以当他们输入提示时,他们会得到提示。作为惩罚,我希望用户的分数减半。此外,如果玩家回答不正确,分数会降低1000分 我的程序总是将分数设置为0。这里有什么问题 编辑代码: #include <iostream> #include <string> #include &l

我做了一个游戏,玩家在游戏中输入加扰的单词。举个例子,我有一个单词“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;
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
枚举字段{WORD,HINT,NUM_fields};
const int NUM_WORDS=5;
常量字符串字[NUM\u WORDS][NUM\u FIELDS]=//5x2数组
{
{“墙”,“你觉得你的头撞到什么东西了吗?”},
{“眼镜”,“这些可能会帮助你看到答案。”},
{“辛苦”,“走得慢,是吗”},
{“坚持”,“坚持下去。”},
{“混乱”,“这就是游戏的全部内容。”}
};
静态施法(时间(0));
int choice=rand()%NUM\u单词;
//选择数组中的值,而不是数组中单词和提示所在的区域
string theWord=WORDS[choice][WORD];//要猜的单词
string theHint=WORDS[choice][HINT];//单词提示
string jumble=theWord;//word的混乱版本
int length=jumble.size();
//Index1和index2是字符串中的随机位置
//最后两行交换区域,以不同的结束for函数
//每次都会混淆变量。
对于(int i=0;i
int amount_of_guesses, amount_of_wrong = 0;
不会将
amount\u猜测
初始化为0。这可能是一个巨大的数字,因为它将是程序运行时内存中的数字。如果
score
低于
amount\u猜测
,则使用整数除法的结果将为0。

代码

int amount_of_guesses, amount_of_wrong = 0;
不会将
amount\u猜测
初始化为0。这可能是一个巨大的数字,因为它将是程序运行时内存中的数字。如果
score
低于
amount\u猜测
,则使用整数除法的结果将为0。

代码

int amount_of_guesses, amount_of_wrong = 0;
不会将
amount\u猜测
初始化为0。这可能是一个巨大的数字,因为它将是程序运行时内存中的数字。如果
score
低于
amount\u猜测
,则使用整数除法的结果将为0。

代码

int amount_of_guesses, amount_of_wrong = 0;

不会将
猜测数量
初始化为0。这可能是一个巨大的数字,因为它将是程序运行时内存中的数字。如果
分数
低于
猜测数量
,则使用整数除法得到的结果将是0。

我相信问题是您从未初始化
猜测数量
。本声明

int amount_of_guesses, amount_of_wrong = 0;
将amount_of_error初始化为0,但amount_of_猜测将只保留一些随机值,这取决于内存中发生的情况。如果这是一个大值,则如下所示:

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

将最终得到分数==0(注意,由于分数是一个整数
score/(猜测的数量*2)
最终成为该分区的底部)

我认为问题在于您从未初始化
猜测的数量

int amount_of_guesses, amount_of_wrong = 0;
将amount_of_error初始化为0,但amount_of_猜测将只保留一些随机值,这取决于内存中发生的情况。如果这是一个大值,则如下所示:

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

将最终得到分数==0(注意,由于分数是一个整数
score/(猜测的数量*2)
最终成为该分区的底部)

我认为问题在于您从未初始化
猜测的数量

int amount_of_guesses, amount_of_wrong = 0;
将amount_of_error初始化为0,但amount_of_猜测将只保留一些随机值,这取决于内存中发生的情况。如果这是一个大值,则如下所示:

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

将最终得到分数==0(注意,由于分数是一个整数
score/(猜测的数量*2)
最终成为该分区的底部)

我认为问题在于您从未初始化
猜测的数量

int amount_of_guesses, amount_of_wrong = 0;
将amount_of_error初始化为0,但amount_of_猜测将只保留一些随机值,这取决于内存中发生的情况。如果这是一个大值,则如下所示:

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

将最终得到分数==0(请注意,由于分数是一个整数
score/(猜测的数量*2)
最终成为该分区的底部)

当猜测的数量加倍时,您的代码对我来说运行良好,错误的数量=0;
更改为:
猜测的数量加倍=0;错误的数量加倍=0;

此外,代码
score=score/(猜测的数量*2);
不会在玩家每次要求提示时将其分数减半,从而正确惩罚玩家

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

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

double-amount-of-of-ou猜测,amount-of-ou错误=0;
更改为:
double-amount-of-ou猜测=0;double-amount-of-ou错误=0;

此外,代码
score=score/(猜测的数量*2);
不会在玩家每次要求提示时将其分数减半,从而正确惩罚玩家

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

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

double-amount-of-of-ou猜测,amount-of-ou错误=0;
更改为:
double-amount-of-ou猜测=0;double-amount-of-ou错误=0;

此外,代码
score=score/(猜测的数量*2);
不能正确使用