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.
There are few things to consider:
1. I think you mixed up variable names 'in' and 'input'
2. When using "cin.getline( in, 256 )" how do you know how many char's are really there? Default delimeter '\n' is not written into the the variable. Might be a problem if first input of 'length' differs from number of char's in 'input[]'
3. If default string is used it ends with '\0', custom string ends with nothing.
the problem im having is that when the code reaches getline it does nothing and is skipped over. i had the delimeter '\n' in before i just took it out when i was messing aroudn tryign to get it to work
I am not sure, but try not to use name 'input' or 'in' - those might be dangerous words. Call your inptut buffer, let say, buf[256], or anything you prefer which can't be a name of some standard method or function.
I don't think that in or input are names of any standard C(++) function.
The first problem is when you do
Code:
length = cin.get();
it read 1 character, but only returns after the user presses enter. This means when you call getline it just reads the newline character from before and puts an empty string into in. This is actually similar to the issue in this thread: http://www.linuxquestions.org/questi...orithm-603752/
To take care of this you can use itream's ignore method.
The second problem is that you are reading into the in buffer and displaying it (even when it is uninitialized which could cause crashes!), but you are actually using the string input as data to your functions.
I suggest you get rid of in altogether, since you can read a line directly into a string like this:
Code:
getline(cin, input);
this also takes care of lines longer than 256 bytes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.