0

Actually i'm trying to getting multiple input from user but after i get the full name and address input my code is automatically skipping other two inputs of amount and account number i'm really confused at this any help will be appreciated!

MInfo i;
    int number;
    cout << "Enter Your Full Name: " << endl;
    cin.getline(i.name,50);
    cin.ignore();
    cout << "Enter Your Address: " << endl;
    cin.getline(i.address,100);
    cin.ignore();
    cout << "Enter Amount You Want to Deposit: " << std::endl;
    cin>> i.balance;
    cout<<"Please Enter A Unique Account Number Between 10000 and 10099"<<endl;
    cin>> number;
  • 1
    You only have to call `cin.ignore` between a `>>` and a `getline`, and then only if there is a newline inbetween. Get rid of the others. – Botje Nov 16 '20 at 15:49
  • 3
    Actually you never need to call `cin.ignore`. All you need to do is always call `getline`. That's its job: to read a single line of input, full stop. It is never necessary too `cin.ignore` or anything else, `getline` is perfectly capable of accomplishing everything by itself, by default. Then, if one needs to validate the read string to be an integer, that can be a follow-up, separate action. Attempting to simultaneously read a line of input and validate it as a numeric type, using `>>` will always end in tears. This is not what `>>` is for. – Sam Varshavchik Nov 16 '20 at 15:51
  • @Botje i tried that but still it doesn't work after i insert name and address other two input gets skipped automatically – Ruchit Patel Nov 16 '20 at 15:54
  • Most likely this a dupe of [this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) but without a [mre] I can't say 100%. – NathanOliver Nov 16 '20 at 15:59
  • Seems fine to me with no ignores needed since `getline` is used before `>>`: https://ideone.com/Kx91RJ – Retired Ninja Nov 16 '20 at 16:02
  • @RetiredNinja if i remove ignores then actually after i input name address get's skipped and then it prints enter adress and number all at once! this is really confusing – Ruchit Patel Nov 16 '20 at 16:17
  • @RetiredNinja what i want is first ask user to input name then address and then number i don't want that cout to print all at once – Ruchit Patel Nov 16 '20 at 16:19
  • @RuchitPatel I am not sure what you are saying. The way the code is displayed in the online IDE and what you would see if you ran it in a terminal are different. You don't see the input in the online IDE but it is being processed line by line just the same. You don't need ignore at all, only if you follow a `>>` with `getline` and you know there is a newline left in the buffer. That condition is not present in your code. – Retired Ninja Nov 16 '20 at 16:22

0 Answers0