I used cin.getline after cin.ignore() but I am getting an error saying unassigned int... Not sure what to do or what is wrong. Any suggestions?
Here is my code:
using namespace std;
cout << " Provide a phrase, up to 30 characters with spaces > " << endl;
cout << " The phrase is: " << phras << endl;
cout << endl;
The problem is that
isn't large enough. If a C string is to hold up to
N characters, it needs to be declared
char letter[N+1] to allow room for the null terminator character. So if the user is going to type a single character, it needs to be:
As a result, you're getting undefined behavior when
cin >> letter writes 2 characters into the array that only has room for 1.
Similarly, if the user is allowed to type a 10-letter word, it should be:
and it should be: