Yerim Kang
Yerim Kang

Reputation: 311

How do I use getline in do-while loop?

#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?

Upvotes: 0

Views: 459

Answers (3)

sandeep kumar
sandeep kumar

Reputation: 39

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();
    } 

Upvotes: 0

Lukas-T
Lukas-T

Reputation: 11340

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

Upvotes: 2

MK3
MK3

Reputation: 56

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!

Upvotes: 1

Related Questions