CORRECT THIS PROGRAM PLEASE

#include<stdio.h>
#include<conio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>


int quantity,sandwichtotal,milkshaketotal,cheesecaketotal,total,
age,totalreview,rate,emppay,review,hours,sales,i,a;
int highest=0;
float totalcost,totalsold,payrate,tax;
float gpay[5];
float npay[5];

struct empdata
{
char fname[10];
char lname[10];
int age; // A structure is created to store the data of three employees
char gender[6];
char DOB [20];
char address[15];
int hours;
int sales;
}des[3];

struct sandwiches
{
char name[8];
int quantity; // A structure is created to store the data of three types of sandwiches sold by the business
}ham,chicken,tuna;

struct milkshakes
{
char name[10];
int quantity;
}chocolate,vanilla,strawberry; // A structure is created to store the data of three types of milkshakes sold by the business

struct cheesecake
{
char name[10];
int quantity; // A structure is created to store the data of two types of cheesecake sold by the business
}cherry,blueberry;



float Payroll()
{
for(i=0;i<3;i++)
{
printf("Please enter first name \n");
scanf("%s",des[i].fname);
printf("Please enter last name \n");
scanf("%s",des[i].lname);
printf("Please enter age \n"); //Option 1. Employees enter their data
scanf("%d",&des[i].age);
printf("Please enter gender \n");
scanf("%s",des[i].gender);
printf("Please enter Date of Birth separated by forward slashes \n");
scanf("%s",des[i].DOB);
printf("Please enter your address separated by underscores \n");
scanf("%s",des[i].address);
printf("Please enter the amount of sales received \n");
scanf("%d",&des[i].sales);

tax=0.08;

printf("Please enter the amount of hours worked \n"); // A sub-function is created to calculate the payroll of each employee
scanf("%d",&hours);
printf("Please enter your payrate \n");
scanf("%f",&payrate);


gpay[i]=des[i].hours*payrate;
npay[i]=gpay[i]-(gpay[i]*tax);

return npay[i];
}

int Itemssold ()

{

sandwichtotal=ham.quantity+tuna.quantity+chicken.quantity;
milkshaketotal=chocolate.quantity+vanilla.quantity+strawberry.quantity; // A sub-function is created to calculate the total sandwiches sold, the total milkshakes sold as well as the total amount of cheeseckes sold
cheesecaketotal=blueberry.quantity+cherry.quantity;

return sandwichtotal,milkshaketotal,cheesecaketotal;
}

int Customer ()
{

totalreview =0;


printf("Please enter a rate from 1-5 or 0 to terminate\n");
scanf("%d", &rate);

do // A sub-function is created to prompt the customer to rate the business. The amount od reviews made is counted.
{
printf("Please enter a rate from 1-5 or 0 to terminate\n");
scanf("%d", &rate);

if (rate<6)
{
totalreview=totalreview+1;
}

}while(rate!=6);



return totalreview;
}

int Goodemployee ()
{

if (sales>highest)
{ // A sub-function is created to calculate the highest saes made by employees. This employee would be the employee of the month
highest=sales;
}



}

float WeeklyReport ()
{
printf("Please enter your preferred sandwich \n");
scanf("%s",sandwich);
printf("Please enter the quantity of sandwiches desired \n"); // The customer is prompted for what they would like
scanf("%d",&quantity);

do
{


if(strcmpi(sandwich,"ham")==0)
{
ham.quantity=ham.quantity+quantity;
}

if(strcmpi(sandwich,"tuna")==0)
{
tuna.quantity=tuna.quantity+quantity;
}

if(strcmpi(sandwich,"chicken")==0)
{
chicken.quantity=chicken.quantity+quantity;
}
printf("Please enter your preferred sandwich \n");
scanf("%s",sandwich);
printf("Please enter the quantity of sandwiches desired \n");
scanf("%d",&quantity);

} while (quantity!=0);

printf("Please enter your preferred type of milkshake \n");
scanf("%s",milkshake);
printf("Please enter the quantity of milkshake desired \n");
scanf("%d",&quantity);

do
{
if(strcmpi(cheesecake,"chocolate")==0)
{
chocolate.quantity=chocolate.quantity+quantity;
}

if(strcmpi(milkshake,"vanilla")==0)
{
vanilla.quantity=vanilla.quantity+quantity;
}

if(strcmpi(milkshake,"stawberry")==0)
{
strawberry.quantity=strawberry.quantity+quantity;
}

printf("Please enter your preferred type of milkshake \n");
scanf("%s",milkshake);
printf("Please enter the quantity of milkshake desired \n");
scanf("%d",&quantity);

}while(quantity!=0);

printf("Please enter your preferred type of cheesecake \n");
scanf("%s",cheesecake);
printf("Please enter the quantity of cheesecake desired \n");
scanf("%d",&quantity);

do
{
if(strcmpi(cheesecake,"cherry")==0)
{
cherry.quantity=cherry.quantity+quantity;
}

if(strcmpi(cheesecake,"blueberry")==0)
{
blueberry.quantity=blueberry.quantity+quantity;
}
printf("Please enter your preferred type of cheesecake \n");
scanf("%s",cheesecake);
printf("Please enter the quantity of cheesecake desired \n");
scanf("%d",&quantity);

}while (quantity!=0);
totalsold=sandwichtotal+milkshaketotal+cheesecaketotal;
for (i=0; i<3; i++)
{

totalcost=totalcost+npay[i]; // A sub-function is created to calculate the total goods sold as well as the total of employee's pay.

}
return totalsold,totalcost;
}

int main ()
{

int i,k,b,d,h;
char sandwich[15],cheesecake[15],milkshake[15],fname[10],lname[10],gender[6], DOB[20], address[15];
int code,age,rate;
FILE*fp;

fp=fopen("Desiire.dat","a+"); // A file is created


printf("\n --------------------------------------------------------- \n");
printf("\n WELCOME TO DESIRE'S NETWORK \n");
printf("\n ----------------------------------------------------------\n");
printf("If you are an employee entering records, please type 1 \n"); // List of options for user to select
printf("If you are a customer who wishes to leave a rate, please type 2 \n");
printf("If you are a customer placing an order, please type 3 \n");
printf("If you wish to exit the program, please type 4 \n");
scanf("%d",&code);
system("cls");


switch (code)
{

case 1:



Payroll(); // The sub-function is called

fprintf (fp,"%s %s \n",des[i].fname,des[i].lname);
fprintf(fp," %d \n",des[i].age);
fprintf(fp," %s \n",des[i].gender);
fprintf(fp," %s \n",des[i].DOB);
fprintf(fp," %s \n",des[i].address); // The output is stored in the file
fprintf(fp," %d \n",des[i].sales);
fprintf(fp," %.2f \n",npay[i]);

Goodemployee(); // A sub-function is called

};

break;

case 2:


Customer(); // A sub-function is called

fprintf(fp,"The amount of reviews made are %d",totalreview);

break;




case 3:



fprintf(fp,"The amount of ham sandwiches sold is %d \n",ham.quantity);
fprintf(fp,"The amount of chicken sandwiches sold is %d \n", chicken.quantity);
fprintf(fp,"The amount of tuna sandwiches sold is %d \n", tuna.quantity);
fprintf(fp,"The total amount of sanwiches bought is %d \n",sandwichtotal);
fprintf(fp,"The amount of chocolate milkshakes sold is %d \n", chocolate.quantity);
fprintf(fp,"The amount of vanilla milkshakes sold is %d \n",vanilla.quantity);
fprintf(fp,"The amount of strawberry milkshakes sold is %d \n", strawberry.quantity); // The output is stored in the file
fprintf(fp,"The total amount of milkshakes bought is %d \n",milkshaketotal);
fprintf(fp,"The amount of cherry cheesecakes sold is %d \n", cherry.quantity);
fprintf(fp,"The amount of blueberry cheesecake sold is %d \n ", blueberry.quantity);

fprintf(fp,"\n");

Itemssold (); // A sub-function is called
MonthlyReport (); // A sub-function is called


fprintf(fp,"The total of stock sold is %d \n",totalsold);
fprintf(fp,"The total of the employee's pay is %.2f \n",totalcost);

system("cls");

break;

case 4:
exit (0);

break;

default: printf(" The option you entered is unavailable");

}


fclose (fp);
getch();

}
On the assumption that you're meant to be using C++, and not C (which is what you've done - you've written a C program):

