Strings

G

Gaijinco

Every information about strings seems to be very confusing!

I need a program to read 3 different strings, one after the other all
of them of unknown size. All the strings are separated by and
end-of-line. The first and last don't have whitespace but the last one
has. What is the best method to do this?

I tried this but the program doesn't get the last string:

string line1;
string line2;
char line3[100];

cin >> line1 >> line2;
cin.getline(line3,100);
 
A

Alf P. Steinbach

* Gaijinco:
Every information about strings seems to be very confusing!

OK, here you'll get some _very clear_ info.
I need a program to read 3 different strings, one after the other all
of them of unknown size. All the strings are separated by and
end-of-line. The first and last don't have whitespace but the last one
has. What is the best method to do this?

I tried this but the program doesn't get the last string:

string line1;
string line2;
char line3[100];

cin >> line1 >> line2;
cin.getline(line3,100);

Use std::string for all three strings.

Use the std::getline function from <string>, which you call like

std::getline( std::cin, s );

Do not use raw character arrays as you have for line3.
 
G

Gaijinco

Use std::string for all three strings.
Use the std::getline function from <string>, which you call like

std::getline( std::cin, s );

You meant like this:

std::string line1, line2, line3;

cin >> line1 >> line2;
std::getline( std::cin, line3 );
cout << line1 << endl << line2 << endl << line3;

The problem with this is that line3 is not printed, I suppose there is
a special function to do it?
 
A

Alf P. Steinbach

* Gaijinco:
You meant like this:

std::string line1, line2, line3;

cin >> line1 >> line2;
std::getline( std::cin, line3 );
cout << line1 << endl << line2 << endl << line3;

The problem with this is that line3 is not printed, I suppose there is
a special function to do it?

Use std::getline also for input of line1 and line2.

And in general, wherever you want to read _lines_ of input.

Simple, yes?
 
P

__PPS__

Gaijinco said:
Every information about strings seems to be very confusing!

I need a program to read 3 different strings, one after the other all
of them of unknown size. All the strings are separated by and
end-of-line. The first and last don't have whitespace but the last one
has. What is the best method to do this?

That's impossible. You cannot have any whitespace in a string that has
no whitespace
I tried this but the program doesn't get the last string:

string line1;
string line2;
char line3[100];

cin >> line1 >> line2;
cin.getline(line3,100);

Basicly, cin >> line1 reads input until whitespace (or end of line)
met; std::getline reads until only end-of-line

for this code:
cin >> line1 >> line2;
cin.getline(line3,100);

if you enter on one line this:
abc def xyz
then line1="abc", line2="def", line3= whatever left, but not longer
than 99 chars (not counting terminatig \n)

if you want to make sure line1 & 2 don't have whitespace, but line3 may
have anything, then:

std::getline(std::cin>>line1>>line2,line3);
 
G

Gaijinco

It continues to behave strange!

If I do this:

string line1, line2, line3;

std::getline(std::cin,line1);
std::getline(std::cin,line2);
std::getline(std::cin,line3);

The program waits for 4 inputs!

If I do:

string line1, line2, line3;

std::getline(std::cin>>line1>>line2,line3);

Then this line:

cout << "\"" << line3 << " " << line3.size() << "\"" << endl;

Print this

" 0"

No matter what I type for line3!

Any ideas of what's wrong?
 
P

__PPS__

Gaijinco said:
Print this

" 0"

No matter what I type for line3!

Any ideas of what's wrong?

That's because you input nothing for line3. You have to input anything
for line1 and line2, but if you just hit [Enter] line3 will be empty
("")
for code:
std::getline(std::cin>>line1>>line2,line3);

do this input:

text1 text2 text3
and see what happens.

if you do this input:

text1
text2
text3

then here's what happens: cin>>line1 read "text1" just untill the \n
character, which isn't read. Then cin>>line2 skips untill the first
non-whitespace character (skips \n) and reads "text2" again leaving \n.
Then getline(cin, line3) reads whatever there's in the input sream
until end-of line character, which is the first character waiting in
the stream. So, it reads in this string: ""
You may discard the next character after the last input operation like
this:
std::cin.ignore()
so:

std::cin >> line1;
std::cin >> line2;
std::cin.ignore();
std::getline(line3);

is what you most likely needed, or you may write like this :)

getline( (cin>>line1>>line2).ignore(), line3 );

....of course, it's better to use getline for all strings, but in this
case input may have spaces
 
G

Gaijinco

In fact the problem was VC++ 6.0, in Dev-Cpp it compiled perfrectly and
in C++ Primer it is stated that getline doesn't work well in VC++ 5.0
and 6.0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top