Page 1 of 1

String Class

Posted: Sun Oct 20, 2002 9:57 am
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++

the following code might help

Posted: Sun Oct 20, 2002 3:17 pm
by kmhasan
#include <string>
#include <iostream>
using namespace std;

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

Posted: Sun Oct 20, 2002 9:07 pm
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.

GETLINE

Posted: Mon Oct 21, 2002 10:08 am
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.

Posted: Mon Oct 21, 2002 9:32 pm
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:

Re: String Class

Posted: Fri May 09, 2008 4:46 pm
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.

Re: String Class

Posted: Mon Oct 06, 2008 2:49 pm
by lnr
Thanks Moni.
Reading Moni's Thread

#include<String>
#include<string>

are same.
amazing...

String Class

Posted: Mon Oct 06, 2008 2:52 pm
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;
}

Re: String Class

Posted: Mon Oct 06, 2008 5:28 pm
by mf
lnr wrote:#include<String>
or
#include<string>
No "or", always use #include <string>. There's no <String> on Unix, at least.

Re: String Class

Posted: Mon Oct 06, 2008 5:50 pm
by lnr
Thanks mf.
I did not know about Unix.