STRUCTURES AND ARRAY

Nov 29, 2014 at 11:21pm
Please help. I am trying to do that records the salerepresentative,his branch number and his sales using struct and repeat it 4 times. So here is my code but can you please tell me what I do wrong?

#include <iostream>
using namespace std;


struct info
{
char representative;
int branch;
int sales;


};

int main()
{
for(int i=0;i<=3;i++)
{info data[i];

{
cout<<"Enter sales representative";
cin>>info[i].representative;
cout<<"Enter sales";
cin>>info[i].sales;
cout<<"Enter branch";
cin>>info[i].branch;
}
}
return 0;
}
Nov 29, 2014 at 11:38pm
closed account (EwCjE3v7)
You need to define yout variable, remove those brace initlizers and replace list with the variables name.

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
#include <iostream>
using namespace std;


struct info
{
char representative;
int branch;
int sales;


};

int main()
{
info data[4];
for(int i=0;i<=3;i++)
{
cout<<"Enter sales representative";
cin>>data[i].representative;
cout<<"Enter sales";
cin>>data[i].sales;
cout<<"Enter branch";
cin>>data[i].branch;

}
return 0;
}
Last edited on Nov 29, 2014 at 11:38pm
Nov 29, 2014 at 11:38pm
hi xShinichiKudo,

info is a type, data is your variable. The program has not a correct declaration for data, which should be an array of four info. Then you assign the values to the variable (data) :

1
2
3
4
5
6
7
8
9
10
11
    info data[4]; // edit, silly mistake :)

    for(int i=0;i<=3;i++) {
//        info data[i];
        cout<<"Enter sales representative: ";
        cin>>data[i].representative;
        cout<<"Enter sales: ";
        cin>>data[i].sales;
        cout<<"Enter branch: ";
        cin>>data[i].branch;
    }


All the best,
Dean
Last edited on Nov 29, 2014 at 11:48pm
Nov 30, 2014 at 4:16am
@Frozen, I try the code and input mac as representative but why the output is "Enter sales representativemac Enter salesEnter branchEnter sales representativeEnter salesEnter branchEnter sales representativeEnter salesEnter branchEnter sales representativeEnter salesEnter branch " What's wrong with my for loop? PLease help
Topic archived. No new replies allowed.