簡體   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