Solved

C++ std cin

Posted on 2007-04-11
31
7,375 Views
Last Modified: 2013-12-14
Hi,
i am trying to read in a string using cin but it seems that cin will only read till a whitespace is detected and stop reading the rest of the string.

e.g.

string a ="";
string b="";
string c="";

cout << "Enter a:";
cin >> a
cout << "Enter b:";;
cin >>b;
cout << "Enter c:";
cin >>c;

the output

Enter a: Hello how are you

it will assign hello to variable a, how to variable b and are to variable c which i do not want. i want to assign a = "Hello how are you"; instead
0
Comment
Question by:jaxrpc
  • 14
  • 10
  • 5
  • +2
31 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Use std::getline function instead.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
The std::getline function will take a string, and takes a deliminator.

Example:

std::getline(a, cin, '\n');

The above code should use the next line character as the deliminator.
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
Did you tried it?
      char line[100];
      cout << " Type a line" << endl;
      cin.getline( line, 100, '\n' );
      cout << line;
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
You was quicker as I :)
0
 

Author Comment

by:jaxrpc
Comment Utility
hi must it be char?

can i do it like?

string line;

cin.getline(line,100,'\n');

0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>cin.getline( line, 100, '\n' );
FYI:
Notice that cin member function can not take an std::string, and that's why this example has to use a C style string.
In order to use an std::string, you have to use the std global function getline, and not iostream member function.

I just want to make sure the questioner doesn't try mixing both codes together, because it won't work.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>hi must it be char?

You can use string, if you use the std::getline function instead of cin.getline function.
Look at the example code I posted.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
string line;
std::getline(line, cin, '\n');
0
 

Author Comment

by:jaxrpc
Comment Utility
hi,

i tried

std::getline(a,cin,'\n');

i got the below error in vc++.net

Error      1      error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided      c:\documents and settings\jaxrpc\desktop\ok.cpp      69      
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Do you have the following includes?

#include <iostream>
#include <string>

You need both.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Correction:
It should be the following:

string line;
std::getline(cin, line, '\n');

cin is the first argument.
0
 

Author Comment

by:jaxrpc
Comment Utility
hi i got it working, i think it should be std::getline(cin,a,'\n');

but i am having a problem now

cout << "Enter a:";
getline(cin,a,'\n');
cout << endl;
cout << "Enter b:";
getline(cin,b,'\n');
cout << endl;

it output will be
Enter a: Enter b: <Cursor here>
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>but i am having a problem now

I don't understand the problem.
Can you please clarify with expected output and actual output?
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
0
 

Author Comment

by:jaxrpc
Comment Utility
hi, thanks for the prompt reply

cout << "Enter a:";
getline(cin,a,'\n');
cout << endl;
cout << "Enter b:";
getline(cin,b,'\n');
cout << endl;

expected output is

Enter a : <getline(cin,a,'\n'); should read here>
Enter b: <getline(cin,b,'\n'); should read here>

but instead the actual output is
Enter a: <getline skipped here>
Enter b: <getline read here>
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:Axter
Comment Utility
Try the following:
cout << "Enter a:";
getline(cin,a,'\n');
cout << "Enter b:";
getline(cin,b,'\n');
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
What version of VC++ are you using.
VC++ 6.0 has a bug in it, and the workaround is to modify VC++ implementation.
0
 

Author Comment

by:jaxrpc
Comment Utility
i am using vc++ 2005
0
 

Author Comment

by:jaxrpc
Comment Utility
hi:
cout << "Enter a:";
getline(cin,a,'\n');
cout << "Enter b:";
getline(cin,b,'\n');

this gives me

Enter a: Enter b: <getline for b here>

it just keep skipping getline for string a
0
 

Author Comment

by:jaxrpc
Comment Utility
hi, this is the complete code

cout << "Enter integer C:";
cin >> c; //Where is an integer
cout << "Enter a:";
getline(cin,a,'\n');
cout << "Enter b:";
getline(cin,b,'\n');

i realised that after the first cin, it will skip the next getline and then the next getline will work.
i tried it on another new cpp it just skips the next getline. How can i resolve this? thanks
0
 

Author Comment

by:jaxrpc
Comment Utility
cout << "Enter integer C:";
cin >> c; //Where is an integer. cin works fine here

cout << "Enter a:";
getline(cin,a,'\n'); //getline gets skipped here

cout << "Enter b:";
getline(cin,b,'\n'); //getline works well here

any idea? int c; string a,b;
0
 
LVL 16

Accepted Solution

by:
AlexNek earned 250 total points
Comment Utility
Could you try this?
      cout << "Enter integer C:";
      cin >> c; //Where is an integer. cin works fine here

      cin.ignore();

      cout << "Enter a:";
      getline(cin,text1,'\n');
      cout << endl;
      cout << "Enter b:";
      getline(cin,text2,'\n');
      cout << endl;
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 250 total points
Comment Utility
It looks like MS still hasn't fixed this for VC++ 2005.
Read the following link which should have a workaround:
http://support.microsoft.com/kb/q240015/
0
 
LVL 16

Expert Comment

by:AlexNek
Comment Utility
code from 2005
                  else if (_Traits::eq_int_type(_Meta, _Metadelim))
                        {      // got a delimiter, discard it and quit
                        _Changed = true;
                        _Istr.rdbuf()->sbumpc();
                        break;
                        }
0
 

Author Comment

by:jaxrpc
Comment Utility
hi, thanks for the reply.
Ahh...vs is giving the problems. i have to submit this to my lecturer, i can't possibly ask him to change the function header for me which i dont think he will even if i explained that it is a product bug.
Any other workarounds? thank you.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
You can use the C library input logic instead.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Look at using scanf with a C style string buffer.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Or use a free compiler like GNU
0
 

Author Comment

by:jaxrpc
Comment Utility
i guess it could be i am using vs.net 2005 beta, will try to install express edition to see if it works because we are asked to use express for windows.
0
 
LVL 4

Expert Comment

by:Raymun
Comment Utility
>> Any other workarounds?
You can try building the string from individual cin's:

std::string a(""), tmp;
while( std::cin.peek() != '\n' ) {
    std::cin >> tmp;
    a+= tmp + " ";
}
a+= std::endl;
std::cout << a << std::endl;

But this assumes a single space between words i.e. it does not capture any other delimiters that std::cin uses when reading input.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>> Any other workarounds?

From my experiences operator>> and getline cannot be mixed. Use getline for both integer and string input and make the conversion using stringstream class
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
    ...
  int c;
  string sc;
  string a,b;
  cout << "Enter integer C:";
  getline(cin,sc,'\n');
  istringstream iss(sc);
  iss >> c;

  cout << "Enter a:";
  getline(cin,a,'\n'); //getline gets skipped here

  cout << "Enter b:";
  getline(cin,b,'\n'); //getline works well here

Regards, Alex

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now