#include <iostream>
#include <ios>
#include <limits>
using namespace std;
int main() {
int x;
char str[80];
cout << "Enter a number and a string:\n";
cin >> x;
cin.ignore('\n');
cin.getline(str,80);
cout << "You have entered:\n";
cout << x << endl;
cout << str << endl;
}
I am facing an issue with the ignore function where using
cin.ignore('\n');
results in an output where I have to press Enter multiple times to print statements (for less number of characters) or else it cuts some of the initial characters from my string in the output (in case of more number of characters). Like these:
while using cin.ignore();
results in the correct output.
Isn't it the same to use either of these as '\n'
discards new line character and an ()
empty bracket also dicards the last character (or new line character as here).
Also, when should we use cin.getline()
and getline(cin, )
?