Using a pointer in another function - Bank Program - c

I have a quick question about this code that I am writing. Referring to void RunBankMenu(int *choice) and void TransactionDecision(...), how would I use the value acquired from RunBankMenu(Choice) to set a if/else or switch statement for TransactionDecision? For example, if a user selects 1, the TransactionDecision function will use the batch of code I set the switch too. How would I pass the pointer to another function so I can read the value?
Thanks!
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define MAXCREDIT -4500
void RunBankMenu(int *choice);
void Greeting();
void AccountBalance(double account, char letter);
void TransactionDecision(int num, double *cPtr, double *sPtr, double *xPtr);
void DepositMoney(double *accountPtr);
void WithdrawMoney(double *accountPtr, char letter);
int main()
{
double checkings = 430.00;
double savings = 812.00;
double credit = -2254.00;
int NumberChoice = 0;
Greeting();
AccountBalance(checkings, savings, credit);
RunBankMenu(&NumberChoice);
printf("%d", NumberChoice);
}
void Greeting()
{
printf("Welcome to the Bank of COP 2220\n\nIt is a pleasure to manage"
" your checking, savings, and credit accounts\n");
}
void AccountBalance(double account, char letter)
{
double checkings = 430.00;
double savings = 812.00;
double credit = -2254.00;
printf("-- You currently have $%.2f in your checking account\n",checkings);
printf("-- You currently have $%.2f in your savings account\n",savings);
printf("-- You currently have $%.2f credit balance\n", credit);
}
void RunBankMenu(int *choice)
{
do{
printf("-----------------------------\n");
printf("(1) to DEPOSIT to CHECKING\n");
printf("(2) to WITHDRAW from CHECKING\n");
printf("(3) to DEPOSIT to SAVINGS\n");
printf("(4) to WITHDRAW from SAVINGS\n");
printf("(5) to DEPOSIT to CREDIT\n");
printf("(6) to TAKE an ADVANCE from CREDIT\n");
printf("(7) to TRANSFER MONEY BETWEEN ACCOUNTS\n");
printf("(8) for all ACCOUNT BALANCES\n");
printf("\n(9) QUIT\n\n");
printf("Select an option: ");
scanf("%d", &*choice);
} while (*choice <= 8);
}
void TransactionDecision(int num, double *cPtr, double *sPtr, double *xPtr)
{
int num1;
}
void DepositMoney(double *accountPtr)
{
}
void WithdrawMoney(double *accountPtr, char letter)
{
}

Perhaps this code will get you started...
#include <stdio.h>
typedef struct accounts_S
{
double checkings;
double savings;
double credit;
} accounts_T;
void DepositMoney(double *accountPtr)
{
}
void WithdrawMoney(double *accountPtr)
{
}
void TransferBetweenAccounts(accounts_T *accounts)
{
}
void AccountBalance(
accounts_T *I__accounts
)
{
printf("-- You currently have $%.2f in your checking account\n", I__accounts->checkings);
printf("-- You currently have $%.2f in your savings account\n", I__accounts->savings);
printf("-- You currently have $%.2f credit balance\n", I__accounts->credit);
return;
}
void TransactionDecision(int NumberChoice, accounts_T *accounts)
{
switch(NumberChoice)
{
case 1:
DepositMoney(&accounts->checkings);
break;
case 2:
WithdrawMoney(&accounts->checkings);
break;
case 3:
DepositMoney(&accounts->savings);
break;
case 4:
WithdrawMoney(&accounts->savings);
break;
case 5:
DepositMoney(&accounts->credit);
break;
case 6:
WithdrawMoney(&accounts->credit);
break;
case 7:
TransferBetweenAccounts(accounts);
break;
case 8:
AccountBalance(accounts);
break;
}
return;
}
void Greeting()
{
printf("Welcome to the Bank of COP 2220\n"
"\n"
"It is a pleasure to manage your checking, savings, and credit accounts\n"
);
return;
}
void RunBankMenu(
int *choice
)
{
printf("-----------------------------\n");
printf("(1) to DEPOSIT to CHECKING\n");
printf("(2) to WITHDRAW from CHECKING\n");
printf("(3) to DEPOSIT to SAVINGS\n");
printf("(4) to WITHDRAW from SAVINGS\n");
printf("(5) to DEPOSIT to CREDIT\n");
printf("(6) to TAKE an ADVANCE from CREDIT\n");
printf("(7) to TRANSFER MONEY BETWEEN ACCOUNTS\n");
printf("(8) for all ACCOUNT BALANCES\n");
printf("\n(9) QUIT\n\n");
do {
printf("Select an option: ");
scanf("%d", choice);
} while((0 == *choice) && (*choice > 9));
return;
}
int main()
{
int rCode = 0;
int NumberChoice;
accounts_T accounts =
{
.checkings = 430.00,
.savings = 812.00,
.credit = -2254.00
};
Greeting();
AccountBalance(&accounts);
do {
RunBankMenu(&NumberChoice);
TransactionDecision(NumberChoice, &accounts);
} while(9 != NumberChoice);
return(rCode);
}

Related

I cant seem to understand how to restrict my scanf to only numbers of float

