C++ std cin

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
jaxrpcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
Use std::getline function instead.
0
AxterCommented:
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
AlexNekCommented:
Did you tried it?
      char line[100];
      cout << " Type a line" << endl;
      cin.getline( line, 100, '\n' );
      cout << line;
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

AlexNekCommented:
You was quicker as I :)
0
jaxrpcAuthor Commented:
hi must it be char?

can i do it like?

string line;

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

0
AxterCommented:
>>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
AxterCommented:
>>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
AxterCommented:
string line;
std::getline(line, cin, '\n');
0
jaxrpcAuthor Commented:
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
AxterCommented:
Do you have the following includes?

#include <iostream>
#include <string>

You need both.
0
AxterCommented:
Correction:
It should be the following:

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

cin is the first argument.
0
jaxrpcAuthor Commented:
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
AxterCommented:
>>but i am having a problem now

I don't understand the problem.
Can you please clarify with expected output and actual output?
0
AlexNekCommented:
0
jaxrpcAuthor Commented:
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
AxterCommented:
Try the following:
cout << "Enter a:";
getline(cin,a,'\n');
cout << "Enter b:";
getline(cin,b,'\n');
0
AxterCommented:
What version of VC++ are you using.
VC++ 6.0 has a bug in it, and the workaround is to modify VC++ implementation.
0
jaxrpcAuthor Commented:
i am using vc++ 2005
0
jaxrpcAuthor Commented:
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
jaxrpcAuthor Commented:
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
jaxrpcAuthor Commented:
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
AlexNekCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxterCommented:
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
AlexNekCommented:
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
jaxrpcAuthor Commented:
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
AxterCommented:
You can use the C library input logic instead.
0
AxterCommented:
Look at using scanf with a C style string buffer.
0
AxterCommented:
Or use a free compiler like GNU
0
jaxrpcAuthor Commented:
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
RaymunCommented:
>> 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
itsmeandnobodyelseCommented:
>> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.