-2

私のコード:

#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 を読み取ろうとしていますが、4 番目に機能しすぎて、そのエラーが発生します。これが機能しない理由と、プログラムの残りの部分をどのように進めることができるかを理解したいのですが、すべてのヘルプはまた、私の自動ソート機能を適用する際に助けを借りることができて非常に感謝しています.

私が抱えている問題は、127行目以降から始まり、文字列の部分文字列が範囲外になり、ケース3で文字列を比較するのが難しいなどのエラーが発生します

4

1 に答える 1

0

まず、レターグレードは配列ではないため、範囲外の障害が発生します。正しいコードは次のようになります。

        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 とのすべての比較は、より小さくなければならず、等しくなくてはなりません。

挿入ソートを実装する 1 つの方法は次のようになります。

    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;

これは、IDと所属グレードを順番に出力します

于 2013-02-06T09:40:31.327 に答える