Error assigning struct value from getline

Currently I have a text file that consists of multiple names and keys deliminated by ";" like so:

client1:client1skey
client2:client2skey
client3:client3skey

I'm wanting to assign these values to a struct so I can work with them later but i'm receiving the error "error: expected primary-expression before ‘[’ token" when compiling.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <ctime>
#include <fstream>

using namespace std;




int main (){
    
    
    
    
    
    int numofclients = 0;
    string lines;
    ifstream CLIENTK;
    CLIENTK.open(".ckeylist");
    while (getline(CLIENTK, lines))
        ++numofclients;
    CLIENTK.close();
    
    
struct clientservers{
    string CLIENT[50];
    string CKEY[50];
    int clientok = 0;
}clients[numofclients];


    
    ifstream fin4(".ckeylist");
        if(!fin4){
            cout << "error reading keylist" << endl;
            return -1;
        }
    ifstream CLIENTK1(".ckeylist", ios::in);    
    CLIENTK1.open(".ckeylist");
    for (int i=0; i < numofclients; i++) {
        getline(CLIENTK1, clientservers[i].CLIENT, ':');
        getline(CLIENTK1, clientservers[i].CKEY, '\n'):
        numofclients++;
    }
    CLIENTK1.close();
        cout << clientservers[1] << endl;
        
    
}
You have a colon instead of a semi-colon at the end of line 50.

I suspect you meant 'clients' (the variable), not 'clientservers' (the type) on lines 49, 50, 54. (This is what is causing your stated error.)

Your strings CLIENT and CKEY on lines 34 presumably don't need the [50]: they are not arrays. Unlike c-strings, std:string doesn't declare a size like this.
Last edited on
Thank you so much lastchance that solved it. Updated code for anyone else who has had this issue:
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
38
39
40
41
42
43
44
45
using namespace std;




int main (){
    
    
    
    
    
    int numofclients = 0;
    string lines;
    ifstream CLIENTK;
    CLIENTK.open(".ckeylist");
    while (getline(CLIENTK, lines))
        ++numofclients;
    CLIENTK.close();
    
    
struct clientservers{
    string CLIENT;
    string CKEY;
    int clientok;
}clients[numofclients];


    
    ifstream fin4(".ckeylist");
        if(!fin4){
            cout << "error reading keylist" << endl;
            return -1;
        }
    ifstream CLIENTK1(".ckeylist", ios::in);    
    CLIENTK1.open(".ckeylist");
    for (int i=0; i < numofclients; i++) {
        getline(CLIENTK1, clients[i].CLIENT, ':');
        getline(CLIENTK1, clients[i].CKEY, '\n');
        numofclients++;
    }
    CLIENTK1.close();
    
        
    
}
Topic archived. No new replies allowed.