2013-02-06 39 views
-2

我的代碼:串標超出範圍C++與fstream的

#include <iomanip> 
#include <iostream> 
#include <string> 
#include <cmath> 
#include <cstdlib> 
#include <fstream> 
#include <ctime> 

using namespace std; 
void insertionSort(int arrtosort[], int size); 
const int SIZE = 10; 
int main() { 
    ofstream out; 
    ifstream ingrades; 
    unsigned seed = time(0); 
    char doAgain = ' '; 
    int id = 0; 
    string grade = " "; 
    int choice = 0; 
    int fileid[SIZE]; 
    int numgrade[SIZE]; 
    int sum = 0; 
    int counter = 0; 
    int average = 0; 
    int idx = 0; 
    string letterGrade = " "; 
    srand(seed); 
    out.open("dataout.txt"); 

    while(counter <= 10) { 
     out << (10000 + rand() % 99999); 
     out << " "; 
     out << (-1 + rand() % 10) << endl; 
     //out<<" "; 
     counter++; 
    } 
    out.close(); 

    ingrades.open("dataout.txt"); 
    for(idx = 0; idx < SIZE; idx++) { 
     ingrades >> fileid[idx] >> numgrade[idx]; 
     sum = sum + numgrade[idx]; 
     counter++; 
    } 

    ingrades.close(); 
    average = sum/counter; 
    out.open("dataout.txt"); 

    for(idx = 0; idx < SIZE; idx++) { 
     if(numgrade[idx] == average || numgrade[idx] == average + 1) { 
      out << fileid[idx] << " " << numgrade[idx] << " B"; 
      out << endl; 
     } else if(numgrade[idx] == average + 2 || numgrade[idx] == average + 3) { 
      out << fileid[idx] << " " << numgrade[idx] << " A-"; 
      out << endl; 
     } else if(numgrade[idx] == average + 4 || numgrade[idx] >= 7 || numgrade[idx] == 10) { 
      out << fileid[idx] << " " << numgrade[idx] << " A"; 
      out << endl; 
     } else if(numgrade[idx] == average - 1) { 
      out << fileid[idx] << " " << numgrade[idx] << " C"; 
      out << endl; 
     } else if(numgrade[idx] == average - 2) { 
      out << fileid[idx] << " " << numgrade[idx] << " F"; 
      out << endl; 
     } else if (numgrade[idx] == -1) { 
      out << fileid[idx] << " " << numgrade[idx] << " N/A"; 
      out << endl; 
     } 
    } 
    out.close(); 

    ingrades.open("dataout.txt"); 
    for(idx = 0; idx < SIZE; idx++) { 
     ingrades >> fileid[idx] >> numgrade[idx]; 
    } 
    ingrades.close(); 

    do { 
     cout << "\n" << right << setw(35) << "Teacher's Menu" << right << endl; 
     cout << "---------------------------------------------------------" << endl; 
     cout << "1) Sort the student list by id, and write it to your file" << endl; 
     cout << "2) Search for a student by id and display the id," << 
      " numeric and letter grade to\n the console" << endl; 
     cout << "3) Search for students with a particular letter grade and display the results\n to the console" << endl; 
     cout << "4) Sort the student list by numeric grade and write it out to a file" << endl; 
     cout << "5) Find the percentage of students for a particular letter grade" << endl; 
     cout << "\n\tPlease enter which number option you wish to use: "; 
     cin >> choice; 
     ingrades.open("dataout.txt"); 
     switch(choice) { 
     case 1: 
      break; 
     case 2: 
      ingrades.clear(); 
      cout << "Enter in Student id: "; 
      cin >> id; 
      idx = 0; 

      for (idx = 0; idx <= SIZE; idx++) { 
       ingrades >> fileid[idx] >> numgrade[idx] >> letterGrade[idx]; 
       if (fileid[idx] == id && idx <= SIZE) { 
        cout << fileid[idx] << " " << numgrade[idx] << " " << letterGrade[idx] << endl; 
        break; 
       } 
      } 

      system("pause"); 
      break; 
     case 3: 
      /* cout << "Enter in the Student letter grade: "; 
       cin >> grade; 
       for(idx = 0; idx < SIZE; idx++) { 
        if(grade == letterGrade[idx]) 
         cout << fileid[idx] << " " << numgrade[idx] << " " << letterGrade[idx] << endl; 
       } 
       break; 
       */ 
     case 4: 
      break; 
     case 5: 
      break; 
     default: 
      cout << "Incorrect option" << endl; 
     } 
     cout << "Would you like to go back to the main menu y or n: " << endl; 
     cin >> doAgain; 
    } while(doAgain == 'y' || doAgain == 'Y'); 
    ingrades.close(); 
    //system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

void insertionSort(int arrtosort[], int size) { 
    int temp = arrtosort[0]; 
    for(int i = 1; i <= size; i++) { 
     temp = arrtosort[i]; 
     int j = 0; 
     for(j = i; j > 0; j--) 
      if(temp < arrtosort[j - 1]) 
       arrtosort[j] = arrtosort[j - 1]; 
      else break; 
     arrtosort[j] = temp; 
    } 
} 

這是一所學校的項目,但我失敗了類,​​但我決心要完成這個項目,並繼續學習幫助真正需要的,我覺得像我撞到一堵磚牆。

我試圖讀取它的工作文本的ID它也出現了我會然後我得到那個錯誤我想了解爲什麼這不工作,以及如何我可以繼續執行程序的其餘部分並非常感謝所有幫助,也可以使用幫助來應用我的自動排序功能老師給我們的循環,但不知道它如何完全工作或如何正確應用它。

我有從線127開始和最多給我像字符串的子串出現了錯誤鈴響了,比較困難的字符串的情況下麻煩3

+2

1)在哪一行,你得到的錯誤? 2)你的問題沒有說錯誤是什麼(雖然標題確實)。 3)'idx <= SIZE'可能應該是'idx Dukeling

回答

0

所有lettergrade首先不是數組,因此超出範圍故障。正確的代碼是這樣的:

 for (idx = 0; idx < SIZE; idx++) { 
      ingrades >> fileid[idx] >> numgrade[idx] >> letterGrade; 
      if (fileid[idx] == id && idx <= SIZE) { 
       cout << fileid[idx] << " " << numgrade[idx] << " " << letterGrade << endl; 
       break; 
      } 
     } 

插入排序也有一些錯誤我的版本看起來像這樣。

void insertionSort(int arrtosort[], int size) { 
    int temp = arrtosort[0]; 
    for(int i = 1; i < size; i++) { 
     temp = arrtosort[i]; 
     int j; 
     for(j = i; j > 0; j--) 
      if(temp < arrtosort[j - 1]) 
      { 
       arrtosort[j] = arrtosort[j - 1]; 
       arrtosort[j-1] = temp; 
      } 
      else break; 
    } 
} 

試着看看你舊的代碼,看看當id不小於它的前任時會發生什麼。正如@Dukeling所提到的,所有與SIZE的比較應該少一些,也不能少一樣。

實現插入排序的一種方法可能是這樣的;

case 4: 
     insertionSort(fileid,SIZE); 
     //fileid is now sorted 
     for(int i=0; i< SIZE;++i) 
     { 
      ingrades.open("dataout.txt"); 
      for (idx = 0; idx < SIZE; idx++) 
      { 
       int id, t_grade; 
       ingrades >> id >> t_grade >> letterGrade; 
       //find the ith ID 
       if (fileid[i] == id) 
       { 
        //and print it; 
        cout << id << " " << t_grade << " " << letterGrade << endl; 
        break; 
       } 
      } 
      ingrades.close(); 
     } 
     break; 

這種打印的識別碼與屬於檔次爲了

+0

首先,我想感謝你們兩位,因爲在4個論壇之後,這是唯一回答我的地方,我真的很感激。其次,你可以解釋爲什麼我的數組for the –

+0

信*級不工作,你如何工作的功能,因爲我有點困惑它 –

+0

我實際上看到我的錯字我的lettergrade,如果我已經聲明它是字符串letterGrade [SIZE ]是否允許我將它用作我的例子的數組? –