Why does the following code not work?
Code:char aName[20];
int number;
cout << "Please enter an integer: ";
cin >> number;
cout << "Enter a sentence: " << endl;
cin.get(aName, 20, '\n');
Printable View
Why does the following code not work?
Code:char aName[20];
int number;
cout << "Please enter an integer: ";
cin >> number;
cout << "Enter a sentence: " << endl;
cin.get(aName, 20, '\n');
You might want to read
skipws - C++ Reference
http://www.cplusplus.com/reference/i...nipulators/ws/
I would guess you are not skipping past the newline on second input.
I am a OK to good C programming; but, just starting to learn C++.
Tim S.
After you enter your integer, there will be a newline in the input buffer that will be consumed by cin.get, I presume.
Also, I would change
char aName[20];
cin.get(aName, 20, '\n');
to
std::string aName;
std::getline(std::cin, aName);
Got it thanks.
You are entering a character after having just entered a number.
cin.ignore(20,'\n'); should work.
You ignore 20 non newline characters or the newline character (whichever comes first) in the buffer so that the cin for the second prompt will work properly.
Why worry about the next 20 characters when you could just call ignore()?
For some reason it only takes up to 9 characters. So for example if I inputed "abcdefghijkl" it only gives me "abcdefghi"
Code:#include <iostream>
using namespace std;
int main()
{
char sentence[20];
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Enter a sentence: ";
cin.ignore(20, '\n');
cin.get(sentence, '\n');
cout << "num is: " << num << "\nsentence is: " << sentence << endl;
return 0;
}
Well \n is a number too
cout << int('\n') - 1 << endl;
Remember how
cin.get(sentence, '\n');
works? You asked for the first nine bytes and you got it.
I said why not ignore() in reference to what m&m posted because in this case, the 20 is just a magic number that contributes precisely zero in any situation.
I'm slowly learning
Something still confusing me is how come sentence holds a max of 19 characters?
Is it because the null character is at sentence[19] and cout can never print the null character?Code:#include <iostream>
using namespace std;
int main()
{
//cout << int('\n') - 1 << endl;
char sentence[20];
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Enter a sentence: ";
cin.ignore();
cin.get(sentence, 20);
cout << "num is: " << num << "\nsentence is: " << sentence << endl;
cout << "19th element: " << sentence[19];
return 0;
}
The sentence variable holds a maximum of twenty characters. If we look at it in math terms, a set from zero to nineteen inclusive has twenty elements. A C string is defined as a sequence of characters terminated by zero. So, sentence holds nineteen characters because the last character must be a zero, but you could always store a shorter string in sentence. I hope that is clear. There is no reason why cout cannot print a zero.
Also, this means that all previous examples of get() in this thread have terrible bugs.
get - C++ ReferenceQuote:
istream& get (char* s, streamsize n );
Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. Use getline if you want this character to be extracted (and discarded).
The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.
At a minimum then:
But the string class has already been mentioned, and there is no reason not to use it right now.Code:#include <iostream>
int main()
{
char sentence[20];
std::cout << "enter string:\n";
std::cin.get(sentence, sizeof(sentence) - 1);
std::cout << sentence << '\n';
}
Why doesn't this output the null character '\n'? I mean '\0'
I thought when the c-string is created it automatically adds a null character, after the last character, to signify the end.Code:char sentence[] = "abcde";
cout << sentence[5];
Since when is '\n' a null character? The null character, '\0', is not actually part of the string. It signals the end of the string. It is never actually outputted; thus you will never see it.
Sorry I meant '\0' not '\n'. I thought I was getting ripped of the last character for the '\0'? For example
will only let me store 19 characters in sentence. I thought that's because the last element (sentence[19]) is used for the null character ('\0')?Code:char sentence[20];
cin.get(sentence, 20);
Yes, of course it is, since you're using a C-style string.
Actually it will let you store 20 characters in a 21 character string. I already posted a blurb on how this works.
cin.get(sentence, 19);
Means that a maximum of 18 characters will be written to string and a zero automatically placed at the 19th place, if you use up the whole string. Please pay attention to the small stuff.
I still recommend std::string.
All right I get it now. Aren't I right that the null character is never printed?
Nothing gets printed.Code:cout << '\0' << endl;
Wait a minute - the null character gets printed the same as a space.