Does anyone know why you have to press enter twice when using cin.getline. e.g.
const int MAX_BUFFER=255;
cout << "Please enter something: ";
cout << "You entered " << Buffer;
If you get rid of getchar() that won't be a problem.
The ony reason why I am using getchar() is to hold the screen for the user. It is still neccesary for me to press the enter twice when getting data from the keyborad buffer; using cin.getline
the getchar() at the bottom is causing the double enter
With the getchar there i would have to press enter 3 times for the program to quit. So if I never put it in i would still have to press enter twice for the program to quit.
As written I don't see a problem with your program. The call to cin.getline() work fine. If not please post your exact! program. There can be problems when using getiline(). The problem usually happens when mixing use of >> and getline() or one of the other istream methods and getline() in the same program. This problem stems from how the various istream methods deal with input. >> and other istream methods OTHER THAN getline() do not remove the terminating char from the istream buffer because they just ignore it. getline() doesn't ignore it however, and that can lead to problems.
If you want to get detailed:
User input is put into a buffer before it is stored in a variable or placed on the screen. When you use the >> operator to place a variable in the buffer it ignores any leading whitespace, (whitespace includes stuff like a space between words, a new line character, a tab character, and so on) and places all non white space input up to the next whitespace into the variable. It stops at the next whitespace and leaves the next white space intact. That can be very useful. For example if the user types in spaceBar spaceBar tab 123 enterKey, and the input is accepted by >>, the >> will ignore everything before 123, put 123 in the variable, and stop at the enterKey (which is really newline char to the computer) and leave the newline char in the buffer. If the user is then asked to input a single word, the newline char remaining in the buffer is ignored, and everything up to the next whitespace is entered into the variable.
getline(), on the other hand, doesn't ignore leading white space. On the one hand this is good, because it allows you enter a phrase or a sentence or a whole novel at a time and store it all in the same variable. On the other hand if the first thing getline() sees in the buffer is a leftover newline char from a previous call to >> and if the terminating char for this call to getline() is the newline char (which it is by default, although you can change it if you wish), then getline() sees the first newline char and stops input before anything else can happen. It turns out that getline() also differs from other istream methods in that it disposes of the terminating char rather than leaving it in the buffer. Therefore if you were to place a second call to getline() right away the first call will have cleared the newline char from the buffer, the second call will see the input and away you go. However, this is not the recommended approach.
The recommeded approach, or at least the one I've seen in multiple places, is to call the ignore() method of istream classes after ever call to >> IF you are going to be using getline() in the program and/or IF this is a piece of software that will be potentially modified by someone else in the future.
char ch ;
cout << "enter a single character" << endl;
cin >> ch;
cout << "enter a phrase" << endl;
There are a bunch of other issues dealing with specifics. Do you ignore just 1 char or up to 80 char or untile the terminating char for ignore() is encountered, or up to 256 char our until the terminating char is identified, or what?
cin.getline(buffer, 79, '~'); means accept up to 79 char and place them in the char called buffer unless you find a ~ first. If you find the ~ first discard the ~. Of course if buffer has been declared like this:
then the call to getline(() above will get you into trouble, too.
Is it any wonder that C++ is felt to be a difficult language to learn??