473,387 Members | 3,750 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,387 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 2212
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.