473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

string class and cin

65
when i use the code below, i never have an opportunity to enter the second string,
i know that the problem is related with null character remaining in the buffer.
but whats the exact story?
because when i use cin>>str2 instead of getline(cin,str2), there isnt any problem. why?

#include <iostream>
#include <string>

int main()
{
string str1,str2;
cout<<"enter 1st string: ";
cin>>str1;

cout<<"enter 2nd string: ";
getline(cin,str2);
//cin>>str2;

cout<<"\n1st string is: "<<str1<<endl;
cout<<"2nd string is: "<<str2<<endl;

return 0;
}
Jul 8 '07 #1
1 2214
when i use the code below, i never have an opportunity to enter the second string,
i know that the problem is related with null character remaining in the buffer.
but whats the exact story?
because when i use cin>>str2 instead of getline(cin,str2), there isnt any problem. why?

#include <iostream>
#include <string>

int main()
{
string str1,str2;
cout<<"enter 1st string: ";
cin>>str1;

cout<<"enter 2nd string: ";
getline(cin,str2);
//cin>>str2;

cout<<"\n1st string is: "<<str1<<endl;
cout<<"2nd string is: "<<str2<<endl;

return 0;
}
The problem is because the extraction operator (<<) skips all leading whitespace characters (spaces, newling character, tab character etc.), and then stops at the next whitespace character and leaves it in the istream (cin)

So when you use cin << and enter the word "hello" you when you hit the [enter] key you also put a newline character in the istream. that newline character stays there. Now if you use cin << again, it skips that newline character so there's no problem.

But the getline function simply reads all characters until it finds the newline character. So the cin << leaves a newline character ('\n') and then getline sees that and thinks it's done.

When I found this problem I wrote a function of my own that handles this, It is used exactly as getline, and uses the same parameters:

Expand|Select|Wrap|Line Numbers
  1. void getlineNew(istream& inPut, string& line)
  2. {
  3.     char ch;
  4.  
  5.     ch = inPut.peek();//checks what the next character is.
  6.     if (ch == '\n')   //if it's the newline character it
  7.         inPut.get(ch);//reads it so that getline doesn't
  8.                       //get confused by a leftover newline char
  9.  
  10.     getline(inPut, line);
  11. }//end getlineNew
This function checks what the next character is first, if it's a newline character it throws it away. Then it calls getline using the same parameters you passed to it.
Jul 8 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
23
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
3
by: Vincent Cantin | last post by:
I have a class defined by a template which needs to "say" its type to the user via string. As an example, here is the class that I want to fix : template<class T> class Container : public...
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
6
by: Sanjay Pais | last post by:
Is there anyway I can define an enum to have a value of a string or character type? for example I would like to have public enum HOW_GOOD { AWESOME = "A", GREAT= "G",
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.