Solved

C++ std cin

Posted on 2007-04-11
31
7,404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 10
  • 5
  • +2
31 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 18891111
Use std::getline function instead.
0
 
LVL 30

Expert Comment

by:Axter
ID: 18891132
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
ID: 18891144
Did you tried it?
      char line[100];
      cout << " Type a line" << endl;
      cin.getline( line, 100, '\n' );
      cout << line;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:AlexNek
ID: 18891157
You was quicker as I :)
0
 

Author Comment

by:jaxrpc
ID: 18891190
hi must it be char?

can i do it like?

string line;

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

0
 
LVL 30

Expert Comment

by:Axter
ID: 18891198
>>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
ID: 18891210
>>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
ID: 18891228
string line;
std::getline(line, cin, '\n');
0
 

Author Comment

by:jaxrpc
ID: 18891233
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
ID: 18891349
Do you have the following includes?

#include <iostream>
#include <string>

You need both.
0
 
LVL 30

Expert Comment

by:Axter
ID: 18891363
Correction:
It should be the following:

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

cin is the first argument.
0
 

Author Comment

by:jaxrpc
ID: 18891367
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
ID: 18891410
>>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
ID: 18891429
0
 

Author Comment

by:jaxrpc
ID: 18891433
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
 
LVL 30

Expert Comment

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

Expert Comment

by:Axter
ID: 18891474
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
ID: 18891484
i am using vc++ 2005
0
 

Author Comment

by:jaxrpc
ID: 18891502
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
ID: 18891664
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
ID: 18891685
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
ID: 18891891
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
ID: 18891978
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
ID: 18892081
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
ID: 18895151
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
ID: 18895163
You can use the C library input logic instead.
0
 
LVL 30

Expert Comment

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

Expert Comment

by:Axter
ID: 18895169
Or use a free compiler like GNU
0
 

Author Comment

by:jaxrpc
ID: 18895276
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
ID: 18895308
>> 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
ID: 18896915
>> 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

734 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