string class

J

Jeff

Hi,

I just got C++.NET 2003 and I tried the string class and it doesn't do what
VC++ 5.0 with the string library.

using cin>>str I was able to write a sentence in with spaces in MVC++ 5.0.
With C++.NET 2003 I can't.
Whats the deal?

Sincerely,
JB
 
J

Jeff

I want to add that in the client "main()" it doesn't work.
When written as a class, as an object I am able to get a result that isn't
the easiest way. It seems to break the purpose of the string class library.
 
R

Rolf Magnus

Jeff said:
Hi,

I just got C++.NET 2003 and I tried the string class and it doesn't do
what VC++ 5.0 with the string library.

using cin>>str I was able to write a sentence in with spaces in MVC++ 5.0.
With C++.NET 2003 I can't.

What do you mean by "I can't"? Show a short piece of code that demonstrates
your problem.
 
J

Jeff

Rolf Magnus said:
What do you mean by "I can't"? Show a short piece of code that
demonstrates
your problem.

Here is the code:
#include <iostream>

using std::cout;

using std::cin;

using std::endl;

#include <string>

using std::string;

int main()

{

string st;

cin >> st;

cout << '\n' << st << endl;

return 0;

}

The string behaves like char[]. When a space is introduced and charactrer
follow, the space and following characters do not print. In VC++ 5.0
everything printed.

JB
 
M

Mike Wahler

Jeff said:
Rolf Magnus said:
What do you mean by "I can't"? Show a short piece of code that
demonstrates
your problem.

Here is the code:
#include <iostream>

using std::cout;

using std::cin;

using std::endl;

#include <string>

using std::string;

int main()

{

string st;

cin >> st;

cout << '\n' << st << endl;

return 0;

}

The string behaves like char[].

No, it behaves like a std::string.
When a space is introduced and charactrer follow, the space and following
characters do not print.

This is not the string class doing this, but the
operator << used with 'cout'.

In VC++ 5.0 everything printed.

With that exact same code, I strongly doubt that (or if
it did, that compiler was not complying with the standard
in this respect.

All that having been said, there is indeed a function
in the standard library which will do what you want:
'std::getline()' (declared by <string>). It's a 'free'
(i.e. nonmember) function. Use it like this:

#include <iostream>
#include <string>

int main()
{
std::string st;
std::cout << "Type a string: ";
std::getline(std::cin, st);
std::cout << "You typed: " << st << '\n';
return 0;
}

-Mike
 
B

blip

When a space is introduced and charactrer follow, the space and following
This is not the string class doing this, but the
operator << used with 'cout'.

Actually its std::cin that does'nt take spaces .The insertion >>
operator
has nothing to do with this problem. std::getline() or
std::cin.getline() should
fix it.

Regards,
Me
 
R

Rolf Magnus

blip said:
Actually its std::cin that does'nt take spaces .

cin doesn't care what you give it. It is the insertion operator that reads
from the stream until it finds a whitespace character or the end of the
stream.
The insertion >> operator has nothing to do with this problem.
Wrong.

std::getline() or std::cin.getline() should fix it.

Yes. How would they do that if cin is the problem? They both use it.
 
M

Mike Wahler

blip said:
Actually its std::cin that does'nt take spaces .The insertion >>
operator
has nothing to do with this problem.

Please cite the source of information upon which you
base these claims.
std::getline() or
std::cin.getline() should
fix it.

Yes, as I already stated, the function the OP needs is
'std::getline()'. However, 'std::cin::getline()' will
*not* do what he wants, since it stores characters to
an array and not a std::string object.

Please check your facts before making claims about C++.

-Mike
 
B

blip

Mike said:
Please check your facts before making claims about C++.

Later after I posted, I realized my mistake but did'nt want to post
twice in a row since that does'nt look good and wastes space....
cin.getline() is for char[] 's
getline() is for strings.

Regards,
Me
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top