1

I'm trying to input some characters into the array but after hitting enter , the terminal would not accept further input.

#include <bits/stdc++.h>
using namespace std ;
int main() {
    char Array[5];
    for(int i=0;i<5;++i){
        cout << "Enter :";
        cin.ignore('\n');
        cin >> Array[i];
    }
    for(auto data : Array){
        cout << data << endl;
    }
    
    return 0;
}

Any help would be appreciated.

  • I just ran this with `g++` and was surprised that the program finished without any inputs (with some time delay) and no outputs at all. I've never used `std::cin::ignore`, so i suspect that's the culprit. – Elliott Jun 16 '21 at 03:10
  • Why are you ignoring input you haven't even tried to read yet? You shouldn't be calling `ignore()` before reading, but after instead (if at all). Also, the 1st parameter of `ignore()` is the number of characters to ignore. `'\n'` has a numeric value of 10, so you are ignoring 10 characters each time, not ignoring until a line break is reached – Remy Lebeau Jun 16 '21 at 03:12
  • 1
    Also, see [Why should I not #include ?](https://stackoverflow.com/questions/31816095/) – Remy Lebeau Jun 16 '21 at 03:15
  • 1
    see also [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Jun 16 '21 at 03:25
  • Sure sir I will check out the links but I just can't comprehend why con.ignore() doesn't work as I meant to clear the buffer during second iteration of the loop so I can get rid of the '\n' but it's weird that it doesn't work and it's weird that it does work if you do without cin.ignore() as the '\n' is still in the input buffer? – WoohooAnonymousAh Jun 16 '21 at 03:47
  • Alright sir @Remy Lebeau I changed the cin.ignore('\n') to con.ignore(1) and included it after the input and it indeed worked! – WoohooAnonymousAh Jun 16 '21 at 04:01
  • @ShahainManujith the correct way to ignore up to the next line break is: `#include cin.ignore(numeric_limits::max(), '\n');` – Remy Lebeau Jun 16 '21 at 04:16
  • True @RemyLebeau now I got it that it takes the ASCII value of the new line character if it is the first argument, again thank you very much as well :) – WoohooAnonymousAh Jun 16 '21 at 04:17

1 Answers1

4

You don't need the cin.ignore() here:

#include <iostream>

int main() {
    char Array[5];
    for(int i=0;i<5;++i){
        std::cout << "Enter: ";
        // No use in cin.ignore() here 
        std::cin >> Array[i];
    }
    for(auto data : Array){
        std::cout << data << std::endl;
    }
    
    return 0;
}

Example:

Enter: 3
Enter: 4
Enter: 5
Enter: 6
Enter: 7
3
4
5
6
7

Main thing to note for you is the fact that cin object will skip any leading white space on itself. So, you don't have to worry that cin will read in '\n' on a second iteration, thus, terminating and skipping the input, it won't read it in.

While the ignore('<character>') will extract specified character, know as delimiting character, from the input sequence and discard it. In your case, the character is \n. The cin function stops extracting characters from the stream as soon as an extracted character compares equal to this. So, no way to pass it \n, no way to terminate input normally (you can still pass it EOF).

More on the proper use of the cin.ignore(): When and why do I need to use cin.ignore() in C++.

I also want to include this in the answer: Why should I not #include <bits/stdc++.h>?.

rawrex
  • 4,044
  • 2
  • 8
  • 24
  • Sir, I included the cin.ignore() since in the second iteration of the loop the '\n' is still present in the input buffer. But it doesn't seem to work and why is that? – WoohooAnonymousAh Jun 16 '21 at 03:43
  • @itsShahain the `cin` reads up to (but not including) the first whitespace character. Have you tried to execute the code in the answer? – rawrex Jun 16 '21 at 03:49
  • @rawrex Yes sir it works! But why doesn't cin.ignore() remove the '\n' during second iteration? – WoohooAnonymousAh Jun 16 '21 at 03:53
  • @itsShahain `cin.ignore('\n')` simply won't let you to terminate input normally on the first iteration. Also, check the edit, added answer to your concern about `cin` having '\n' sitting in it. – rawrex Jun 16 '21 at 03:56
  • @itsShahain am I being clear? Feel free to ask questions on the topic. – rawrex Jun 16 '21 at 04:04
  • @rawrex Yes sir it finally works and I fully understand it now! Thank you and may God Bless You! – WoohooAnonymousAh Jun 16 '21 at 04:06
  • @ShahainManujith you're welcome! Please don't forget to mark an answer as accepted if you consider one as such. – rawrex Jun 16 '21 at 04:11
  • 1
    @rawrex "*the `cin` reads up to (but not including) the first whitespace character*" - more accurately, it is `operator>>` that behaves that way, not `cin` itself. – Remy Lebeau Jun 16 '21 at 04:19
  • @RemyLebeau true! Thanks! – rawrex Jun 16 '21 at 04:30
  • @RemyLebeau. What If instead of cin I used scanf, and spoiler alert I did and it simply did not work. Scanf doesn't skip whitespace isn't it in the 2nd iteration and so on when the new line char is in the buffer. – WoohooAnonymousAh Jun 16 '21 at 07:22
  • @ShahainManujith yes, `scnaf` reads until whitespace. Check this [thread](https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) on the `scanf`, it is not recommended to use without a string reason. – rawrex Jun 16 '21 at 07:32