Data strucuture c++ exercise

I have to do this:
Information on the sale and purchase of a company will be saved in a data structure.
Struct consists of the name of the shopping company, the shopping type (buy or sell),
the amount of shopping, date of shopping and shopping description (like discount).
1. Define the data structure to keep the information about 1000 companies.
2. This data structure was assumed that the necessary information has been entered in
advance, please write the function that calculate how much the sale and purchase of the
company are. (do not write main() function…)

and I have this code but it do not work. What is/are the problem/s?
#include <iostream>
#include <cstring>
#include <conio.h>

#define n_shop 1000

struct Date
{
int day;
int month;
int year;
};
struct Company
{
char shop[50];
char bos;
float amount;
Date d_shopping;
int disc;
}name[n_shop];

float calculate (company total)
{
int h=0;
int i;
for (i=0; i<n_shop; i++)
{
if(name[i].BoS=='b')
total=total - name[i].amount + (name[i].amount*(name[i].discount/100));
if(name[i].BoS=='s')
total=total + name[i].amount - (name[i].amount*(name[i].discount/100));

}
return (total);
}

Hello.

I assume those are the whole code you have in your file.

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

// Where is main?

#include <iostream> // why do you need this?
#include <cstring> // why do you need this?
#include <conio.h> // why do you need this?

#define n_shop 1000

struct Date
{
int day;
int month;
int year;
};
struct Company
{
char shop[50];
char bos;
float amount; 
Date d_shopping;
int disc;
}name[n_shop];

float calculate (company total) // Is it "C"ompany?
{
int h=0;
int i;
for (i=0; i<n_shop; i++)
{
if(name[i].BoS=='b')
// when did you initialize elements of array 'name'? Is there any probability you call this function before initialization?
// is "BoS" is supposed to be bos?
total=total - name[i].amount + (name[i].amount*(name[i].discount/100));
// where did you declared variable 'total'?
// Is Company structure is supposed to have member variable 'discount'? Isn't it disc?
if(name[i].BoS=='s')
total=total + name[i].amount - (name[i].amount*(name[i].discount/100)); 

}
return (total);  // Why does it needs to be sealed with parenthesis?
}


It is ok now? How can I improve it for the program do that exercise says?


#define n_shop 1000

struct Date
{
int day;
int month;
int year;
};
struct Company
{
char shop[50];
char bos;
float amount;
Date d_shopping;
int disc;
}name[n_shop];

float calculate (Company total)
{
int i;
for (i=0; i<n_shop; i++)
{
if(name[i].bos=='b')
total=total - name[i].amount + (name[i].amount*(name[i].disc/100));
if(name[i].bos=='s')
total=total + name[i].amount - (name[i].amount*(name[i].disc/100));

}
return total;
}
Last edited on
Topic archived. No new replies allowed.