为什么打印不正确?

时间:2016-01-30 03:53:51

标签: c++ increment

Initializing jdb ...
> 

为什么不增加提前谢谢!如果代码对编码世界看起来很邋。,我很抱歉。

#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;
}

////////////////////////////////

 int vCorrectOrWrong(string UserAnswer,string CorrectAnswer, int k)
 {
 if( UserAnswer != CorrectAnswer )
     cout << "WRONG!" << endl;
     else
     {
         cout <<"RIGHT!"<< endl;
         k++;
     }
    return k;
  }

2 个答案:

答案 0 :(得分:0)

您可以通过引用将值k传递给vResults,这样您就不必将相同的值返回到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);

答案 1 :(得分:0)

您需要更好地跟踪变量。

while (i != 5)循环内,您始终将j设置为vCorrectOrWrong的输出。问题是,当k用作参数时,您始终使用vCorrectOrWrong作为k的参数, j始终等于0 。如果用户最后一个问题出错,那么您在输出中使用的最后cout将为0;如果用户得到最后一个问题,则为{1}。

我强烈建议您使用调试器或在代码中插入function ClearOld() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sh = ss.getSheetByName('Done'); SpreadsheetApp.setActiveSheet(sh); var MyRange = sh.getRange(1,3,sh.getLastRow(),1);//selects C column var dataInColumnC = MyRange.getValues(); //Logger.log('dataInColumnC: ' + dataInColumnC); var todaysDate = new Date(); var todayAsMilliseconds = todaysDate.getTime(); Logger.log('todayAsMilliseconds: ' + todayAsMilliseconds); var WeekAgo = todayAsMilliseconds - (7*(24*3600*1000)); Logger.log('WeekAgo: ' + WeekAgo); var valueInCell, dateInCell, lengthOfData = dataInColumnC.length; for (var i=lengthOfData; i > 0; i-=1) { //decrement count if (dataInColumnC[i-1] === undefined) {continue}; valueInCell = dataInColumnC[i-1][0]; if (valueInCell === undefined || valueInCell === "") {continue}; Logger.log('valueInCell: ' + valueInCell); Logger.log('typeof valueInCell: ' + typeof valueInCell); dateInCell = valueInCell.getTime(); Logger.log('dateInCell: ' + dateInCell); if(dateInCell < WeekAgo){ Logger.log('Row - ' + i + ' Is more than a week old'); sh.deleteRow(i); }; }; }; 语句,以帮助您跟踪变量中存储的值。我会把解决这个问题留给你。