为什么打印不正确?
Why isn't it printing properly?
#include <iostream>
#include <iomanip>
using namespace std;
int vCorrectOrWrong(string UserAnswer, string CorrectAnswer, int k);
string vAnswerSheet(string sArray[], string sUserAnswer, int iElement);
void vResults(int,int);
int main()
{
const int ctSIZE = 5;
string sQuestions[ctSIZE] = {"1. What was the first planet to be discovered by the telescope?", "2. What is the diameter of earth?",
"3. Name the yellow teletubby?", "4. In which state was the first oil well drilled in the Untied States?", "5. How many tentacles does a squid have?" };
string sLetterAnwers[ctSIZE] = {"a. Mars b. Mercury c. Uranus d. Jupiter", "a. 5,000 miles b. 6,000 miles c. 7,000 miles d. 8,000 miles",
"a. Dipsy b. Po c. LaLa d. Tinky Winky", "a. Pennsylvania b. Texas c. Wyoming d. North Dakota", "a. 8 b. 9 c. 10 d. 11"};
string sUserAnswer;
string a;
int i = 0;
int j = 0;
int k =0;
while(i != 5)
{
cout << sQuestions[i] << endl;
cout << sLetterAnwers[i] << endl;
cin >> sUserAnswer;
a = vAnswerSheet(sLetterAnwers,sUserAnswer,i);
j = vCorrectOrWrong(sUserAnswer, a,k);
i++;
}
vResults(j,ctSIZE);
cout << "PROGRAM ENDED!" << endl;
return 0;
}
string vAnswerSheet(string sArray[], string sUserAnswer, int i)
{
string x = sArray[i];
string Answer;
if(sArray[0] == x)
Answer = "c";
else if(sArray[1] == x)
Answer = "d";
else if(sArray[2] == x)
Answer = "c";
else if (sArray[3] == x)
Answer = "a";
else if (sArray[4] == x)
Answer = "c";
return Answer;
}
为什么不会k增量提前感谢!如果代码看起来很草率,我深表歉意,这对编码世界来说是非常新的。
int vCorrectOrWrong(string UserAnswer,string CorrectAnswer, int k)
{
if( UserAnswer != CorrectAnswer )
cout << "WRONG!" << endl;
else
{
cout <<"RIGHT!"<< endl;
k++;
}
return k;
}
///
void vResults(int y, int x)
{
cout << " You got: " << y << " of the " << x << " Questions correct!"<< endl;
y = y*100/x;
cout <<y<<"%"<< endl;
}
您可以通过引用 vResults 来传递值 k,这样就不必将相同的值返回到 j 中并使用它。喜欢这个-
您的函数 vCorrectOrWrong 将如下所示 -
int vCorrectOrWrong(string UserAnswer, string CorrectAnswer, int &k) {..}
而不是称它为——
j = vCorrectOrWrong(sUserAnswer, a,k);
..
..
vResults(j,ctSIZE);
这样称呼它——
vCorrectOrWrong(sUserAnswer, a,k);
..
..
vResults(k,ctSIZE);
您需要更好地跟踪变量。
在while (i != 5)
循环中,您不断将j
设置为vCorrectOrWrong
输出的任何输出。 问题是您始终使用 k
作为参数进行vCorrectOrWrong
,并且当k
用作参数时,它始终等于 0。 如果用户弄错了最后一个问题,则在输出中使用的末尾的j
将为 0,如果用户正确回答了最后一个问题,则为 1。
我强烈建议使用调试器或在代码中插入cout
语句,以帮助您跟踪变量中存储的值。 我会把解决这个问题留给你。
相关文章:
- wcout不会打印正确的单引号
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 为什么这里打印的复数不正确?
- C++:我的简单计算器程序无法打印出来。我也认为我的变量命名不正确
- C++ cout 在擦除换行符后打印字符串不正确
- cout 打印的值不正确
- 菱形打印不正确
- 排序向量不断修改数据并打印不正确的结果
- C++字符串到字符串打印不正确,无法获取 unicode 路径
- 使用图像 8UC1 c++ 的 unchar 垫正确打印不正确
- 类中的动态字符数组不会打印正确的结果
- 最长的公共子序列打印不正确
- 中缀到后缀转换器 + 评估程序正确打印到控制台,但不正确地打印到文本文件
- 为什么打印不正确?
- for_each打印数据不正确
- C编程,数组打印不正确
- Vector和String的Structs不能正确打印
- 在c++中打印动态创建的负数会导致不正确的输出
- 打印uint8_t变量不正确
- PHP扩展值不正确打印原因