#include <stdio.h>
#include <string.h>
#define mL 5
#define NL 20
#define UL 6
struct LIST
{
char n[NL];
float am;
char u[UL];
};
struct array
{
struct LIST array;
};
void addCityInformation(struct array *add, int *items);
void printCities(struct array *all, int items);
int main(void)
{
struct array shopping[mL];
int choice, nrOfItemsAdded = 0;
do
{
printf("\nWhat du you want to do?");
printf("\n1 - add grocery");
printf("\n2 - print shopping list");
printf("\n3 - exit");
printf("\nYour choice: ");
scanf("%d", &choice);
while(getchar() != '\n');
switch (choice)
{
case 1:
addCityInformation(&shopping[nrOfItemsAdded], &nrOfItemsAdded);
break;
case 2:
printCities(shopping, nrOfItemsAdded);
break;
case 3:
printf("Exiting program\n\n");
break;
default:
printf("Invalid input\n\n");
break;
}
}
while(choice != 3);
return 0;
}
int clean_stdin()
{
while (getchar()!='\n');
}
void addCityInformation(struct array *add, int *items)
{
if(*items == mL)
printf("No more space in the list\n");
else
{
printf("Enter name: ");
fgets(add->array.n, NL, stdin);
add->array.n[strlen(add->array.n)-1] = '\0';
do {
printf("Enter amount: ");
}while (scanf("%f", &add->array.am )); //loop untill other than float
getchar();
printf("Enter unit: ");
fgets((add->array.u), UL, stdin);
add->array.u[strlen(add->array.u)-1] = '\0';
(*items)++;
}
}
void printCities(struct array *all, int items)
{
printf("\n\n%-20s %-15s %-9s | %-6s\n", "Name", "amount", "unit");
printf("--------------------------------------------------------\n");
for(int i = 0; i < items; i++)
printf("%-20s %-15.1f %-9.4s \n", all[i].array.n, all[i].array.am, all[i].array.u);
}
This is my loop beside that i am only showing a part of the code. It now just continues to give enter amount and letting me register it in the struct. I want to restrict the user to only entering positive numbers and no character at all. And if he types a character it should rerun the loop even if it is 123Av123 it should run the loop and only register the correct number
Edit: now showing the whole code//loop untill other than float is what i want help with
int check=scanf("%f", &add->array.am )
if(check!=1||add->array.am<0){
printf("Incorrect input");
return 1;
}
I think that will do it.
Edit: you wanted it to rerun after so use continue; instead of return;

Passing the value from this function

#define _CRT_SECURE_NO_WARNINGS
/*
Purpose: This program allows the user to bet on horses
in a race to earn money on said wagers. I'm trying to run the configureBalance function and then add money to the balance. I'm getting an exception read access violation
*/
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH myFlush()
//Prototyping
void getChoice(char *userChoice); // main menu choice
void displayMenu(); // visual menu
void myFlush(); // flush
void configureBalance(int *balance, int *wallet, int *withdraw, int *deposit); // this function is for editing account credentials
void currentBalance(int *balance); // displays the account balance
void coolRaceVisual(); // cool looking visual
//Structs
main() {
int balance = 0, wallet = 0, withdraw = 0, deposit = 0;
char choice = ' ';
do {
getChoice(&choice);
switch (choice) {
case 'A':
configureBalance(balance, wallet, withdraw, deposit);
PAUSE;
break;
case 'B':
coolRaceVisual();
PAUSE;
break;
case 'Q':
CLS;
printf("[][][][][][][][][][][]\n");
printf("[] Goodbye ! []\n");
printf("[][][][][][][][][][][]\n");
break;
default:
printf("[][][][][][][][][][][][][][][][][][][][][][][]\n");//
printf("[] Invalid Selection! Please try again []\n");// This
prompt shows up when the user
printf("[][][][][][][][][][][][][][][][][][][][][][][]\n");//
inputs something incorrectly
PAUSE;
CLS;
break;
return;
}
} while (choice != 'Q');
PAUSE;
}//end main
void getChoice(char *userChoice) {
displayMenu();
scanf("%c", userChoice); FLUSH;
*userChoice = toupper(*userChoice);
}//end getChoice
void displayMenu() {
CLS;
printf(" Horse Derby Ticket Office \n");
printf(" \n");
printf(" A) Configure Balances. \n");
printf(" \n");
printf(" B) Watch the Race. \n");
printf(" \n");
printf(" C) View Race Records. \n");
printf(" \n");
printf(" D) Save and Quit. \n");
printf(" \n");
printf(" Q) Quit. \n");
printf(" \n");
}// end displayMenu
void myFlush() {
while (getchar() != '\n');
}//end myFlush
void configureBalance(int *balance, int *wallet, int *withdraw, int *deposit) {
CLS;
char configureMenuChoice = ' ';
printf("What service would you like? (Not FDIC Insured)\n\n");
printf("A) Add funds to your account balance.\n");
printf("B) Withdraw funds to your wallet.\n");
printf("C) Check Account Balance.\n");
printf("\n\n");
scanf("%c", &configureMenuChoice);
configureMenuChoice = toupper(configureMenuChoice);
Uppercases the choice configuring balances
if (configureMenuChoice == 'A') {
CLS;
printf("How much would you like to add to your account balance? \n");
This adds directly to the balance
scanf("%i", &deposit);
*balance = *balance + *deposit;
}
if (configureMenuChoice == 'C') {
CLS;
currentBalance(*balance); // displays current balance, made a functino so it can be used at will
}
}//end conFigureBalance
void currentBalance(int *balance) {
printf("Your current balance is: %i\n", &balance);
}//end checkBalance
Change this:
scanf("%i", &deposit);
to this:
scanf("%i", deposit);
since deposit is of type int* in that context (the body of the function configureBalance).
It's the same logic as followed here: scanf("%c", userChoice);, so I wonder how you missed it.

Menu system in c

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++
}

Trouble with a switch function menu using structs in C

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.

Unresolved External Symbol in C

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().

Resources