#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;
}
Почему wont k increment Заранее благодарим! Я прошу прощения, если код выглядит неряшливым, очень новым для мира кодирования.Почему он не печатает правильно?
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;
}
Предлагаю вам ознакомиться с передачей параметров по значению vs по ссылке в вашем любимом учебнике на C++. –