繁体   English   中英

我怎样才能让我的柜台正常工作?

[英]How can I get my counter to work properly?

我正在 arrays 上做我的 c++ 作业,除了问题的计数器外,一切似乎都工作正常。 在我展示代码之前,这些是问题的要求

  1. 创建一个包含 10 个是或否问题的数组
  2. 提示用户输入是或否,是'Y'或'y'。 没有别的
  3. 打印回是的数量'
  4. 根据“是”的数量给出排名

这就是我的代码的样子

#include <iostream>
#include <string>
using namespace std;
int main() 
{
    //variables
    int i, yes;
    char choice;
    string mastery; 
    //declare array of questions
    string quiz [10] = {
    "1. Have you been gaming for more than three years? ", "2. Have you ever dressed as a videogame character for halloween? ", "3. Do you own more than one console? ", "4. Have you stayed up passed midnight to play games? ", "5. Have you ever thrown a controller out of rage? ", "6. Have you ever needed to look up a walk through for a game? ", "7. Do you have a main in a fighting game? ", "8. Ever watched an esports tournament? ", "9. Did you ever own a handheald console? ", "10. Ever had a crush on a character in a game? "
    };
    //header
  cout << "Are you truly a gamer? Take this quiz and find out!" << endl;
    cout << endl;
    //array 
    for (i = 0; i < 10; i++)
        {
            cout << quiz[i];
            cin >> choice; 

            if (choice == 'Y' || choice == 'y')
            {
                yes = yes + 1;
            }
                
        }
    cout << endl;
    //amount of yes'
    cout << "You answered yes " << yes << " times" << endl;
    //calculate mastery level
    if (yes <=2)
    {
        mastery = "Novice";
    }
    else if (yes <= 5)
    {
        mastery = "Apprentice";
    }
    else if (yes <= 8)
    {
        mastery = "adept";
    }
    else if (yes <= 10)
    {
        mastery = "Expert";
    }    
    //print mastery level
    cout << "Your gamer rank is: " << mastery << endl;
}

这是我得到的样本 output

Are you truly a gamer? Take this quiz and find out!

1. Have you been gaming for more than three years? y
2. Have you ever dressed as a videogame character for halloween? y
3. Do you own more than one console? y
4. Have you stayed up passed midnight to play games? y
5. Have you ever thrown a controller out of rage? y
6. Have you ever needed to look up a walk through for a game? y
7. Do you have a main in a fighting game? y
8. Ever watched an esports tournament? y
9. Did you ever own a handheald console? y
10. Ever had a crush on a character in a game? y

You answered yes 4821545 times
Your gamer rank is: 

我不知道为什么我的计数器不工作,希望得到一些反馈和解决方案!

修复:

int i = 0;
int yes = 0;

默认情况下,C++ 不会初始化您的变量。 除非您明确地这样做,否则它们将包含垃圾值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM