•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,506 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,649 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1248 | Replies: 5
![]() |
•
•
Join Date: Sep 2007
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
I try to use the getline function, it works in simple program and accepts the input from user but when i try to use inside the do while loop, it just escape the line and shows that there is null input. cin works fine,
case 1:
cl.append(Cb("abc",false));
cout << "text";
cout << "text1" << cl <<"&\n";
getline(cin,srch);
cl.append(Cb(srch,false));
cl.append(Cb("bbb",false));
cout << cl <<"&\n";
break;
here the cl is the instance of the object abc and Cb is the constructor defined in namespace, it accepts the first and third append but it does not take the srch variable which is of string type and display null through cl which is the operator overloaded one.
case 1:
cl.append(Cb("abc",false));
cout << "text";
cout << "text1" << cl <<"&\n";
getline(cin,srch);
cl.append(Cb(srch,false));
cl.append(Cb("bbb",false));
cout << cl <<"&\n";
break;
here the cl is the instance of the object abc and Cb is the constructor defined in namespace, it accepts the first and third append but it does not take the srch variable which is of string type and display null through cl which is the operator overloaded one.
The usual problem is trying to mix say
cin >> somevar;
with getline().
They don't play well together, since a simple cin will leave a newline on the input stream, and this is exactly what getline stops at (thus giving the illusion of it being skipped).
Exactly how to get round this has been discussed endlessly on the forum and in the snippets section, perhaps look there.
Personally, I go with reading ALL input with getline, then convert from a string in memory. Trying to mix input and conversion all in one step almost always results in a mess in all but the most trivial of programs.
cin >> somevar;
with getline().
They don't play well together, since a simple cin will leave a newline on the input stream, and this is exactly what getline stops at (thus giving the illusion of it being skipped).
Exactly how to get round this has been discussed endlessly on the forum and in the snippets section, perhaps look there.
Personally, I go with reading ALL input with getline, then convert from a string in memory. Trying to mix input and conversion all in one step almost always results in a mess in all but the most trivial of programs.
Is that .append used correctly?
http://www.fredosaurus.com/notes-cpp...er-string.html
or is that your own class definition?
http://www.fredosaurus.com/notes-cpp...er-string.html
or is that your own class definition?
Last edited by iamthwee : Sep 29th, 2007 at 4:55 pm.
... the hat of 'is this a cat in a hat?'
•
•
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation:
Rep Power: 9
Solved Threads: 163
>> The usual problem is trying to mix say ... They don't play well together, since a simple cin will leave a newline on the input stream
the solution for this is simple (assuming that you are not interested in leading whitespaces in a line):
>>> i do not have the mixing...
in which case you may be using an old compiler (and libstdc++). for example see
http://support.microsoft.com/kb/240015 in vc++6 (vc++7, vc++8 do not have these problems)
http://gcc.gnu.org/bugzilla/buglist....ontent=getline
in g++ mainly 3.2.x or earlier (these have been fixed in later versions).
the solution for this is simple (assuming that you are not interested in leading whitespaces in a line):
char ch ; string str ;
cin.get(ch) ;
getline( cin >> ws, str ) ;
// user enters: X <newline> Hello World <newline>>>> i do not have the mixing...
in which case you may be using an old compiler (and libstdc++). for example see
http://support.microsoft.com/kb/240015 in vc++6 (vc++7, vc++8 do not have these problems)
http://gcc.gnu.org/bugzilla/buglist....ontent=getline
in g++ mainly 3.2.x or earlier (these have been fixed in later versions).
Last edited by vijayan121 : Sep 30th, 2007 at 9:51 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- getline printing null characters (C++)
- Error C2660 Function does not take 1 argument (C++)
- too many arguments to function error message. (C++)
- help with creating and calling a function (C++)
- C++ Stream_Var.getline function (C++)
- Image Resize Function Error (PHP)
- getline function acts extrangly (C++)
- Function Error Message Help (C++)
Other Threads in the C++ Forum
- Previous Thread: How to output certain letters from words??
- Next Thread: Meh wats wrng???



Linear Mode