How can I capture the string in C++?
For example, the input is: Mary Average User
and I can capture the those three words into three variables?
Thx!~
Printable View
How can I capture the string in C++?
For example, the input is: Mary Average User
and I can capture the those three words into three variables?
Thx!~
Something like this would work -
Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
string a,b,c;
cin >> a >> b >> c;
cout << a << endl << b << endl << c << endl;
return 0;
}
you wanna capture into different variables or into a single one???Quote:
Originally posted by DramaKing
How can I capture the string in C++?
For example, the input is: Mary Average User
and I can capture the those three words into three variables?
Thx!~
look for a single variable you could use:
what zen said is right...Code:char s[200];
gets(s);
//if youre reading from a file then use fgets
Mike
Thx!! The program can put three words in three different variables, how about if I key in two words, the first word will put in varaible "a" and the second word will put in varible c?
Use cin.getline() to get the input as a string then use strtok() or strchr() to split the string up into its constituent words.A search of these boards should throw some light on this.