0

I am making a school schedule, in which the user is supposed to select an exact number of subjects they are going to type in, by inputting a number, which will be stored in subCount variable. Later on, the user is met with a loop, demanding input, where they are supposed to type in subjects one by one. The end of my program is subjects getting listed in exact order.

However, my schedule is not tied to specifically any language and for example, some of my language’s subjects’ names are divided in two words (English – Anglų kalba). I know that space works as a blank, after which all remaining contents are accordingly placed in another variable. I have tried using getline(cin, sub) too and that did not work out. Here is my output’s example:

Enter the subject count
6
Enter each subject one by one
(press 'Return' to move on)
Istorija
Anglų kalba
Etika
Fizika
Vokiečių kalba
------------------
Istorija
Anglų
kalba
Etika
Fizika
Vokiečių

Note that I was not let to type in another subject and "kalba" got placed separately.

Enter the subject count
6
Enter each subject one by one
(press 'Return' to move on)
Istorija
Anglų
Etika
Fizika
Vokiečių
Matematika
------------------
Istorija
Anglų
Etika
Fizika
Vokiečių
Matematika

Variation which does not use "kalba" (displayed correctly). How could I achieve what I want? My code:

int main() {

int subCount;
vector<string> subNames;

cout << "Enter the subject count\n";
cin >> subCount;
cout << "Enter each subject one by one\n";
cout << "(press 'Return' to move on)\n";

    for (int i = 0; i < subCount; ++i) {
            string sub;
            cin >> sub;
    subNames.push_back(sub);
}
cout << "------------------\n";
for (int i = 0; i < subCount; ++i) {
    cout << subNames[i] << '\n';
}
  • 4
    Use `std::getline` instead of `>>`. – Olaf Dietsche May 31 '21 at 19:47
  • You say you already tried using getline.... exactly what didn't work out? – Ben Voigt May 31 '21 at 19:51
  • @BenVoigt Solved this problem by changing the conditions of both loops to `i < subCount+1`, guess it has something to do with array indexes starting at 0. –  May 31 '21 at 20:02
  • I'm guessing you read a blank line as the first entry, as a consequence of switching from `>>` to `getline` in the middle of your data. Please see https://stackoverflow.com/q/21567291/103167 – Ben Voigt May 31 '21 at 20:04

1 Answers1

0

getline() function considers newline or \n character as the delimitation character and the ENTER (new line) is valid input for this function.

You need to keep a mechanism to refrain the extra newline from being consumed by this getline function.

This could be a working solution-

#include<iostream>
#include<vector>

using namespace std;

int main() {

    int subCount;
    string dummyline;
    vector<string> subNames;

    cout << "Enter the subject count\n";
    cin >> subCount;

    // consume blank line
    getline(cin, dummyline);

    cout << "Enter each subject one by one\n";
    cout << "(press 'Return' to move on)\n";

    for (int i = 0; i < subCount; ++i) {
        string sub; 
        getline(cin,sub);
        subNames.push_back(sub); 
    }
    
    cout << "------------------\n";

    for (int i = 0; i < subCount; ++i) {
        cout << subNames[i] << '\n';
    }
}

For more context see - How to use getline() in C++ when there are blank lines in input

bijoy26
  • 133
  • 9