string

Hi everyone,

I'm trying to get a program to do sometimes quiet simple, but I'm struggling to get it done..

What I just want to do is type through an istream three word separeted by space like this... Jose Carvallo Peralta...and later treat that string in the way that I can get three different strings with the name like this string a = Jose, string b = Carvallo, string c = Peralta..

I f somebody could give me a clue, I have read all the member that string has and I do believe that It doesnt have to be so hard...but at the moment I cant find a solution....

thanks!!!
I have done this , but it's completely wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int main(int argc, char **argv)
{
	string a;
	string aux1,aux2,aux3;
	//char temp[20];
	cin>>a;
	//size_t length = a.length();
	
	
		size_t i = 0;
		while(a[i] != ' '){
			aux1 = a[i];
			i++;
		}
		i++;
		while(a[i] != ' '){
			aux2 = a[i];
			i++;
		}
		i++;
		while(a[i] != ' ' && '\0'){
			aux3 = a[i];
			i++;
		}
	
	
	cout<<aux1<<endl;
	cout<<aux2<<endl;
	cout<<aux3<<endl;
 
 
 }




closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>

int main(int argc,char **argv)
{

   std::string aux1,aux2,aux3;
   std::cin>>aux1>>aux2>>aux3;///can do that

        std::cout<<aux1<<std::endl;
	std::cout<<aux2<<std::endl;
	std::cout<<aux3<<std::endl;
 }
Last edited on
Thanks, always I though that doing that I had to press enter after typing a word..

so cin copy until a white space??
closed account (SECMoG1T)
no you don't need to press any key, just type in a continuous stream.

by default std::cin uses the std::noskpws flag which leads till a space is encountered.

http://www.cplusplus.com/reference/ios/noskipws/
I think andy1992 has a pretty good answer, but i think this makes it look a little bit nicer in my opinion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>

int main(int argc,char **argv)
{
   using namespace std;

   string aux1,aux2,aux3;
   cin >> aux1 >> aux2 >> aux3; //can do that

   cout << aux1 << endl;
   cout << aux2 << endl;
   cout << aux3 << endl;

 }


The spaces aren't necessary but make it easier to read.
thanks Andy It was very useful
Topic archived. No new replies allowed.