0

Can anyone explain why I am having trouble with the below C++ code?

#include <iostream>
using namespace std;

class stud
{
public:    
    string name,adrs;     
    long long unsigned int  mob;
};

int main()
{
    stud s[10];
    unsigned int num;
    cout << endl << "Enter the number of students(<10): ";
    cin >> num;
    cout << endl;
    for(int i = 0; i < num; i++)
    {
        cout << endl << "Enter student " << i+1 << " name(put '.' at end and press enter): ";
        getline(cin, s[i].name);  // this line skips some data before even they are
                                  //entered and there is no error while compiling
    }
    system("CLS");
    for(int i = 0; i < num; i++)
    {
        cout << endl << " Student " << i+1 << " name is: ";
        cout << s[i].name << endl;
    }
    return 0;
}

When I try to input a string value for an object in the array as above, using getline() without any delimiter (which uses a new line by default), I don't get correct output since some other data is automatically being skipped.

But, when I use getline() as follows instead of above, it works fine, but it needs a delimiter at the end:

getline(cin, s[i].name, '.');

Please help me find a solution. I think the Enter key is pressed several times at one key press, and that's why the getline() skips some data. I'm not sure about this, though.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • 1
    Post question as paragraphs, not comments in code. Indent and format your code properly. –  Jul 03 '18 at 01:39
  • "*I think the `Enter` key is pressed several times at one key press*" - what makes you think that? That is not how the `Enter` key usually works. StackOverflow is not a debugging service. What have you done so far to troubleshoot this issue yourself? Have you even tried using a debugger yet? And why are you requiring users to type `'.'` at the end of a name? `getline()` doesn't require that by default, so what's wrong with simply letting the user type `Enter` to end a name? – Remy Lebeau Jul 03 '18 at 01:52

1 Answers1

0

before correcting you program one thing to know is that

Actually, a newline is always appended to your input when you select Enter or Return when submitting from a terminal.

cin>> doesn't remove new lines from the buffer when the user presses Enter.

This has little to do with the input you provided yourself but rather with the default behaviour std::getline() exhibits. When you provided your input for the name (std::cin >> num;), you not only submitted the following characters, but also an implicit newline was appended to the stream, getline() mistakes this for user input along with enter.

It is recommended to use cin.ignore() to get rid of those extra characters after using cin>>(whatever) if you are going to use getline(cin,any string) later. Edit this part of your code:

    stud s[10];
    unsigned int num;
    cout << endl << "Enter the number of students(<10): ";
    cin >> num;
    cout << endl;
    cin.ignore();//just add this line in your program after getting num value through cin
    //fflush(stdin);
    //cin.sync();
    //getchar();
    for(int i = 0; i < num; i++)
    {

        cout<<endl<< "Enter student " << i+1 << " name(put '.' at end and press enter): ";
        getline(cin,s[i].name);
    }
    system("CLS");

you can use and may be tempted to use fflush(stdin) also but it is not recommended as it has undefined behaviour, as According to the standard, fflush can only be used with output buffers, and obviously stdin isn't one. about cin.sync():

using “cin.sync()” after the “cin” statement discards all that is left in buffer. Though “cin.sync()” does not work in all implementations (According to C++11 and above standards).

you can also use getchar() to get the newline caused by Enter

mss
  • 1,228
  • 1
  • 7
  • 16
  • Your answer is very helpful sir and cin.ignore() worked but as a beginner to c++ i never heard about ignore() sync() functions and it's not mentioned in any book or video tutorial while the teach cin. Could you refer me to some advanced sites/book where i could find a "DEEP DIVE " on everything i learn..It would be very helpful sir. – Electro Voyager Jul 06 '18 at 01:54
  • @ElectroVoyager: visit this link :https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – mss Jul 08 '18 at 03:30
  • @ElectroVoyager: about the website, I think currently the best website for beginners is geeksforgeeks , link: https://www.geeksforgeeks.org/c-plus-plus/ – mss Jul 08 '18 at 03:32