我有一个程序,旨在从 2 个单独的文本文件中获取数据,将它们放在数组中,将它们相互比较,并输出某些结果。但是,当我从文件中读取数据并尝试显示数据时,在最终显示文本文件中的所有数据之前,我得到了很多奇怪的符号。这是代码。
// Ch07-Exam Grader.cpp : Defines the entry point for the console application.
//
//Libraries
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Prototypes
void initialization(void);
void proccess(void);
void eoj(void);
void writeIt(void);
void readIt(void);
void calculate(void);
//Global Variables
ifstream student;
ifstream correct;
int main()
{
initialization();
return 0;
}
//This function opens the files and calls the function to send the data into the array
void initialization (void){
correct.open("CorrectAnswers.txt");
readIt();
student.open("StudentAnswers.txt");
readIt();
}
void proccess (char c[], char s[], int length){
int correctCount = 0;
int incorrectCount = 0;
for (int i = 0; i< length; i++){
if (s[i] == c[i]){
correctCount = correctCount + 1;
} else {
incorrectCount = incorrectCount + 1;
}
}
}
void eoj (void){
}
void writeIt (void){
}
//This function will take the data and place it into seperate arrays
void readIt (void){
char studentArray[20]; //Array to hold the student answers
char correctArray[20]; //Array to hold the correct answers
//Loops to place data to seperate arrays
for (int i = 0; !correct.eof(); i++){
correct >> correctArray[i];
}
for (int j = 0; !student.eof(); j++){
student >> studentArray[j];
}
for (int i = 0; i < 20; i++){
cout << studentArray[i] <<endl;
}
proccess(correctArray, studentArray, 20);
}
void calculate (void){
}
这是结果:
只有字母是文本文件的一部分。