The Unresolved external symbol error is preventing the compilation of my code. It specifically is mentioning two functions being called in main. The functions are a part of a switch I am trying to create and it is still under construction. If anyone has any suggestions for how I can fix the bug or improve my code please let me know and thank you in advance. FYI- I already searched for similar questions and they are not specific to my problem...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//This is a macro intended for use with the emplyName array.
#define SIZE 20
//This struct has all the varibles that I will be using in my functions
typedef struct person
{
char* emplyName[5][SIZE];
float emplyHours[5];
float emplyRate[5];
float emplyGross[5];
float emplyBase[5];
float emplyOvrt[5];
float emplyTax[5];
float emplyNet[5];
float emplyTotal[5];
}input;
void menu(void);
void employeeInfo(input* emply);
void editEmployees(input* emply);
void print(input* emply);
int main(void)
{
struct person *payroll={""};
int choice = 0;
menu();
scanf_s("%c", &choice);
switch (choice){
case '1':
employeeInfo(payroll);
break;
case '2':
editEmployees(payroll);
break;
case '3':
print(payroll);
break;
case '4':
break;
default:
printf("Invalid entry\n");
}
system("pause");
}
void employeeInfo(input *emply)
{
int i=0;
do {
printf("Enter employee name.\n");
scanf_s("%s", &emply->emplyName[i]);
printf("Enter employee hours.\n");
scanf_s("%f", &emply->emplyHours[i]);
printf("Enter Hourly rate.\n");
scanf_s("%f", &emply->emplyRate[i]);
} while (++i <= 5);
void calculations(input *emply);/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
int i;
i = 0;
for (i = 0; i < 5; i++){
if (emply->emplyHours[i] > 40) {
emply->emplyOvrt[i] = (emply->emplyHours[i] - 40) * (emply->emplyRate[i] * 1.5);
}
emply->emplyGross[i] = (((emply->emplyHours[i])*(emply->emplyRate[i])) + emply->emplyOvrt[i]);
emply->emplyBase[i] = (emply->emplyGross[i]) - (emply->emplyOvrt[i]);
emply->emplyTax[i] = ((emply->emplyGross[i])*.2);
emply->emplyNet[i] = (emply->emplyGross[i]) - (emply->emplyTax[i]);
emply->emplyTotal[0] += emply->emplyGross[i];
}
}
void print(input *emply);
{
int i;
for (i = 0; i < 5; i++)
{
printf("Employee Name:%s\n", emply->emplyName[i]);
printf("Hours Worked:%.2f\n ", emply->emplyHours[i]);
printf("Hourly Rate:%.2f\n", emply->emplyRate[i]);
printf("Gross Pay:%.2f\n", emply->emplyGross[i]);
printf("Base Pay:%.2f\n", emply->emplyBase[i]);
printf("Overtime Pay:%.2f\n", emply->emplyOvrt[i]);
printf("Taxes Paid:%.2f\n", emply->emplyTax[i]);
printf("Net Pay:%.2f\n", emply->emplyNet[i]);
}
printf("Total paid to all employees : %.2f\n", emply->emplyTotal[0]);
}
void editEmployees(input*emply);
{
char edit;
int i;
printf("which employee would you like to edit?\n");
for (i = 0; i < 5; i++){
printf("%d.%s", i + 1, emply->emplyName[i]);
}
scanf_s("%c", &edit);
switch (edit){
case '1':
printf("Enter employee name.\n");
scanf_s("%s", &emply->emplyName[0]);
printf("Enter employee hours.\n");
scanf_s("%f", &emply->emplyHours[0]);
printf("Enter Hourly rate.\n");
scanf_s("%f", &emply->emplyRate[0]);
}
}
}
void menu(void){
printf("Main Menu\n");
printf("1. Add Employee\n");
printf("2. Edit Employee\n");
printf("3. Print Employee\n");
printf("4. Print All EMployees\n");
printf("0. exit\n");
}
You defined editEmployees() and print() local to employeeInfo().
This hides them from the the rest of the program.
This is not Standard C.
If you'd indented your code properly you most probably would have noticed this yourself.
Same for calculations().
Related
I'm having problems editing the fields of a structure from a seperate function, I'm trying to edit fields of my drone structure from the update droneinfofunction .basically i get the same error for all the arrows (invalid type argument of '->')
i'm sure this problem stems from my lack of understanding of pointers
any help would be greatly appreciated :)
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
//define struct info and variables
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {
int searchID, numdrones, i, drYrMan;
double drMass, drTopSpeed, drMaxDist, drLoadCap;
char drname[20];
numdrones = no_of_drones;
printf("what drone ID would you like to update?: ");
scanf("%d", &searchID);
printf("name: ");
scanf("%s", drname);
printf("year manufactured: ");
scanf("%d", &drYrMan);
printf("mass: ");
scanf("%lf", &drMass);
printf("top speed: ");
scanf("%lf", &drTopSpeed);
printf("max distance: ");
scanf("%lf", &drMaxDist);
printf("load capacity: ");
scanf("%lf", &drLoadCap);
droneinfo[searchID]->drone_number = searchID;
droneinfo[searchID]->drone_name = drname;
droneinfo[searchID]->year_manufactured = drYrMan;
droneinfo[searchID]->mass = drMass;
droneinfo[searchID]->top_speed = drTopSpeed;
droneinfo[searchID]->max_distance = drMaxDist;
droneinfo[searchID]->load_capacity = drLoadCap;
for(i=0; i < numdrones; i++){
}
return 0;
}
//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){
int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;
scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
if (!strcmp(namechoice, droneinfo[i].drone_name)) {
printf("found a match\n\nID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
found = 1;
}
}
if(found == 0){
printf("\nNo matches were found!\n");
}
return 0;
//make condition for all
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;
//open the drone.txt file where the drone info is stored
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//initialise the function that puts the struct in an array
while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
//print the dtone info in an array
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
do{
//program menu with appropriate menu items
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
//switch for the 7 available menu cases
switch(choice)
{
case 1:
//input drone function
updateDroneInfo(droneinfo, no_of_drones);
break;
case 2:
//search drone function
searchDroneName(droneinfo, no_of_drones);
break;
case 3:
//simulate drone function
break;
case 4:
//display simulation results
break;
case 5:
//save drone information
break;
case 6:
//save all results function
break;
case 7:
//exit/breaks the loop
break;
default:
printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
break;
}
} while(choice != 7);
return 0;
}
re
droneinfo[searchID]
has the type drone_info and not drone_info*, so you use . instead of ->.
In statements like this:
droneinfo[searchID]->drone_number = searchID;
the expression droneinfo[searchID] is not a pointer. It has the type drone_info because the pointer droneinfo was already dereferenced by the subscript operator.
You have to write:
droneinfo[searchID].drone_number = searchID;
Also arrays do not have the assignment operator. You need to copy element elements from one array to another.
Instead of this statement:
droneinfo[searchID]->drone_name = drname;
you have to write using the standard string function strcpy:
$include <string.h>
//...
strcpy( droneinfo[searchID].drone_name, drname );
The first screen, gotoxy code is working. But in second screen. Nothing happens, like, it does not read the gotoxy code at all. Please enlighten me about the problem.
Here is the 1st screen :
Here is the 2nd screen :
Here is the code. I would love to learn more about gotoxy.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy( int column, int line );
int main();
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int choice;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,8);
printf("1. Add Product\n\n");
gotoxy(22,10);
printf("2. Display Product\n\n");
gotoxy(22,12);
printf("3. Search Product\n\n");
gotoxy(22,14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22,16);
printf("5. Update Product\n\n");
gotoxy(22,20);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch(choice)
{
case 1 : addProduct();
break;
case 2 : displayProduct();
break;
case 3 : searchProduct();
break;
case 4 : reorderProduct();
break;
case 5 : updateProduct();
break;
default : printf("Wrong input. Please try again.");
system("cls");
main();
}
return (0);
}
int addProduct()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
do
{
fp = fopen("inventory.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
main();
}
else
{
do{
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
}while(checker != 'Y' && checker != 'N');
if(checker == 'Y'){addProduct();}
if(checker == 'N'){
system("cls");
main();}
}
return(0);
}
I'm having serious trouble with my program it is supposed to provide a menu and do all the functions the code is pretty explanatory my problem is I only have visual studios which doesnt allow scanf and scanf_s and messes with things so I use online compilers but those are still iffy. Can any one help and give me some tips. I'm having trouble with the math and function to list accounts, some whiles are empty as well I wasn't sure if that was best to use. I'm relatively new to this forum. It also can't have struct and can only be in C :-(
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
char option;
while(1){
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
option = getchar();
scanf("%c\n", &option);
switch(option)
{
case '1': open();
break;
case '2': list();
break;
case '3': deposit();
break;
case '4': withdraw();
break;
case '5': return 0;
default: exit();
break;
}
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
The problem with scanf was interesting. Here is an example for how to do without (although you shouldn't) such that you can play with your code easier.
#include <stdio.h>
#include <stdlib.h>
// only 5 accounts possible
int accounts[5];
// each its balance
float avail_bal[5];
void options();
// open(P) is a standard Posix function
void bopen();
void list();
void deposit();
void withdraw();
// exit(3) is a standard C function
void pexit();
int main(void)
{
char option;
while (1) {
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
options();
option = getc(stdin);
// swallow the '\n'
getc(stdin);
switch (option) {
case '1':
bopen();
break;
case '2':
list();
break;
case '3':
deposit();
break;
case '4':
withdraw();
break;
case '5':
pexit();
default:
pexit();
}
}
return 0;
}
void options()
{
puts("1. Open Account");
puts("2. List Accounts");
puts("3. Deposit");
puts("4. Withdraw");
puts("5. Exit");
}
void bopen()
{
int acc_num;
char c;
puts("Open new account(enter number 1-5)");
c = getc(stdin);
getc(stdin);
// assuming ASCII here where the digits 0-9 start at place 48 in the table
acc_num = (int) c - 48;
if (acc_num < 1 || acc_num > 5) {
puts("Account number must be between one and five inclusive");
return;
}
if (accounts[acc_num] != 0) {
printf("Account number %d is already taken\n", acc_num);
return;
}
// mark account as taken
accounts[acc_num] = 1;
// spend a fiver for the new client for being a new client
avail_bal[acc_num] = 5.0;
printf("Account number: %d\n", acc_num);
printf("Available balance: %f\n", avail_bal[acc_num]);
}
void list()
{
int i;
for (i = 0; i < 5; i++) {
if (accounts[i] != 0) {
printf("Account 000%d: %f\n", i, avail_bal[i]);
}
}
}
void deposit()
{
float add;
int acc_num;
char c;
char s[100];
puts("Which account do you want to deposit money in?");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
printf("Amount to deposit: ");
// to get a number without scanf() we have to read the input as a string
// (fgets() adds a '\0' at the end, so keep a seat free for it)
fgets(s, 99, stdin);
// and convert it to a double (atof() only for brevity, use strtod() instead)
add = atof(s);
avail_bal[acc_num] += add;
printf("Amount deposited %f\n", add);
}
void withdraw()
{
int acc_num;
float withdraw;
char c;
char s[100];
// all checks ommitted!
puts("Account to withdraw from: ");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
puts("Amount to withdraw from account: ");
fgets(s, 99, stdin);
withdraw = atof(s);
avail_bal[acc_num] -= withdraw;
printf("Current balance for account %d: %f\n", acc_num, avail_bal[acc_num]);
}
void pexit()
{
// place logic to save data here or use a function triggered by atexit() for that task
puts("Imagine all of your data would have been put in a safe place!");
exit(EXIT_SUCCESS);
}
Just replace the constructs with scanf before you pass your work for grading.
If you're using integer values as options why you are using character's
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
int option;
printf("****Banking System WELCOME****\n");
void options();
printf("Enter 1-5 of the following options: \n");
scanf("%d",&option);
switch(option)
{
case 1: open();
break;
case 2: list();
break;
case 3: deposit();
break;
case 4: withdraw();
break;
case 5: return 0;
default: exit();
break;
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
The program compiles fine however there are a few issues that come up...
The calculations will not print out properly.
After making a selection the menu will run again automatically without asking for input.
Edit employees does not overwrite current employee only adds another to the list.
I am including all my code since I am not sure where the bug is.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//This is a macro intended for use with the emplyName array.
#define SIZE 20
//This struct has all the varibles that I will be using in my functions
typedef struct person
{
char emplyName[SIZE];
float emplyHours;
float emplyRate;
float emplyGross;
float emplyBase;
float emplyOvrt;
float emplyTax;
float emplyNet;
float emplyTotal;
}input;
void menu(void);
void editEmployees(input* emply);
void print(input* emply);
void employeeInfo(input* emply);
void calculations(input *emply);
int main(void)
{
struct person payroll[5] = { 0 };
int choice = 0, currentEmployee = 0;
calculations(&payroll);
do
{
menu();
scanf_s("%c", &choice, 1);
switch (choice){
case '1':{
employeeInfo(&payroll[currentEmployee]);
currentEmployee;
break;
}
case '2':{
editEmployees(&payroll[currentEmployee]);
break;
}
case '3':{
print(&payroll[currentEmployee]);
break;
}
case '4':{
printAll(payroll);
break;
}
case '0':{
break;
}
default:
printf("Invalid entry\n");
}
} while (choice != 5);
system("pause");
}
void employeeInfo(input *emply)
{
printf("Enter employee name.\n");
scanf_s("%s", &emply->emplyName,SIZE);
printf("Enter employee hours.\n");
scanf_s("%f", &emply->emplyHours);
printf("Enter Hourly rate.\n");
scanf_s("%f", &emply->emplyRate);
}
void calculations(input *emply)/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
if (emply->emplyHours > 40) {
emply->emplyOvrt = (emply->emplyHours - 40) * (emply->emplyRate * 1.5);
}
emply->emplyGross = (((emply->emplyHours)*(emply->emplyRate)) + emply->emplyOvrt);
emply->emplyBase = (emply->emplyGross) - (emply->emplyOvrt);
emply->emplyTax = ((emply->emplyGross)*.2);
emply->emplyNet = (emply->emplyGross) - (emply->emplyTax);
emply->emplyTotal += emply->emplyGross;
}
void print(input *employees)
{
int i;
for (i = 0; i < 5; i++)
{
printf("Employee Name:%s\n", employees[i].emplyName);
printf("Hours Worked:%.2f\n ", employees[i].emplyHours);
printf("Hourly Rate:%.2f\n", employees[i].emplyRate);
printf("Gross Pay:%.2f\n", employees[i].emplyGross);
printf("Base Pay:%.2f\n", employees[i].emplyBase);
printf("Overtime Pay:%.2f\n", employees[i].emplyOvrt);
printf("Taxes Paid:%.2f\n", employees[i].emplyTax);
printf("Net Pay:%.2f\n", employees[i].emplyNet);
}
printf("Total paid to all employees : %.2f\n", employees[i].emplyTotal);
}
void printAll(input *emply)
{
int i;
for (i = 0; i < 5; i++)
{
if (strcmp(emply->emplyName[i], "-1") == 0){
break;
}
printf("Employee Name:%s\n", emply[i].emplyName);
printf("Hours Worked:%.2f\n ", emply[i].emplyHours);
printf("Hourly Rate:%.2f\n", emply[i].emplyRate);
printf("Gross Pay:%.2f\n", emply[i].emplyGross);
printf("Base Pay:%.2f\n", emply[i].emplyBase);
printf("Overtime Pay:%.2f\n", emply[i].emplyOvrt);
printf("Taxes Paid:%.2f\n", emply[i].emplyTax);
printf("Net Pay:%.2f\n", emply[i].emplyNet);
}
printf("Total paid to all employees : %.2f\n", emply[i].emplyTotal);
}
void editEmployees(input*emply)
{
int edit;
int i;
printf("which employee would you like to edit?\n");
for (i = 0; i < 5; i++){
printf("%d.%s\n", i + 1, emply[i].emplyName);
}
scanf_s("%d", &edit);
employeeInfo(&emply[edit]);
}
void menu(void)
{
printf("Main Menu\n");
printf("1. Add Employee\n");
printf("2. Edit Employee\n");
printf("3. Print Employee\n");
printf("4. Print All Employees\n");
printf("0. exit\n");
}
Few things:
If the menu isn't working properly, it is possible that values are left uninitialized.
You should really check the return value of scanf. Furthermore, you need to clear the input buffer after making a call to scanf. char ch = getchar(); while (ch != EOF && ch != '\n') ch = getchar();.
I don't think you are compiling with warnings; you should. For instance, currentEmployee; does absolutely nothing.
Inside editEmployee, you are passing only a single employee to it. A better way would be to just pass payroll by value. Arrays will implicitly pass their elements by pointer -- if you change the value of one of the array's indices without changing the location the array points to, then that change will be reflected when the program returns to main.
You should call calculations at the end of employeeInfo. Right now, it isn't being called but once.
You should properly init payroll to actual values; otherwise, the result of calculations is undefined. Also, `calculations(&payroll) is just plain wrong as that should pass a pointer-to-array which is obviously not you wanted. (How is the program compiling?)
Here's some editted code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//This is a macro intended for use with the emplyName array.
#define SIZE 20
//This struct has all the varibles that I will be using in my functions
typedef struct person
{
char emplyName[SIZE];
float emplyHours;
float emplyRate;
float emplyGross;
float emplyBase;
float emplyOvrt;
float emplyTax;
float emplyNet;
float emplyTotal;
}input;
void menu(void);
void editEmployees(input* emply);
void clear_input(void);
void print(input *employee);
void printAll(input* emply);
void employeeInfo(input* emply);
void calculations(input *emply);
int main(void)
{
struct person payroll[5] = {
{
.emplyName = "-1"
},
{
.emplyName = "-1"
},
{
.emplyName = "-1"
},
{
.emplyName = "-1"
},
{
.emplyName = "-1"
}
};
int choice = 0, currentEmployee = 0;
int valid = 0;
do
{
do {
menu();
valid = scanf_s("%c", &choice, 1);
clear_input();
} while (valid != 1);
switch (choice) {
case '1':
employeeInfo(&payroll[currentEmployee]);
break;
case '2':
editEmployees(&payroll[currentEmployee]);
break;
case '3':
print(&payroll[currentEmployee]);
break;
case '4':
printAll(payroll);
break;
case '0':
break;
default:
printf("Invalid entry\n");
break;
}
} while (choice != '0');
system("pause");
/* Required */ return 0;
}
void clear_input(void) {
char ch;
while ((ch = getchar()) != EOF && ch != '\n');
}
void employeeInfo(input *emply)
{
int valid = 0;
do {
printf("Enter employee name.\n");
valid = scanf_s("%s", &emply->emplyName,SIZE);
clear_input();
} while (valid != 1);
do {
printf("Enter employee hours.\n");
valid = scanf_s("%f", &emply->emplyHours);
clear_input();
} while (valid != 1);
do {
printf("Enter Hourly rate.\n");
valid = scanf_s("%f", &emply->emplyRate);
clear_input();
} while (valid != 1);
calculations(emply);
}
void calculations(input *emply)
/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
if (emply->emplyHours > 40) {
emply->emplyOvrt = (emply->emplyHours - 40) * (emply->emplyRate * 1.5);
}
emply->emplyGross = (((emply->emplyHours)*(emply->emplyRate)) + emply->emplyOvrt);
emply->emplyBase = (emply->emplyGross) - (emply->emplyOvrt);
emply->emplyTax = ((emply->emplyGross)*.2);
emply->emplyNet = (emply->emplyGross) - (emply->emplyTax);
emply->emplyTotal += emply->emplyGross;
}
void print(input *employee) {
printf("Employee Name:%s\n", employee->emplyName);
printf("Hours Worked:%.2f\n ", employee->emplyHours);
printf("Hourly Rate:%.2f\n", employee->emplyRate);
printf("Gross Pay:%.2f\n", employee->emplyGross);
printf("Base Pay:%.2f\n", employee->emplyBase);
printf("Overtime Pay:%.2f\n", employee->emplyOvrt);
printf("Taxes Paid:%.2f\n", employee->emplyTax);
printf("Net Pay:%.2f\n", emplyoyee->emplyNet);
printf("Total paid to employee : %.2f\n", employee->emplyTotal);
}
void printAll(input *emply)
{
int i;
float total = 0.0;
for (i = 0; i < 5; i++)
{
// Array index beside struct name, not member
if (strcmp(emply[i].emplyName, "-1") == 0){
continue;
}
printf("Employee Name:%s\n", emply[i].emplyName);
printf("Hours Worked:%.2f\n ", emply[i].emplyHours);
printf("Hourly Rate:%.2f\n", emply[i].emplyRate);
printf("Gross Pay:%.2f\n", emply[i].emplyGross);
printf("Base Pay:%.2f\n", emply[i].emplyBase);
printf("Overtime Pay:%.2f\n", emply[i].emplyOvrt);
printf("Taxes Paid:%.2f\n", emply[i].emplyTax);
printf("Net Pay:%.2f\n", emply[i].emplyNet);
total += emply[i].emplyTotal;
}
printf("Total paid to all employees : %.2f\n", total);
}
void editEmployees(input*emply)
{
int edit;
int i;
int valid = 0;
do {
printf("which employee would you like to edit?\n");
for (i = 0; i < 5; i++) {
printf("%d.%s\n", i + 1, emply[i].emplyName);
}
valid = scanf_s("%d", &edit);
clear_input();
} while (valid != 1);
employeeInfo(&emply[edit]);
}
void menu(void)
{
printf("Main Menu\n");
printf("1. Add Employee\n");
printf("2. Edit Employee\n");
printf("3. Print Employee\n");
printf("4. Print All Employees\n");
printf("0. exit\n");
}
At first
struct person payroll[5] = { 0 };
int choice = 0, currentEmployee = 0;
here you declare array of 5 structures. And instead of
calculations(&payroll);
you should use
calculations(&payroll[0]);
or
calculations(payroll);
to pass pointer to first element of array.
Variable currentEmployee does not changes. This line does nothing
currentEmployee;
Also, your code contains a number of other problems. You should learn more about pointers and arrays in C.
Any idea on when I choose to add a client and as soon as I enter in a client ID the program crashes for that entry?
#include <stdio.h>
#include <stdlib.h>
struct client
{
int clID;
char cname;
char caddress;
char cemail;
int cfees;
int ceID;
char cename;
}typedef client;
struct employee
{
int empID;
char ename;
double erate;
double ehours;
double esalary;
int ecID;
}typedef employee;
void mainMenu();
void clientMenu();
void empMenu();
void getClient(client* pcli);
void getEmp(employee* pemp);
void payroll(employee* pemp);
void dispPay(employee* pemp);
void dispClient(client* pcli);
void dispEmployee(employee* pemp);
int main()
{
client cli[100];
client* pcli = &cli[0];
employee emp[20];
employee* pemp = &emp[0];
int answer = -1;
int mchoice;
int cchoice;
int echoice;
int ccount;
int ecount;
int input[9];
int* psearchclientID;
int i;
printf("Do you wish to start the program? 1 for yes 2 for no: ");
scanf("%d", &answer);
if(answer ==1)
{
while(mchoice != 3)
{
mainMenu();
scanf("%d", &mchoice);
switch(mchoice)
{
case 1: while(cchoice != 3)
{
clientMenu();
scanf("%d", &cchoice);
switch(cchoice)
{
case 1: getClient(pcli + i);
ccount++;
break;
case 2: printf("Enter the client ID to search for: ");
psearchclientID = fgets(input, 9, stdin);
strtok(input, "\n");
for(i = 0; i < 1; i++)
{
if(strcmpi(psearchclientID, (pcli->clID + i)) == 0)
printf("Client found at position %d\n", i);
else
printf("Client not found!");
}//end for
break;
}//end client switch
}//end client while
cchoice = 0;
break;
case 2: while(echoice != 4)
{
empMenu();
scanf("%d", &echoice);
switch(echoice)
{
case 1: getEmp(pemp + i);
ecount++;
break;
case 2: payroll(pemp + i);
dispPay(pemp + i);
break;
case 3:
break;
}//end emp switch
}//end emp while
echoice =0;
break;
}//end switch
}//end main while
}//end if
else if(answer ==2)
{
printf("Goodbye!");
exit(0);
}
return 0;
}//end main
void mainMenu()
{
printf("1-Client Menu\n"
"2-Employee Menu\n"
"3-Quit\n");
printf("Enter a choice from the menu: ");
}//end mainMenu
void clientMenu()
{
printf("1-Add a client\n"
"2-Search client\n"
"3-Go Back to Main Menu\n");
printf("Enter a choice from the menu: ");
}//end clientMenu
void empMenu()
{
printf("1-Add an Employee\n"
"2-Process an Employee(payroll)\n"
"3-Search Employee\n"
"4-Go Back to Main Menu\n");
printf("Enter a choice from the menu: ");
}//end empMenu
This is specifically the code for entering in the client info
void getClient(client* pcli)
{
printf("Enter client ID: ");
scanf("%d", &pcli->clID);
printf("Enter client name: ");
scanf("%s", &pcli->cname);
printf("Enter client address: ");
scanf("%s", &pcli->caddress);
printf("Enter client email: ");
scanf("%s", &pcli->cemail);
printf("Enter monthly service fees:" );
scanf("%d", &pcli->cfees);
}//end getClient
void getEmp(employee* pemp)
{
printf("Enter employee ID: ");
scanf("%d", &pemp->empID);
printf("Enter employee name: ");
scanf("%s", &pemp->ename);
printf("Enter employee hourly rate: ");
scanf("%lf", &pemp->erate);
printf("Enter employee hours worked: ");
scanf("%lf", &pemp->ehours);
}//end getEmp
void payroll(employee* pemp)
{
pemp->esalary = pemp->erate * pemp->ehours;
}//end payroll
void dispPay(employee* pemp)
{
printf("Employee ID %d\nEmployee Salary: %2.2f\n", pemp->empID, pemp->esalary);
}//end dispPay
This is where the information would be displayed
void dispClient(client* pcli)
{
printf("Client ID: %d\n Name: %s\n Address: %s\n Email: %s\n Monthly fees: %d\n Employee assigned: %d\n Employee name: %s\n", pcli->clID, pcli->cname, pcli->caddress, pcli->cemail, pcli->cfees, pcli->ceID, pcli->cename);
}//end dispClient
void dispEmployee(employee* pemp)
{
printf("Employee ID: %d\n Name: %s\n Hourly Rate: %2.2f\n Hours worked: %2.2f\n Salary: %2.2f\n Client(s) assigned: %s\n", pemp->empID, pemp->ename, pemp->erate, pemp->ehours, pemp->esalary, pemp->ecID);
}//end dispEmp
You don't include & (address) operator for %s (strings) while reading. For example, in function getClient, use
printf("Enter employee name: ");
scanf("%s", pemp->ename);
This is one of the problems in your program.
Your clientMenu switch statement to add a client calls:
case 1: getClient(pcli + i);
It is possible to be in that routine where i is uninitialized, so it could be anything, and very likely beyond the bounds of your 100 element array. In that case getClient will very likely be operating in memory it does not own, making your program liable to crash.
It's possible there's more than that issue at play, as well. As others have stated, more information about the crash would help.