String Class

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
emka
New poster
Posts: 10
Joined: Sat Oct 19, 2002 7:20 am
Location: Singapore
Contact:

String Class

Post by emka » Sun Oct 20, 2002 9:57 am

Hi everyone,

I would like to ask you about how to use the String class??

usually I use:

[cpp]#include <String.h>
...
String s;
...
[/cpp]

but I cannot do it in VC++
-mk

User avatar
kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

the following code might help

Post by kmhasan » Sun Oct 20, 2002 3:17 pm

#include <string>
#include <iostream>
using namespace std;

int main() {
string a = "This is a test";
cout<<a<<endl;
return 0;
}

emka
New poster
Posts: 10
Joined: Sat Oct 19, 2002 7:20 am
Location: Singapore
Contact:

Post by emka » Sun Oct 20, 2002 9:07 pm

thanks very much.. it works...
^_^

but the problem now is, how can I read a whole line of a string?

if I use

Code: Select all

string s;
cin >> s;
it only reads one word from the buffer.
-mk

User avatar
Ming Han
Learning poster
Posts: 77
Joined: Thu Jun 06, 2002 7:10 pm
Location: Singapore
Contact:

GETLINE

Post by Ming Han » Mon Oct 21, 2002 10:08 am

Example:

[cpp]
#include <string.h>
#include <iostream.h>

using namespace std;

int main(){
char dat[100]={'\0'};
cin.getline(dat,100,'\n');
return 0;
}
[/cpp]

check up http://www.cplusplus.com is you don't know.

User avatar
Moni
Experienced poster
Posts: 202
Joined: Fri Mar 22, 2002 2:00 am
Location: Chittagong. CSE - CUET
Contact:

Post by Moni » Mon Oct 21, 2002 9:32 pm

The class is different for Turbo C++, Borland C++ also for VC++. Some uses <String.h> some <string.h> or <String> etc. The containing funcitios are also not same.So find it in the help sections of your compiler. :wink:
ImageWe are all in a circular way, no advances, only moving and moving!

amr saqr
New poster
Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

Re: String Class

Post by amr saqr » Fri May 09, 2008 4:46 pm

Hi,
You can do something like this,

Code: Select all

#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin,str);
cout<<str<<endl;
return 0;
}
however, after you enter the string, it will wait another \r from you in VC++
but it works properly in VS2005,
Hope it helps.
C++ Is The Best.

lnr
Experienced poster
Posts: 141
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: String Class

Post by lnr » Mon Oct 06, 2008 2:49 pm

Thanks Moni.
Reading Moni's Thread

#include<String>
#include<string>

are same.
amazing...

lnr
Experienced poster
Posts: 141
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

String Class

Post by lnr » Mon Oct 06, 2008 2:52 pm

#include<String>
or
#include<string>

int main()
{
string s;
cin>>s;
//this takes a string until a space character.
char str[105];
cin.getline(str,100,'\n');//this takes until a newline
cout<<s<<endl;
cout<<str<<endl;
return 0;
}

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: String Class

Post by mf » Mon Oct 06, 2008 5:28 pm

lnr wrote:#include<String>
or
#include<string>
No "or", always use #include <string>. There's no <String> on Unix, at least.

lnr
Experienced poster
Posts: 141
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: String Class

Post by lnr » Mon Oct 06, 2008 5:50 pm

Thanks mf.
I did not know about Unix.

Post Reply

Return to “C++”