1
2
3
4
5
#include<stdio.h>
#include<conio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h> 


Don't use any of these headers. This is C++. The C ones have modern C++ replacements. conio.h is non-standard and really shouldn't be used at all. I've no idea why you've got stdarg in this.

Don't use arrays. Use C++ vectors.

Don't use character arrays for holding strings; use actual C++ strings.

Indent your code properly so you can see the structure clearly.

Don't use FILE. This is C++. http://www.cplusplus.com/doc/tutorial/files/


Don't use printf. Use cout (from the C++ ostream header). This is C++, not C.

Don't use scanf. Use cin (from the C++ iostream header). This is C++, not C.

Don't use exit.
Don't use getch.
Don't use system.

Your switch block has a closing brace }; far too early. It's supposed to come after all the cases.

return totalsold,totalcost; You can only return ONE variable.

What you've got here, basically, is a big mess of C code.

Creating all your variables right at the start with that big mess of ints? That's very bad style, and it's something that C had to do right up until the year 1999. Join us in the 21st century; declare variables where you need them.

This whole thing is clearly C code (from before the year 1999). Are you meant to be writing C code?
Last edited on
Style issues aside, this code is a mess. That's okay, we can fix it. When writing programs, it's always a good idea to write a little code and get it working. Then write a little more and get that working. Otherwise you run the risk of having one huge mess. Also, getting little pieces working will often reveal structural problems in your code. It's easier to fix them when the code is small.

