2013-08-27 4 views
-1

2 개의 텍스트 파일에서 데이터를 가져 와서 배열에 배치하고 서로 비교하고 특정 결과를 출력하는 프로그램이 있습니다. 그러나 파일에서 데이터를 읽고 데이터를 표시하려고하면 마지막으로 텍스트 파일에 모든 데이터가 표시되기 전에 많은 이상한 기호가 나타납니다. 여기에 코드가 있습니다. 만 문자 텍스트 파일의 일부파일에서 데이터를 읽을 때 기호가 이상합니까?

weird output

:

// 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){ 

} 

는 그리고 이것은 결과입니다.

+0

적어도 입력 파일의 일부를 보여줍니다 .... 그리고 모든 경고와 함께 컴파일하고 디버거를 사용하는 법을 배우십시오. –

+1

'readIt()'은'correct'와'student'를 모두 읽지 만'student'를 열기 전에 호출하고 있습니다. – Barmar

+0

간단한 실수입니다. 도움을 많이 주셔서 감사합니다. 나는 그것을 고쳐야한다. – tserran

답변

0

왜 initialization() 함수에서 readIt()을 두 번 호출합니까? readIt()은 두 파일이 모두 열려 있다고 가정하지만 첫 번째 파일을 열고 readIt()을 호출하고 두 번째 파일을 열고 readIt()을 다시 호출합니다. 이 결함에서 문제의 원인 일 수 있습니까?

+0

그것은있었습니다. 나는 그것을 고쳐야한다. 고마워. – tserran

관련 문제