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>?.