So, here is your code with just the employee part. The rest is either ifdef'ed out or deleted (because I hit the post size limit). I have fixed the compilation errors and some (but not all) of the compilation errors. I also indented the code.

It's a very good idea to be precise about what your functions are doing and put a comment above each one describing that. For example, look at Payroll(). it's pretty clear that it's job is to
// Prompt the user and fill in the des array
But something stands our right away. Why does it return a float? At line 120 the return value is ignored, so why does it return anything? Finally at line 77, the return statement is INSIDE the loop, which is clearly wrong. See, just by being precise about the function, we've already found a bug.

Now look at Goodemployee() and do the same thing. What should this function do? What data should it change and what should it return? Write a comment and then verify that the code does what you want.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>

int quantity, sandwichtotal, milkshaketotal, cheesecaketotal, total,
    age, totalreview, rate, emppay, review, hours, sales, i, a;
int highest = 0;
float totalcost, totalsold, payrate, tax;
float gpay[5];
float npay[5];

struct empdata
{
    char fname[10];
    char lname[10];
    int age;                    // A structure is created to store the data of \
three employees
    char gender[6];
    char DOB[20];
    char address[15];
    int hours;
    int sales;
} des[3];

struct sandwiches
{
    char name[8];
    int quantity;               // A structure is created to store the data of \
three types of sandwiches sold by the business
} ham, chicken, tuna;

struct milkshakes
{
    char name[10];
    int quantity;
} chocolate, vanilla, strawberry;       // A structure is created to store the \
data of three types of milkshakes sold by the business

struct cheesecake
{
    char name[10];
    int quantity;               // A structure is created to store the data of \
two types of cheesecake sold by the business
} cherry, blueberry;

