为什么我的数组没有输出正确的数据,这些数据是从外部文件输入的?

Why isn't my array outputting the correct data, which was input from an external file?

本文关键字:数据 从外部 文件 输入 我的 输出 为什么 数组      更新时间:2023-10-16

当输入来自文本文件时,我的数组输出似乎遇到了问题......虽然输出应该是单个字符(A,B,C,D),但当出现提示时,数组会输出乱码,如@,?等符号。任何形式的帮助将不胜感激。程序的成绩部分也没有计算,但我自己可以弄清楚。 该程序编译并运行,错误为:

29  46  C:UsersRobertDesktopbob projectProj1.cpp   [Warning] deprecated           
conversion from string constant to 'char*' [-Wwrite-strings]

第 32 行也有同样的错误

#include <iostream>
#include<cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;
// Global constants
const int SIZE = 20;    // The number of questions
const int numQuestions = 20;
// Function prototypes
void readFile(char filename[],char answers[], int SIZE);
void compareAnswers(char student[], char correct[], int SIZE, int & numMissed);
void displayTestResults(int numMissed, int SIZE);
int main()
{
    // Number of questions missed.
    int numMissed;
// Array to hold the correct answers
char correct[SIZE];
// Array to hold the student's answers
char student[SIZE];
//Read the correct answers.
readFile("CorrectAnswers.txt", correct, SIZE);
    // Read the student's answers.
readFile("StudentAnswers.txt", student, SIZE);
// Compare the student's answers with the correct
// answers.
compareAnswers(student, correct, SIZE, numMissed);
// Display the test results.
displayTestResults(numMissed, SIZE);
   system("PAUSE");
return 0;
}
// ********************************************************
// The readFile function reads the contents of an answer  *
// file into an array.                                    *
// ********************************************************
void readFile(char filename[], char values[], int SIZE)
{
fstream inFile;
// Open the file.
inFile.open(filename);
for (char i = 0; i < SIZE; i++)
{
inFile>>values[i];
inFile.close();
}
return;
}
// ********************************************************
// The compareAnswers function compares the elements of   *
// the student array with the elements of the correct     *
// array. For each student answer that is incorrect the   *
// funcction increments a counter that keeps track of the *
// number of incorrect answers, making this available via *
// a call by reference argument.  The function displays   *
// the question number answered incorrectly along with    *
// answer given by the student and the correct answer.    *
// ********************************************************
void compareAnswers(char student[], char correct[],
                int SIZE, int &numMissed)
{
// Initialize numMissed.
numMissed = 0;
cout<< "Checking the answers...n";
// Step through the answer arrays comparing
// each answer.
for (char i = 0; i < SIZE; i++){

  if(student[i] == correct[i])
  {i++;}

  else
  {
    numMissed++;
    cout<<"I'm sorry, you had the wrong answer for "<<i + 1<<"."<<endl;
    cout<<"Your answer was "<<student[i]<<".";
    cout<<endl;
    cout<<"The correct answer was "<<correct[i]<<".";
    i++;
    cin.ignore();
  }
}

return;
}
// ********************************************************
// The displayTestResults function displays the test      *
// statistics.                                            *
// ********************************************************   
void displayTestResults(int numMissed, int SIZE)
{ 
int temp;
    // Calculate the number of correctly answered
// questions.
int correctlyAnswered = SIZE - numMissed;
// Calculate the numeric score correctly rounded to the nearest tenth of a percent.
temp == static_cast<int>(static_cast<double>(correctlyAnswered/SIZE*10+0.5));
// Display the number of correctly answered
// questions.
cout<<" The number of questions you got correct is:" <<correctlyAnswered;
// Display the percentage of correctly
// answered questions.
cout<<" The percentage of questions you got correct is:"<<temp;
//Display the letter grade on the exam.
cout<<endl<<endl;
if (temp >=93)
{cout<<"Your letter grade is an: A";}
else if(temp>=89)
{cout<<"Your letter grade is an: A-";}
else if(temp>=87)
{cout<<"Your letter grade is a: B+";}
else if(temp>=83)
{cout<<"Your letter grade is a: B";}
else if(temp>=79)
{cout<<"Your letter grade is a: B-";}
else if(temp>=77)
{cout<<"Your letter grade is a: C+";}
else if(temp>=73)
{cout<<"Your letter grade is a: C";}
else if(temp>=69)
{cout<<"Your letter grade is a: C-";}
else if(temp>=67)
{cout<<"Your letter grade is a: D+";}
else if(temp>=63)
{cout<<"Your letter grade is a: D";}
else if(temp>=57)
{cout<<"Your letter grade is a: D-";}
else
{cout<<"Your letter grade is a: F"<<endl;}
return;
`enter code here`}

若要使警告静音,应将该函数声明为:

void readFile(const char *filename, char *answers, int SIZE)

它之所以抱怨,是因为您将一个常量的字符串文本传递给一个不将参数声明为 const 的函数。

要解决乱码问题,请将 for 循环的主体更改为:

if(student[i] != correct[i]) {
    numMissed++;
    cout<<"I'm sorry, you had the wrong answer for "<<i + 1<<"."<<endl;
    cout<<"Your answer was "<<student[i]<<".";
    cout<<endl;
    cout<<"The correct answer was "<<correct[i]<<".";
    cin.ignore();
}

您不需要在循环内执行i++,它已经在循环标头中完成。