why getline ignoring first character of input while using in loop?

Bilal Hameed
do
{
 system("cls");
        cout<<"Enter Item Name:"<<endl;
        cin.ignore();
        cin.getline(item_name,size);
        cout<<item_name<<endl;
        cout<<"Enter Item Price:"<<endl;
        cin>>item_price;
cout<<" Do You Want To Add More Item..?\nPress Y/N."<<endl;
char c;
c=getche();
}while(c=='y'||c=='Y');

this is the pace of code , actually i am using this in my project, i cannot remove cin.ignore because i have to get input from user unless user press any other character rather then y

R Sahu

You know the answer to your question in the title. The call

cin.ignore();

reads the next character from cin and discards it.

The real question that you are grappling with, I think, is: how do you terminate the loop?

That's simple enough. Don't compare just one character. Compare the entire line.

do
{
   system("cls");
   cout << "Enter Item Name:"<<endl;
   cin.getline(item_name, size);
   cout << item_name << endl;
   cout << "Enter Item Price:" << endl;
   cin >> item_price;

   cout << "Do You Want To Add More Item..?\nPress Y/N." << endl;
   std::string ans;
   getline(cin, ans);

} while (ans == "y" || ans == "Y");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java input buffer and do-while loop behavior (why does it check first character 3 times?)

From Dev

Why does a Java while loop that asks for user input using scanner skip the first input

From Dev

Getline always ignoring first word of input c++

From Dev

jQuery .val() ignoring first character of input

From Dev

getline truncating first character

From Dev

Thread ignoring first input when using getch

From Dev

Java - losing first user input using a while loop

From Dev

Populate a listbox using a loop while ignoring blanks

From Dev

Bash: While loop ignoring IFS on first line when inside loop

From Dev

Why does ignore() before getline() take one less character input?

From Dev

Taking user input with getline() and comparing to another string in a while loop

From Dev

Using scanf for character input, but the do-while loop wont stop at the null character

From Dev

exiting out of a loop using getline when you encounter a newline character

From Dev

C++ Why does getline() end the while loop?

From Dev

c - while loop keeps ignoring scanf after bad input

From Dev

input character inside a while loop with validation

From Dev

While loop ignoring parameters

From Dev

C++ Why is my first data input with getline(cin, array[x]) is on the sameline and broken while my 2nd data input is fine?

From Dev

While Loop looping only once when using getline from file

From Dev

Validate the input using a while loop

From Dev

Regex ignoring first character if it's a $

From Dev

Regex for ignoring first and last character

From Dev

While loop statement sums all but the first input?

From Dev

Input using getline(cin,n); is not printing first input and i am not using cin>> to take input anywhere

From Dev

why does this for loop in C++ using getline() work

From Dev

getline() function is not working in while loop

From Dev

Only using the first character of an input to char variable

From Dev

When using a nested while loop with getline, the inner while loop won't run more than once

From Dev

why loop is skipping input in first iteration?

Related Related

  1. 1

    Java input buffer and do-while loop behavior (why does it check first character 3 times?)

  2. 2

    Why does a Java while loop that asks for user input using scanner skip the first input

  3. 3

    Getline always ignoring first word of input c++

  4. 4

    jQuery .val() ignoring first character of input

  5. 5

    getline truncating first character

  6. 6

    Thread ignoring first input when using getch

  7. 7

    Java - losing first user input using a while loop

  8. 8

    Populate a listbox using a loop while ignoring blanks

  9. 9

    Bash: While loop ignoring IFS on first line when inside loop

  10. 10

    Why does ignore() before getline() take one less character input?

  11. 11

    Taking user input with getline() and comparing to another string in a while loop

  12. 12

    Using scanf for character input, but the do-while loop wont stop at the null character

  13. 13

    exiting out of a loop using getline when you encounter a newline character

  14. 14

    C++ Why does getline() end the while loop?

  15. 15

    c - while loop keeps ignoring scanf after bad input

  16. 16

    input character inside a while loop with validation

  17. 17

    While loop ignoring parameters

  18. 18

    C++ Why is my first data input with getline(cin, array[x]) is on the sameline and broken while my 2nd data input is fine?

  19. 19

    While Loop looping only once when using getline from file

  20. 20

    Validate the input using a while loop

  21. 21

    Regex ignoring first character if it's a $

  22. 22

    Regex for ignoring first and last character

  23. 23

    While loop statement sums all but the first input?

  24. 24

    Input using getline(cin,n); is not printing first input and i am not using cin>> to take input anywhere

  25. 25

    why does this for loop in C++ using getline() work

  26. 26

    getline() function is not working in while loop

  27. 27

    Only using the first character of an input to char variable

  28. 28

    When using a nested while loop with getline, the inner while loop won't run more than once

  29. 29

    why loop is skipping input in first iteration?

HotTag

Archive