-1

I'm trying to take input from User in program But unable to take input at index 0

void User::call() { `cout<< "number of times :"; cin>>a; for (int i=0 ;i < a;i++) { cout<<"ENTER NAME : "; getline(cin,b); cin.ignore(); } }

`

I have attached the pictures of code and output result . Kindly check. MY CODE HERE AND OUTPUT HERE.

ppxx
  • 177
  • 1
  • 11
  • Please [edit] your question, to include the copy-pasted [mcve], and input/output used, as text (not as images). – Algirdas Preidžius Apr 15 '19 at 17:37
  • 2
    Don't post images of code here please. – πάντα ῥεῖ Apr 15 '19 at 17:37
  • Hello , i wasn't able to put the code ,that's why posting images . Kindly accept my Apology. – ppxx Apr 15 '19 at 17:39
  • 2
    @BilalKhan "_i wasn't able to put the code ,that's why posting images_" I don't believe you. I have no problem in putting code in my questions/answers, so you shouldn't have any problems as well. In addition to that: you should've, at the very least, got a message, which stated the problem with your question, before posting it. You should've considered the suggestion (if any), before trying to workaround the problem. – Algirdas Preidžius Apr 15 '19 at 17:40
  • 1
    Possible duplicate of [Using getline() in C++](https://stackoverflow.com/questions/18786575/using-getline-in-c) –  Apr 15 '19 at 17:53

1 Answers1

0

Please see this answer. I think the issue is caused by a stay newline or carriage return character.

In your code, try changing:

cin >> a to the following:

cin >> a;
cin.ignore(); // ignore the stray newline after you input a number.
  • If the question is answered in another question, consider marking it as a duplicate. – Algirdas Preidžius Apr 15 '19 at 17:41
  • @AlgirdasPreidžius I'm not sure if it's a duplicate yet, since it's a little unclear of the exact issue OP is experiencing. Particularly, if it's an issue only when they enter in `0`, or if it's an issue with all numbers. –  Apr 15 '19 at 17:43
  • @KevinEvans : Error displaying after running your suggestion in code : 'std::string' has no member named 'ignore' – ppxx Apr 15 '19 at 17:47
  • @BilalKhan Did you use the correct variable? You should be calling `.ignore()` on `cin`, not on `b`. –  Apr 15 '19 at 17:50
  • @KevinEvans , Thanks . Problem Solved within minutes – ppxx Apr 15 '19 at 17:52
  • @BilalKhan Cool. Please marked the question as solved by pressing the checkbox to the right of the answer. –  Apr 15 '19 at 17:53
  • @KevinEvans , Also tell me what text i replace with cout and cin on stack overflow while posting the code . It is showing red line under cin and cout of my code while posting – ppxx Apr 15 '19 at 17:53
  • @BilalKhan I'm not sure what your issue is when posting code to StackOverflow. You can post a screenshot of your error and post it to [Meta StackOverflow](https://meta.stackoverflow.com/). –  Apr 15 '19 at 17:55
  • @KevinEvans , https://stackoverflow.com/questions/55694535/how-to-fix-the-error-of-not-taking-value-at-0-position-in-loop?answertab=votes#tab-top – ppxx Apr 15 '19 at 18:17