0
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

struct student {
    string name;
    int number;
    double score;
};

bool compare(const student& a, const student& b)  {
    return a.score > b.score;
}

int main()
{
    // int x = 10;
    struct student stu[x];
    int i = 0;

    /** for(i = 0; i<x; i++) {
        cout<<"Please enter the name.: ";
        getline(cin, stu[i].name,'\n');
        cout<<"Please enter the student number.: ";
        cin>>stu[i].number;
        cout<<"Please enter the score.: ";
        cin>>stu[i].score;
        cin.ignore();
    } **/

    do {
        cout<<"Please enter the name.: ";
        getline(cin, stu[i].name,'\n');
        cout<<"Please enter the student number.: ";
        cin>>stu[i].number;
        cout<<"Please enter the score.: ";
        cin>>stu[i].score;
        i++;
        cin.ignore();
    } while (stu[i].name == "quit");

    sort(stu, stu+x, compare);

    cout<<"Rank"<<'\t'<<"Name"<<'\t'<<"Student Number"<<'\t'<<"Score"<<endl;
    for(int j = 0; j<x; j++) {
        cout<<j+1<<'\t'<<stu[j].name<<'\t'<<stu[j].number<<'\t\t'<<stu[j].score<<endl;
    }
}

To use the for loop, I need to know the number of the students, but in case I don't know the number, I want to use do-while loop and it has to run until I type "quit". But now it occurs an error. I guess stu[i].name == "quit" is the problem, how can I fix it?

273K
  • 29,503
  • 10
  • 41
  • 64
Yerim Kang
  • 201
  • 1
  • 4
  • 11
  • 1
    You probably want to run `while (stu[i].name != "quit");` – NadavS May 02 '20 at 08:12
  • 1
    Please describe the actual error. A compiler error message? Some unexpected runtime behaviour? Also, you never check if your reads actually succeed - you might be getting your stream into a bad state somehow, and getting stuck in an infinite loop of failed reads. – BoBTFish May 02 '20 at 08:15

3 Answers3

2

I suggest the following to make your code safer and use modern C++.

  • read all data into a temporary variable, so you don't end up with a student namend "quit".
  • check if each reading was successfull, if not, just exit the loop.
  • use std::vector and push_back each student instead of relying on a fixed size array.

std::vector<student> stu;

do {
    student input;

    std::cout << "Please enter the name.: ";
    if (!std::getline(std::cin, input.name, '\n') || input.name == "quit") {
        break;
    }

    std::cout << "Please enter the student number.: ";

    if (!(std::cin >> input.number)) {
        break;
    }

    std::cout << "Please enter the score.: ";

    if (!(std::cin >> input.score)) {
        break;
    }

    // At this point all reading was successfull
    stu.push_back(input);

    std::cin.ignore();
} while (true); // Exit conditions are covered inside the loop
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
1

First, code is not compiling. You are you using variable x while it is commented. Un-comment it & code will compile.

Second, to make the code quit when name is quit, need to change it to:

while (stu[i-1].name != "quit")

Notice the i-1 instead of i, and != instead of ==

Third, I guess you don't need want to print on last for loop the name "quit" - so need to print up to i-1

Good Luck!

MK3
  • 56
  • 8
0

you can use the break. run the loop as infinite and check is name is quit then break the loop please refer the fallowing code

   while(i< N){
            cout<<"Please enter the name.: ";
            getline(cin, stu[i].name,'\n');
            if (stu[i].name == "quit")
                  break;
            cout<<"Please enter the student number.: ";
            cin>>stu[i].number;
            cout<<"Please enter the score.: ";
        cin>>stu[i].score;
        i++;
        cin.ignore();
    }