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 »

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
kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

the following code might help

Post by kmhasan »

#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 »

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
Ming Han
Learning poster
Posts: 77
Joined: Thu Jun 06, 2002 7:10 pm
Location: Singapore
Contact:

GETLINE

Post by Ming Han »

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.
Moni
Experienced poster
Posts: 202
Joined: Fri Mar 22, 2002 2:00 am
Location: Chittagong. CSE - CUET
Contact:

Post by Moni »

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 »

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: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: String Class

Post by lnr »

Thanks Moni.
Reading Moni's Thread

#include<String>
#include<string>

are same.
amazing...
lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

String Class

Post by lnr »

#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 »

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: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: String Class

Post by lnr »

Thanks mf.
I did not know about Unix.
Post Reply

Return to “C++”