float
Payroll()
{
    for (i = 0; i < 3; i++) {
        printf("Please enter first name \n");
        scanf("%s", des[i].fname);
        printf("Please enter last name \n");
        scanf("%s", des[i].lname);
        printf("Please enter age \n");           //Option 1. Employees enter th\
eir data
        scanf("%d", &des[i].age);
        printf("Please enter gender \n");
        scanf("%s", des[i].gender);
        printf("Please enter Date of Birth separated by forward slashes \n");
        scanf("%s", des[i].DOB);
        printf("Please enter your address separated by underscores \n");
        scanf("%s", des[i].address);
        printf("Please enter the amount of sales received \n");
        scanf("%d", &des[i].sales);

        tax = 0.08;

        printf("Please enter the amount of hours worked \n");   // A sub-functi\
on is created to calculate the payroll of each employee
        scanf("%d", &hours);
        printf("Please enter your payrate \n");
        scanf("%f", &payrate);

        gpay[i] = des[i].hours * payrate;
        npay[i] = gpay[i] - (gpay[i] * tax);
        return npay[i];
    }
}

void
Goodemployee()                  // dmh
{

    if (sales > highest) {                       // A sub-function is created t\
o calculate the highest saes made by employees. This employee would be the empl\
oyee of the month
        highest = sales;
    }

}


int
main()
{

    int i;
    int code;
    FILE *fp;

    fp = fopen("Desiire.dat", "a+");             // A file is created

    printf("\n --------------------------------------------------------- \n");
    printf("\n WELCOME TO DESIRE'S NETWORK \n");
    printf("\n ----------------------------------------------------------\n");
    printf("If you are an employee entering records, please type 1 \n");       \
 // List of options for user to select
    printf("If you are a customer who wishes to leave a rate, please type 2 \n"\
);
    printf("If you are a customer placing an order, please type 3 \n");
    printf("If you wish to exit the program, please type 4 \n");
    scanf("%d", &code);
    system("cls");

    switch (code) {

    case 1:

        Payroll();                               // The sub-function is called

        fprintf(fp, "%s %s \n", des[i].fname, des[i].lname);
        fprintf(fp, " %d \n", des[i].age);
        fprintf(fp, " %s \n", des[i].gender);
        fprintf(fp, " %s \n", des[i].DOB);
        fprintf(fp, " %s \n", des[i].address);   // The output is stored in the\
 file
        fprintf(fp, " %d \n", des[i].sales);
        fprintf(fp, " %.2f \n", npay[i]);

        Goodemployee();                          // A sub-function is called

        break;
#if 0
    case 2:

        Customer();                              // A sub-function is called

        fprintf(fp, "The amount of reviews made are %d", totalreview);

        break;

    case 3:

        fprintf(fp, "The amount of ham sandwiches sold is %d \n", ham.quantity)\
;
        fprintf(fp, "The amount of chicken sandwiches sold is %d \n", chicken.q\
uantity);
        fprintf(fp, "The amount of tuna sandwiches sold is %d \n", tuna.quantit\
y);
        fprintf(fp, "The total amount of sanwiches bought is %d \n", sandwichto\
tal);
        fprintf(fp, "The amount of chocolate milkshakes sold is %d \n",
                chocolate.quantity);
        fprintf(fp, "The amount of vanilla milkshakes sold is %d \n", vanilla.q\
uantity);
        fprintf(fp, "The amount of strawberry milkshakes sold is %d \n", strawb\
erry.quantity);  // The output is stored in the file
        fprintf(fp, "The total amount of milkshakes bought is %d \n", milkshake\
total);
        fprintf(fp, "The amount of cherry cheesecakes sold is %d \n", cherry.qu\
antity);
        fprintf(fp, "The amount of blueberry cheesecake sold is %d \n ",
                blueberry.quantity);

        fprintf(fp, "\n");

        Itemssold();                             // A sub-function is called
        MonthlyReport();                         // A sub-function is called

        fprintf(fp, "The total of stock sold is %d \n", totalsold);
        fprintf(fp, "The total of the employee's pay is %.2f \n", totalcost);

        system("cls");

        break;

    case 4:
        exit(0);

        break;
#endif
    default:
        printf(" The option you entered is unavailable");

    }
}

@dhayden,

wouldn't it make more sense to have one struct FoodItem instead of milkshakes cheesecake and sandwiches since they store all the same data.
wouldn't it make more sense to have one struct FoodItem instead of milkshakes cheesecake and sandwiches

Yes. I was going to get to that after the Payroll part was fixed.
Topic archived. No new replies allowed.