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++
String Class
Moderator: Board moderators
String Class
-mk
the following code might help
#include <string>
#include <iostream>
using namespace std;
int main() {
string a = "This is a test";
cout<<a<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string a = "This is a test";
cout<<a<<endl;
return 0;
}
K M Hasan
http://www.cs.umanitoba.ca/~kmhasan/
http://www.cs.umanitoba.ca/~kmhasan/
thanks very much.. it works...
^_^
but the problem now is, how can I read a whole line of a string?
if I use
it only reads one word from the buffer.
^_^
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;
-mk
GETLINE
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.
[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.
Re: String Class
Hi,
You can do something like this,
however, after you enter the string, it will wait another \r from you in VC++
but it works properly in VS2005,
Hope it helps.
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;
}
but it works properly in VS2005,
Hope it helps.
C++ Is The Best.
Re: String Class
Thanks Moni.
Reading Moni's Thread
#include<String>
#include<string>
are same.
amazing...
Reading Moni's Thread
#include<String>
#include<string>
are same.
amazing...
String Class
#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;
}
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
No "or", always use #include <string>. There's no <String> on Unix, at least.lnr wrote:#include<String>
or
#include<string>
Re: String Class
Thanks mf.
I did not know about Unix.
I did not know about Unix.