Entering struct information from command line

Hey guys, im having a problem here, i need to make script that uses structure, but i need to enter struct information from the command line. By the way im doing it as menu type.

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
#include <iostream>

using namespace std;

struct okey
{
    string one;
    string two;
    string ttree;
    double four;
};
int main()
{
    okey j;
    int n,pas;
    do
    {
       
        cout << "1) enter information " << endl;
        cout << "2) bla bla bla " << endl;
        cout << "3) blo blo blo" << endl;
        cout << "your choice : ";
        cin >> pas;
 if(pas == 1)
        {
cout << "How many record?" ;
cin >> n;
        if(pas == 1)
        {
            cout << "How many records?" ;
            cin >> n;
            for (int j=0; j <=n; j++){

                     cout << "one: ";
                     getline(cin, j.one);
                     cout << "two: ";
                     getline(cin, j.two);
                     cout << "three: ";
                     getline(cin, j.three);
                     cout << "four: ";
                     getline(cin, j.four);
                
} 
        }


When i do this i get error "Member reference base type 'int' is not a structure or union'

Can you guys help me to solve this out?

EDIT:
Almost forgot:
What if the guy decides to add some more information after he did? It will rewrite the first record, how to avoid this? :/
Last edited on
cin >> j.one;
you can't assign to a std::string like this. Instead look here:
http://www.cplusplus.com/reference/string/string/getline/

also you have a spelling error in your struct declaration:
string tree;

also you need a few closing braces at the end (unless you haven't posted the full thing)

also you need a 'while' to accompany your 'do' (again, unless you haven't posted the full program).

also, consider using sensible names for class, structs, methods and variables. Things like "okey", "one", "two", "three" will not be helpful when you come to debug your code.
Last edited on
You have declared 'j' as both an int inside the context of your for loop (line 27), and as a struct okey (line 29).
Hello mutexe, no its not the full code its only the part, that i need help with. its only a test, so thats why the names are so random.

so I changed it to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        if(pas == 1)
        {
            cout << "How many records?" ;
            cin >> n;
            for (int j=0; j <=n; j++){

                     cout << "one: ";
                     getline(cin, j.one);
                     cout << "two: ";
                     getline(cin, j.two);
                     cout << "three: ";
                     getline(cin, j.three);
                     cout << "four: ";
                     getline(cin, j.four);
                 }

I sitll get the same error: Member refenrence base type 'int' is not a struct
Hello Texan40,

Thanks, ive changed it to for (j=1; j <=n; j++){ now I get:
Cannot increment value of type 'okey'
That's because okey is still defined as j as well.
1
2
3
4
5
6
okey j; // struct okey

for(int j = 0; j < n; j++) // local int j is incremented
{
  cout << "one: ";
  getline(cin,j.one); // local variable j is not a struct or union, it is an int so this is in error 


There are more errors, but I think it's important for you to understand how the scope of variables effects you. At the very least do not use the same variable name both globally and locally as it can be confusing.
Topic archived. No new replies allowed.