ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Originally posted by blizunt7 hey all i cant believe i cant get this working:
<code>
char test[80];
cout << "enter a Full Sentence: ";
cin.getline(test,80);
<code>
the getline DOES NOT WORK???? , and program execution just continues with the next line of code after getline.
i have also tried:
cin.read(test, 80);
but this required that i type exactly 80 chars before input stream exits.
WHat am i doin wrong?? soo stupid i know
Josh
It would help if you posted a complete program. The following code works for me:
Code:
#include <iostream>
int main() {
using namespace std;
for (int i=0; i < 3; ++i) {
char test[80];
cout << "enter a Full Sentence: ";
cin.getline(test,80);
cout << test << endl;
}
}
Of course if you type more than 80 characters, this will give you a buffer overrun and a big security hole, so you're better off using a C++ string object.
Which version of GCC/c++ are you using? In case you don't know, GCC-2.96 (http://gcc.gnu.org/gcc-2.96.html) was released by Red Hat, is not a GNU project, has lots of iostream-related bugs, and isn't even a C++ compiler, so I would not be very surprised if it didn't compile that code properly.
I can see nothing wrong with this snippet of code. Thus it is likely that the problem lies elsewhere in your program.
As previously stated, please post a complete program that fails, and the version of the C++ compiler that you're using.
The only other idea that springs to mind is that you might be using ctrl+D (EOF) to end the input stream after the first input; this would close standard input and allow nothing further to be read from it.
My guess is that your problem lies with using a cin >> something before doing the cin.getline. cin >> blah generally leaves the line feed in the buffer, so at the point that you use a cin.getline() it immediately reads that line feed and returns.
You could try using cin.getline() twice to clear the buffer, or use a cin.ignore(1); // Possibly need 2 for Windows as there is likely a carriage-return/linefeed combo of characters.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.