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

时间:2013-10-03 22:44:47

标签: c++ arrays function character

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

29  46  C:\Users\Robert\Desktop\bob project\Proj1.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`}

1 个答案:

答案 0 :(得分:1)

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

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++,它已经在循环标题中完成了。