0
cin.getline ()
This is the first part of my code #include <iostream> using namespace std; int main() { int n; cin >> n; cin.ignore (n); char asli [100]; cin.getline (asli,100); cout << asli; Its supposed to get a number n which Im gonna need later and then get a string the reason I used cstyle string is because I want to use cin.getline()، but when I input "zohal" its output is "al" instead of zohal I used different example it never gets the first three characters, why?!
2 Réponses
+ 7
Reason:
This👉cin.ignore(n);
Assume, input as: 4 zohal
Firstly, the computer will assign value 4 to n. As you specified n number of characters to be ignored through cin.ignore(n) predefined function, first 4 characters will be ignored (including spaces/line)
So that, _(1 space)zoh(3 characters) will be ignored.
Hence, Output will be: al
Solution:
just replace cin.ignore(n); with any of the following lines below.
• cin.ignore();
• cin.ignore(1, '\n');
• cin.ignore(1);
Hope this helps. :)
0
Nobody knows tge reason??