How to read/store two words in one variable?

Basically I have a txt with names and usernames. I know that name+username = up to 20 characters. How do I tell compiler to recognize empty space between names as a symbol and store 2 words as one?

1
2
3
4
5
char name[20];
<...> 
ifstream in ("a.txt");
<...>
 in >> play[i][j].name (empty symbol) reading futher >> ... 
Last edited on
If they are both are only words on same line, you can use getline.
Yes, one casual line contains name+username and some numbers.

Can you be more specific?
in.getline(play[i][j].name, 19) This will store entire line (or first 19 symbols if line is longer) into variable
That's what I get with it:


error: ambiguous overload for 'operator>>' in 'in >> in.std::basic_ifstream<char>::<anonymous>.std::basic_istream<_CharT, _Traits>::getline<char, std::char_traits<char> >(((char*)(&(*(play + ((sizetype)(((unsigned int)i) * 1400u))))[j].grupe::name)), 19)'|
1
2
3
4
5
6
    for(i=0; i<grp; i++){
        in >> n[i];
//cout << n[i] << endl;
        for (j=0; j<n[i]; j++){
        in >> in.getline(play[i][j].name, 19) >>  play[i][j].mins >> play[i][j].sec;}}
}


I've tried also this approach:


1
2
 in >> play[i][j].name >> temp >>   play[i][j].mins >> play[i][j].sec;
        play[i][j].name=play[i][j].name + " " + temp; 



but then ironically I get
error: invalid operands of types 'char [20]' and 'const char [2]' to binary 'operator+'|

I guess it won't work this way with char[20].
Last edited on
Do not use c-strings aka char arrays. Use C++ way to manipulate strings: std::string
1
2
3
4
5
std::string name;
/*...*/
std::string first, username;
std::cin >> first >> username >> play[i][j].mins >> play[i][j].sec;
play[i][j].name = first + " " + username;

Topic archived. No new replies allowed.