[英]How can I get my counter to work properly?
I'm doing my c++ homework on arrays, everything seems to be working fine except the counter for the problem.我正在 arrays 上做我的 c++ 作业,除了问题的计数器外,一切似乎都工作正常。 Before I show the code these are the requirements for the question
在我展示代码之前,这些是问题的要求
This is what my code looks like这就是我的代码的样子
#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;
}
This is a sample output I got这是我得到的样本 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:
I don't know why my counter is not working, would appreciate some feedback and solutions!我不知道为什么我的计数器不工作,希望得到一些反馈和解决方案!
The Fix:修复:
int i = 0;
int yes = 0;
By default, C++ will not initialize your variables.默认情况下,C++ 不会初始化您的变量。 Unless you do it explicitly, they will contain a garbage value.
除非您明确地这样做,否则它们将包含垃圾值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.