2016-03-11 168 views
-5

我正在做一個任務,要求創建一些函數分別讀取數據,計算分數和輸出數據。C++無效轉換int *到int

我覺得我很接近有這個完整的,但繼續運行到以下錯誤:

66 [錯誤]無效的轉換,從int (*)(studentType*, int)int [-fpermissive]

30 [錯誤]初始化的void printResults(std::ofstream&, studentType*, int, int)參數3 -fpermissive]

我搜索通過計算器和其他論壇,但不能完全得到它的底部。有誰知道我的代碼可能會導致這些錯誤?

我也有一個偷偷摸摸的懷疑,我傳遞價值的方式並不完全是現貨。我知道這是一個開放式的問題,但任何反饋將不勝感激。

下面是代碼:

#include <iostream> 
    #include <string> 
    #include <fstream> 
    #include <iomanip> 

    using namespace std; 

    //sets limit for students 
    const int max_students = 20; 

    //student info struct 
    struct studentType 
    { 
     string firstName; 
     string lastName; 
     int score; 
     char grade; 
    }; 

    //function declarations 
    void dataInput(ifstream& inFile, studentType slist[], int listSize); 
    void calcGrade(studentType sList[], int listSize); 
    void printResults(ofstream& outFile, studentType sList[], int highScore, int listSize); 
    int highScore(studentType sList[], int listSize); 

    int main() 
    { 

     ifstream inFile; 
     ofstream outFile; 
     int listSize = max_students; 
     studentType sList[max_students]; 

     //checks for input file 
     inFile.open("scores.txt"); 

      if (!inFile) 
      { 
       cout << "No input file detected.. please try again." << endl; 

       return 0; 
      } 

     //checks for output file  
     outFile.open("results.txt"); 

      if (!outFile) 
      { 
       cout << "Cannot locate output file.. please try again" << endl; 

       return 0; 
      } 

     //calls functions 
     dataInput(inFile, sList, listSize); 

     calcGrade(sList, listSize); 

     printResults(outFile, sList, highScore, listSize); 

     return 0; 

    } 

    //function to collect names and scores from each student 
    void dataInput(ifstream& inFile, studentType sList[], int listSize) 
    { 
     for(int i = 0; i < listSize; i++) 
     { 

      inFile >> sList[i].firstName >> sList[i].lastName >> sList[i].score; 

     } 

    } 

    //calculates grades based on input scores 
    void calcGrade(studentType sList[], int score, int listSize) 
    { 

     for(int i = 0; i < listSize; i++) 
     { 
      if (score >= 90) 
       sList[i].score = 'A'; 
      else if (score >= 80) 
       sList[i].score = 'B'; 
      else if (score >= 70) 
       sList[i].score = 'C'; 
      else if (score >= 60) 
       sList[i].score = 'D'; 
      else 
       sList[i].score = 'F'; 

     } 

    } 

    //calculates highest score(s) 
    int highScore(studentType sList[], int listSize) 
    { 

     int i = 0; 

     int highScore = sList[i].score; 

     for(i; i < listSize; i++) 
     { 
      if(sList[i].score > highScore) 
      { 
       highScore = sList[i].score; 
      } 

     } 


     return highScore; 
    } 

    //prints results into output file 
    void printResults(ofstream& outFile, studentType sList[], int highScore, int listSize) 
    { 

     int topScore = highScore; 
     int i = 0; 

     //header 
     outFile << setw(10) << "Name " << setw(10) << "Score " << setw(10) << "Grade " << endl; 

     for (int i = 0; i < listSize; i++) 
     { 
      //content 
      outFile << left << setw(25) << sList[i].lastName << ", " << sList[i].firstName << 
      right << " " << setw(5) << sList[i].score << setw(5) << " " << sList[i].grade << endl; 

      outFile << " " << endl; 

      outFile << "Highest test score: " << topScore << endl; 

      outFile << "Students with the highest score: " << endl; 

      for(int i = 0; i < listSize; i++) 
      { 
       if (sList[i].score == topScore) 
       { 
        outFile << sList[i].lastName << ", " << sList[i].firstName << endl; 
       } 

      } 

     } 

    } 
+1

你真的認爲這有助於發佈所有的代碼?! – Bathsheba

+0

@Bathsheba我是這個網站的新手。我不記得在建議中沒有這樣做,但是謝謝你的發言。 – elpretentio

+0

我認爲這將有助於建立背景,但我現在不知道。 – elpretentio

回答

0

的問題是在這裏:

int main() 
    ... 
    printResults(outFile, sList, highScore, listSize); 
    return 0; 
} 

的第三個參數你把highScore,但它是你聲明的功能!它應該是一個int的數字。

+0

謝謝,我正在糾正現在 – elpretentio

1

兩個主要問題:

  1. 你定義void calcGrade(studentType sList[], int listSize);但隨後實現它爲void calcGrade(studentType sList[], int score, int listSize)

  2. 主你使用變量highScore這沒有定義,只有定義了這樣的名稱的函數,這就是爲什麼你越來越奇怪的錯誤,指向有趣的指針ction不能轉換成int

+0

謝謝,這是有幫助的 – elpretentio