2013-10-03 40 views
0

當輸入來自文本文件時,我似乎遇到了我的數組輸出問題......雖然輸出應該是單個字符(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

哪一行是錯誤行?我不會算。 – nhgrif

+0

不要在'for'循環中執行'i ++',你已經在循環的頭部做了這個。 – Barmar

+0

@nhgrif這是第29行:readFile(「CorrectAnswers.txt」,correct,SIZE); –

回答

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++在循環中,它已經在循環插頭來實現。

+0

在函數中使用SIZE作爲參數名稱也是令人困惑的,因爲OP在整個pgm中將它用作常量。 「尺寸」會更好。 – Duck

+0

的確如此,但如果我在答案中提出這個問題,我將不得不重寫整個程序,因爲他在所有功能中都這麼做。 – Barmar

+0

沒有必要改變你的答案。評論更多的是對OP的風格筆記。 – Duck

相關問題