Payslip generation program in c - c

this is the question:
Write an interactive program to generate pay slips for the staff of size 12 employees (2 members are clerks, one computer operator, 6 salesmen, 3 helpers) , working in a small chemist retail shop. Assumptions can be made wherever necessary. The payslip should display the employee no., employee name, no. of days worked during the month, date of generation of the payslip, month for which the salary is being paid, all the details of the payment, deductions, gross-pay and net-pay.
when i run the program, it says invalid pointer, even though i havent used a pointer
i am can anyone let me know what mistake(s) are there in this program?
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char name[30][30], designation[20[20], empid[12][12];
int i;
int n = 12;
int working_days = 27;
float basic[12], days_absent[12], days_present[12], gross_salary[20], pf[12], allowance[12], net[12];
void enter_details_of_employee();
void display();
void get_time();
void main()
{
int k;
printf("Enter 1 to enter employee details and 2 to display salary\n");
scanf("%d", &k);
if(k == 1)
{
enter_details_of_employee();
}
else if(k == 2)
{
display();
get_time();
}
else
{
printf("invalid choice");
}
}
void enter_details_of_employee ()
{
int choice;
int clerk_counter = 0, operator_counter = 0, salesman_counter = 0, helper_counter = 0, max = 0;
do {
printf("\n enter details of employees\n");
printf("enter employee name\n");
scanf("%c", &name);
printf("enter employee id\n");
scanf("%c", &empid);
printf("enter your choice for employee designation\n 1.clerk \n 2.computer operator\n 3. salesman\n 4.helper\n");
scanf("%d", &choice);
if (choice == 1)
{
if(clerk_counter == 2)
{
printf("sorry, you have already entered the details of all clerks\n");
}
else
{
designation = "clerk";
basic = 8000.00;
printf("enter no of days absent\n");
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.1;
allowance = gross_salary*0.55;
net = (gross - pf) + allowance;
clerk_counter++;
}
}
else if (choice == 2)
{
if(operator_counter == 1)
{
printf("sorry, you have already entered the details of all computer operators\n");
}
else
{
designation = "computer operator";
basic = 9000;
printf("enter no of days absent\n);
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.12;
allowance = gross_salary*0.75;
net = (gross - pf) + allowance;
operator_counter++;
}
}
else if (choice == 3)
{
if(salesman_counter == 6)
{
printf("sorry, you have already entered the details of all salesman\n");
}
else
{
designation = "salesman";
basic = 10000;
printf("enter no of days absent\n);
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.15;
allowance = gross_salary*0.95;
net = (gross - pf) + allowance;
salesman_counter++;
}
}
else if (choice == 4)
{
if(salesman_counter == 3)
{
printf("sorry, you have already entered the details of all helpers\n");
}
else
{
designation = "helper";
basic = 6500;
printf("enter no of days absent\n);
scanf("%d", &days_absent);
days_present = working_days - days_absent;
gross_salary = basic - ((days_absent / working_days) * basic);
pf = gross_salary*0.08;
allowance = gross_salary*0.45;
net = (gross - pf) + allowance;
helper_counter++;
}
}
else
{
printf("invalid choice");
}
}
while (max!=12);
}
void get_time()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}
void display()
{
printf("SALARY SLIP OF EMPLOYEES ";
printf("---------------------------------------------------------------------------------------------------");
printf("empid\t name\t days_absent\t days_present\t gross_salary\t PF\t allowance\t net");
printf("---------------------------------------------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf(empid[i][i] name[i][i] basic[i] days_absent[i] days_present[i] gross_salary[i] pf[i] allowance[i] net);
}
}

Errors:
1) Syntax Error 1:
char name[30][30], designation[20[20], empid[12][12]; // Bracket mismatch
2) Syntax Error 2:
char designation[20[20];
designation = "clerk"; // What is this?? ..the invalid pointer error
3) Find rest yourself or try compiling with -Wall option.

here is a modified program, any feedback??
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
typedef struct mystruct{
char name[30];
char designation[20];
char empid[8];
float basic;
float days_absent;
float days_present;
float gross_salary;
float pf;
float allowance;
float net;
int counter;
int clerk_counter;
int operator_counter;
int salesman_counter;
int helper_counter;
char *next;
}mystruct;
char *p;
void enter_details_of_employee();
void display();
void get_time();
void main()
{
do
{
int k, choice;
printf("Enter 1 to enter employee details and 2 to display salary\n");
scanf("%d", &k);
if(k == 1)
{
p = (mystruct*)malloc(sizeOf(mystruct));
enter_details_of_employee();
}
else if(k == 2)
{
display();
get_time();
}
else
{
choice = 0;
printf("invalid choice, press 1 to continue\n");
}
}
while choice == 1;
}
void enter_details_of_employee ()
{
if(p->mystruct.counter!=12)
{
int proceed = 1;
do
{
printf("\n enter details of employees\n");
printf("enter employee name\n");
scanf("%u", &p->mystruct.name);
printf("enter employee id\n");
scanf("%u", &p.empid);
printf("enter your choice for employee designation\n 1.clerk \n 2.computer operator\n 3. salesman\n 4.helper\n");
scanf("%d", &choice);
if (choice == 1)
{
if(p->mystruct.clerk_counter == 2)
{
printf("sorry, you have already entered the details of all clerks\n");
}
else
{
p->mystruct.designation = "clerk";
p->mystruct.basic = 8000.00;
printf("enter no of days absent\n");
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.1;
p->mystruct.allowance = p->mystruct.gross_salary*0.55;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.clerk_counter++;
p->mystruct.counter++;
}
}
else if (choice == 2)
{
if(p->mystruct.operator_counter == 1)
{
printf("sorry, you have already entered the details of all computer operators\n");
}
else
{
p->mystruct.designation = "computer operator";
p->mystruct.basic = 9000;
printf("enter no of days absent\n);
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.12;
p->mystruct.allowance = p->mystruct.gross_salary*0.75;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.operator_counter++;
p->mystruct.counter++;
}
}
else if (choice == 3)
{
if(p->mystruct.salesman_counter == 6)
{
printf("sorry, you have already entered the details of all salesman\n");
}
else
{
p->mystruct.designation = "salesman";
p->mystruct.basic = 10000;
printf("enter no of days absent\n");
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.15;
p->mystruct.allowance = p->mystruct.gross_salary*0.95;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.salesman_counter++;
p->mystruct.oounter++;
}
}
else if (choice == 4)
{
if(p.->mystruct.salesman_counter == 3)
{
printf("sorry, you have already entered the details of all helpers\n");
}
else
{
p->mystruct.designation = "helper";
p->mystruct.basic = 6500;
printf("enter no of days absent\n);
scanf("%u", &p->mystruct.days_absent);
p->mystruct.days_present = p->mystruct.working_days - p->mystruct.days_absent;
p->mystruct.gross_salary = p->mystruct.basic - ((p->mystruct.days_absent / p->mystruct.working_days) * p->mystruct.basic);
p->mystruct.pf = p->mystruct.gross_salary*0.08;
p->mystruct.allowance = p->mystruct.gross_salary*0.45;
p->mystruct.net = (p->mystruct.gross - p->mystruct.pf) + p->mystruct.allowance;
p->mystruct.helper_counter++;
p->mystruct.counter++;
}
}
else
{
printf("invalid choice, press 1 to continue\n");
scanf("%d", &proceed);
}
while (proceed);
}
}
else
{
printf("data entry complete");
}
}
}
void get_time()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}
void display()
{
printf("SALARY SLIP OF EMPLOYEES ";
printf("---------------------------------------------------------------------------------------------------");
printf("empid\t name\t days_absent\t days_present\t gross_salary\t PF\t allowance\t net");
printf("---------------------------------------------------------------------------------------------------");
while(p.next!=NULL);
{
print(p.mystruct);
}
}

Related

while loop not breaking even with break C programming

Trying to make a GPA calculator.
Here is my code:
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
float fgpa, grade, n;
char reply;
n = 1;
grade = 1;
printf("--- GPA Calculator ---");
while(fgpa < 1 || fgpa > 4)
{
printf("\n\nEnter your current GPA: ");
scanf("%f", &fgpa);
if(fgpa < 1 || fgpa > 4)
printf("Invalid Input! Please re-enter value.");
}
printf("\nUsing the following table to convert grade to points\n\nA+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\nC = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
while(grade > 0, n++)
{
printf("\nEnter points for module %.0f, enter \"0\" if there are no more additional modules: ", n);
scanf("%f", &grade);
printf("%f", grade);
fgpa = (fgpa + grade) / n;
}
fgpa = fgpa* n / (n-1);
n--;
printf("\n\nNumber of modules taken: %.0f\nFinal GPA: %.1f", n, fgpa);
return 0;
}
I've tried using if(grade = 0) break; but its still not breaking the loop even when the grade is correctly read 0.
picture of 0 being read correctly but loop still continuing
There are multiple problems in the code:
fgpa is uninitialized so the first test in the loop has undefined behavior.
you should also test the return value of scanf() to detect invalid or missing input.
while (grade > 0, n++) is incorrect too: you should instead always read the next grade and test its value and break from the loop before incrementing n.
Your averaging method seems incorrect too: you do not give the same weight to every module.
It seems more appropriate for your purpose to use for ever loops (for (;;)), unconditionally read input, check for scanf() success and test the input values explicitly before proceeding with the computations.
Here is a modified version:
#include <stdio.h>
// flush the rest of the input line, return EOF at end of file
int flush(void) {
int c;
while ((c = getchar()) != EOF && c != \n')
continue;
return c;
}
int main() {
float fgpa;
float grade;
int n = 1;
char reply;
printf("--- GPA Calculator ---");
for (;;) {
printf("\n\nEnter your current GPA: ");
if (scanf("%f", &fgpa) == 1) {
if (fgpa >= 1 && fgpa <= 4)
break;
}
} else {
if (flush() == EOF) {
fprintf(stderr, "unexpected end of file\n");
return 1;
}
}
printf("Invalid Input! Please re-enter value.\n");
}
printf("\nUsing the following table to convert grade to points\n\n"
"A+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\n"
"C = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
for (;;) {
printf("\nEnter points for module %d, enter \"0\" if there are no more additional modules: ", n);
if (scanf("%f", &grade) == 1) {
if (grade <= 0)
break;
printf("%f\n", grade);
fgpa = fgpa + grade;
n = n + 1;
} else {
if (flush() == EOF)
break;
printf("Invalid Input! Please re-enter value.\n");
}
}
fgpa = fgpa / n;
printf("\n\nNumber of modules taken: %d\nFinal GPA: %.3f\n", n, fgpa);
return 0;
}

cannot write/'w' FILE in function

I don't know what is wrong with this coding. I even use 'a' to output the display to for Name, Address, and Phone Number. for Read/'r' FILE my coding succefully run without any error but when try to Write FILE suddenly it cannot open the file or NULL.
This is a snapshot of the error
This is the function that I want to diplay the output in FILE of output.txt
void display(char cName[], char cAddress[], char cPhoneNo[])
{
output = fopen("output.txt", "w");
printf("\n_________________________________________________");
printf("\n_________________________________________________");
fprintf(output, "\n\n Name\t\t: %s", cName);
fprintf(output, " Address\t: %s", cAddress);
fprintf(output, " PhoneNo\t: %s", cPhoneNo);
fclose(output);
}
This is the whole coding
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void cookiesList(char[20][5], char* [], float*);
void infoInput(char[], char[], char[]);
int order(char* [], int*);
void display(char[], char[], char[]);
void outputDisplay(char[20][5], char[20][40], int[], float[], float, size_t, float, float, float, float);
float calculateSubTotal(float, int, float*);
float bundle(char*, char*, float);
FILE* input, * output;
int main()
{
char customerName[50], customerAddress[70], customerPhoneNo[12];
char productCode[20][5], productName[20][40];
int productQuantity[20], decision, orderSubTotal=0;
float productPrice[20], productPriceTotal[20];
size_t counter = 0;
float cashback, discount, finalprice;
char bundleName[10], bundleItem[32];
float bundlePrice;
printf("\t\t\t#-------------------------------------------------------#");
printf("\n\t\t\t|\t WELCOME TO ONLINE COOKIES PEOPLE STORE \t|");
printf("\n\t\t\t#-------------------------------------------------------#\n");
infoInput(customerName, customerAddress, customerPhoneNo);
do
{
decision = order(&productCode[counter], &productQuantity[counter]);
cookiesList(productCode[counter], &productName[counter], &productPrice[counter]);
orderSubTotal = orderSubTotal + calculateSubTotal(productPrice[counter], productQuantity[counter], &productPriceTotal[counter]);
counter++;
} while (decision == 1);
bundlePrice = bundle(&bundleName, &bundleItem, orderSubTotal);
if ((15 <= orderSubTotal) && (orderSubTotal < 76))
{
cashback = 1.5;
discount = 0;
}
else if ((76 <= orderSubTotal) && (orderSubTotal < 151))
{
cashback = 2.5;
discount = orderSubTotal * 0.05;
}
else if (151 <= orderSubTotal)
{
cashback = 3.5;
discount = orderSubTotal * 0.1;
}
finalprice = orderSubTotal - discount + bundlePrice;
display(customerName, customerAddress, customerPhoneNo);
outputDisplay(productCode, productName, productQuantity, productPriceTotal, orderSubTotal, counter
, discount, cashback, finalprice, bundlePrice);
return 0;
}
void cookiesList(char pdtCode[][5], char* pdtNm[], float* pdtprice)
{
if ((strcmp(pdtCode, "c111") == 0) || (strcmp(pdtCode, "C111") == 0))
{
strcpy(pdtNm, "Dark Chocolate Chips");
*pdtprice = 15.0;
}
else if ((strcmp(pdtCode, "c112") == 0) || (strcmp(pdtCode, "C112") == 0))
{
strcpy(pdtNm, "Premium Assorted Shortbread Cookies");
*pdtprice = 25.5;
}
else if ((strcmp(pdtCode, "c113") == 0) || (strcmp(pdtCode, "C113") == 0))
{
strcpy(pdtNm, "Hawaiian Salted Coconut");
*pdtprice = 17.5;
}
else if ((strcmp(pdtCode, "c114") == 0) || (strcmp(pdtCode, "C114") == 0))
{
strcpy(pdtNm, "Oatmeal & Raisins Cookies");
*pdtprice = 13.0;
}
else if ((strcmp(pdtCode, "c115") == 0) || (strcmp(pdtCode, "C115") == 0))
{
strcpy(pdtNm, "Chocolate Caramel Almond Cookies");
*pdtprice = 16.0;
}
else
{
exit(cookiesList);
}
}
void infoInput(char cNm[], char cAdrs[], char cPhN[])
{
input = fopen("file.txt", "r");
if (input == NULL) {
printf("ERROR! File does not have any input");
return(-1);
}
else {
//fgets(cNm, 50, fp);
fscanf(input, "\n%[^\n]%*c", cNm);
printf("\n Name entered\t\t: %s", cNm);
//fgets(cAdrs, 70, fp);
fscanf(input, "\n%[^\n]%*c", cAdrs);
printf("\n Address entered\t: %s", cAdrs);
//fgets(cPhN, 12, fp);
fscanf(input, "\n%[^\n]%*c", cPhN);
printf("\n Phone No. entered\t: %s", cPhN);
printf("\n ----------------Product-------------------");
printf("\n C111: Dark Chocolate Chips");
printf("\n C112: Premium Assorted Shortbread Cookies");
printf("\n C113: Hawaiian Salted Coconut");
printf("\n C114: Oatmeal & Raisins Cookies");
printf("\n C115: Chocolate Caramel Almond Cookies");
printf("\n ------------------------------------------\n");
}
fclose(input);
}
int order(char* pdtCdLt[], int* pdtQtyLt)
{
int YorN;
printf(" Enter Product Code\t: ");
scanf(" %s", pdtCdLt);
printf(" Enter Quantity\t\t: ");
scanf(" %d", pdtQtyLt);
printf(" Want to add other product?( Yes = 1 | No = 0 ) ");
scanf(" %d", &YorN);
return YorN;
}
void display(char cName[], char cAddress[], char cPhoneNo[])
{
output = fopen("output.txt", "w");
printf("\n_________________________________________________");
printf("\n_________________________________________________");
fprintf(output, "\n\n Name\t\t: %s", cName);
fprintf(output, " Address\t: %s", cAddress);
fprintf(output, " PhoneNo\t: %s", cPhoneNo);
fclose(output);
}
void outputDisplay(char pdtCd[][5], char pdtName[][40], int pdtQty[], float pdtPriceTtl[], float odrSubTotal, size_t index
, float disc, float CashB, float Fprice, float bdlPri)
{
//yolo = fopen("output.txt", "w");
/*if (yolo == NULL) {
printf("Could Not open file!");
return 0;
}*/
printf("\n Item\t\tQuantity\t\tPrice(RM)\n");
for (size_t i = 0; i < index; i++)
{
printf("\n %s", pdtName[i]);
printf("\n %s\t\t%d\t\t\t%.2f", pdtCd[i], pdtQty[i], pdtPriceTtl[i]);
}
printf("\n\n\t\t\t\t\t============");
printf("\n\t\t\t Subtotal\t%.2f", odrSubTotal);
if (bdlPri > 0)
{
printf("\n\t\t Bundle Price\t%.2f", bdlPri);
}
if (disc > 0)
{
printf("\n\t\t\t Discount\t%.2f", disc);
}
printf("\n\t\t\t CashBack\t%.2f", CashB);
printf("\n\t\t\tFinal price\t%.2f\n", Fprice);
//fclose(yolo);
}
float calculateSubTotal(float pdtPri, int pdtQuanti, float* pdtPTtl)
{
float subTtl;
*pdtPTtl = pdtPri * ((float)pdtQuanti);
return *pdtPTtl;
}
float bundle(char* bdlNm, char* bdlItm, float odrSubTtl)
{
int yorn;
float bdlPrice;
printf("\n Do you want to choose any bundle?");
printf("\n *Every bundle is RM 5 each.( Yes = 1 | No = 0 ) ");
scanf(" %d", &yorn);
if (yorn == 1)
{
printf("\n\t#-----------------------------Type of Bundle----------------------------#");
printf("\n\t|\t\t\t|\t\t\t|\t\t\t|");
printf("\n\t|\t Bundle A \t|\t Bundle B \t|\t Bundle C \t|");
printf("\n\t|\tRecycle Bag\t|\tRecycle Bag\t|\tRecycle Bag\t|");
printf("\n\t|\t & \t|\t & \t|\t & \t|");
printf("\n\t|\t NoteBook \t|\tWater Bottle\t|\tMark Cup\t|");
printf("\n\t|_______________________|_______________________|_______________________|\n");
retry:
printf("\n A = Bundle A, B = Bundle B, C = Bundle C ");
printf("\n Choose Your wanted Bundle : ");
fflush(stdin);
scanf(" %c", &(*bdlNm));
if ((*bdlNm == 'a') || (*bdlNm == 'A'))
{
strcpy(bdlItm, "Recycle bag and Notebook");
}
else if ((*bdlNm == 'b') || (*bdlNm == 'B'))
{
strcpy(bdlItm, "Recycle Bag and Water Bottle");
}
else if ((*bdlNm == 'c') || (*bdlNm == 'C'))
{
strcpy(bdlItm, "Recycle bag and Mark Cup");
}
else
{
printf("\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#");
printf("\n{\t\t\t\t}");
printf("\n }---------ERROR INPUT---------{");
printf("\n{\t\t\t\t}");
printf("\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#");
}
bdlPrice = 5;
}
else
{
bdlPrice = 0;
}
return bdlPrice;
}
What are your OS and IDE?
I think that you are using Windows & VS now.
If you are running this program in Windows, Please start IDE with "Run as Administrator" like below.

Deposit gives negative number?

Good morning, guys! I'm currently a newly started programming learner. Down here is my code for a mini app store. However, there is a problem going on, yet I couldnt locate the problem.
The problem happen when I tried buying an app for $89.99 and I chose to redeem $10 9 times so I would have enough money to purchase the app (I didnt choose the $100 option because it would work just fine). However, the remained amount became $-79.99 instead of $0.01. Like I said, if I chose to deposit $100, the remained balance would be $10.01, which is normal. I don't get where I did wrong. Here is my code!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int Compare(double deposit, double choiceCost);
void DisplayApps(char *selectionPtr);
void SetCost(char selection, double *costPtr);
void PaymentOptions(double *depositPtr,double cost);
void DoItAgain(char *quitPtr);
//void Pay(double *depositPtr, double choiceCost);
void GetChange(double *depositPtr, double choiceCost);
void DoItAgain(char *quitPtr);
int main()
{
char selectionPtr;
char selection;
char quitPtr;
double costPtr;
double deposit = 0.0;
double choiceCost;
double depositPtr = 0.0;
double cost = 0.0;
printf("Welcome to the App Store\n");
printf("***************************\n\n");
printf("Your deposit is: %.2f\n", deposit);
printf("\n");
while (1)
{
do {
DisplayApps(&selectionPtr);
selection = selectionPtr;
SetCost(selection, &costPtr);
choiceCost = costPtr;
Compare(deposit, choiceCost);
while (Compare(deposit, choiceCost) == 0)
{
printf("Your balance isn't enough.\n");
printf("In order to purchase this item, you have to redeem more money.\n");
PaymentOptions(&depositPtr, cost);
deposit += depositPtr;
printf("You have redeemed $%.2f\n", depositPtr);
printf("Your balance now is: $%.2f\n", deposit);
printf("\n");
}
deposit = depositPtr;
GetChange(&depositPtr, choiceCost);
DoItAgain(&quitPtr);
} while (quitPtr == 'Y' || quitPtr == 'y');
return 1;
}
return 0;
}
void DisplayApps(char *selectionPtr)
{
printf("-------------------------\n");
printf("HERE ARE THE SLECTIONS\n");
printf("C -- Clown Punching $299.99\n");
printf("V -- Virtual Snow Globe $349.99\n");
printf("R -- Remote PC $999.99\n");
printf("G -- Grocery List Helper $2.99\n");
printf("M -- Mobile Cam Viewer $89.99\n");
printf("\n");
printf("Please enter a selection: ");
scanf(" %c", &*selectionPtr);
printf("\n");
}
void SetCost(char selection, double *costPtr)
{
if (selection == 'C' || selection == 'c')
{
*costPtr = 299.99;
}
else if (selection == 'V' || selection == 'v')
{
*costPtr = 349.99;
}
else if (selection == 'R' || selection == 'r')
{
*costPtr = 999.99;
}
else if (selection == 'G' || selection == 'g')
{
*costPtr = 2.99;
}
else if (selection == 'M' || selection == 'm')
{
*costPtr = 89.99;
}
}
int Compare(double deposit, double choiceCost)
{
if (deposit < choiceCost)
{
return 0;
}
else
{
return 1;
}
}
void PaymentOptions(double *depositPtr, double cost)
{
printf("You have (4) options to choose:\n");
printf("1 - $1000.00\n");
printf("2 - $500.00\n");
printf("3 - $100.00\n");
printf("4 - $10.00\n");
printf("How much do you want to redeem?");
printf(">>>>> ");
scanf("%lf", &cost);
printf("\n");
printf("-------------------------------------\n");
if (cost == 1)
{
*depositPtr = 1000.00;
}
else if (cost == 2)
{
*depositPtr = 500.00;
}
else if (cost == 3)
{
*depositPtr = 100.00;
}
else if (cost == 4)
{
*depositPtr = 10.00;
}
}
void GetChange(double *depositPtr, double choiceCost)
{
*depositPtr = *depositPtr - choiceCost;
printf("You have purchased this item successfully.\n");
printf("You still have $%.2f remained in you balance.\n", *depositPtr);
}
void DoItAgain(char *quitPtr)
{
printf("Do you want to continue purchase another item? [Y/N]\n");
scanf(" %c", &*quitPtr);
}
In this code : GetChange(&depositPtr, choiceCost);
You shold pass deposit (total deposit) and not &depositPtr (last deposit, only 10)

Banking Program, withdraw function doesn't work? (C)

for some reason I can't get the withdraw function to work in my banking program... I thought I was doing it correct as the functions can be very similar to the deposit function.
The problem is I can't seem to get a withdraw amount without the program even building correctly. The withdraw function should be very similar to the deposit function correct? Because instead of depositing a value into the account, you're simply withdrawing an amount from the given account. If the account doesn't have enough money, then it returns as an error. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUMCUSTOMERS 10 // Maximum number of customers
#define MAXCUSTOMERNAME 20 // Max length of customer's name including null character at the end
// Function Prototypes
int DisplayMenu();
int userLogin(int acctNumber[MAXNUMCUSTOMERS]);
float acctBalance (int custIndex, float money[MAXNUMCUSTOMERS]);
float depositMoney (int custIndex, float money[MAXNUMCUSTOMERS]);
float withdrawFunds (int custIndex, float money[MAXNUMCUSTOMERS]);
/*
*
*/
int main(int argc, char** argv) {
int acctNumber[MAXNUMCUSTOMERS] =
{1111,2222,3333,4444,5555,6666,7777,8888,9999,10000};
char customerName[MAXNUMCUSTOMERS][MAXCUSTOMERNAME] =
{"Joe","Mary","bob","tim","sid","will","ray","les","quinn","helen"};
float money[MAXNUMCUSTOMERS] =
{12.50,25.30,69.3,46.0,777.00,10000.,55.6,33.78,99.54,47895.33};
int option = 0;
int myAcct = -1;
float myBalance = 0.0;
// loop until done
while (option != 6) {
// Display menu - return option
option = DisplayMenu();
if (option == 0) { // customer login
myAcct = userLogin(acctNumber);
if (myAcct == -1) {
printf("Invalid account\n");
} else {
printf ("Welcome %s \n", customerName[myAcct]);
}
} else if (option == 1) { // deposit money
myBalance = depositMoney (myAcct, money);
if (myBalance < 0.0)
printf("Account not found\n");
else
printf ("Your new account balance is: %.2f\n", myBalance);
} else if (option == 2) { // withdraw money
if (myBalance > 0.0)
printf("Account not found\n");
else
printf ("Your new account balance is: %.2f\n", myBalance);
} else if (option == 3) { // account balance
myBalance = acctBalance (myAcct, money);
if (myBalance < 0.0)
printf("Account not found\n");
else
printf ("Your account balance is: %.2f\n", myBalance);
} else if (option == 4) { // add a customer
} else if (option == 5) { // delete a customer
} else if (option == 6) { // exit
}
// exit program
} //end loop until done
return (EXIT_SUCCESS);
}
int DisplayMenu() {
int customerChoice;
printf("Welcome to My Bank\n");
printf("Enter an option from the menu\n");
printf("\t 0 Customer Login\n");
printf("\t 1 Deposit Money Login\n");
printf("\t 2 Withdraw Money\n");
printf("\t 3 Account Balance\n");
printf("\t 4 Add a customer\n");
printf("\t 5 delete a customer\n");
printf("\t 6 Exit\n");
scanf("%d", &customerChoice);
return (customerChoice);
}
int userLogin(int acctNumber[MAXNUMCUSTOMERS])
{
int customerIndex = -1;
int accountNum;
int i;
// get the account number
printf("Please enter your account number>");
scanf("%d", &accountNum);
// loop to find which index has customer information
for (i=0; i < MAXNUMCUSTOMERS; i++)
{
if (accountNum == acctNumber[i])
customerIndex = i;
} // end loop
return customerIndex;
}
float acctBalance (int custIndex, float money[MAXNUMCUSTOMERS])
{
float balance = -1.0;
if (custIndex >= 0)
balance = money[custIndex];
return balance;
}
float depositMoney (int custIndex, float money[MAXNUMCUSTOMERS])
{
float balance = -1.0;
float deposit;
if (custIndex >= 0)
{
// get the deposit amount
printf ("Enter Deposit Amount>");
scanf ("%f", &deposit);
money[custIndex] = money[custIndex] + deposit;
balance = money[custIndex];
}
return balance;
}
//my problem is down here. This section doesn't work???
float withdrawFunds (int custIndex, float money[MAXNUMCUSTOMERS];
{
float balance = -1.0;
float withdraw;
if (custIndex >= 0)
{
//get withdraw amount
printf ("Enter Withdraw Amount>");
scanf ("%f", %withdraw);
money[custIndex] = withdraw - money[custIndex];
balance = money[custIndex];
}
return balance;
}
Try this. ( checking whether the account has enough money isn't implemented here)
float withdrawFunds (int custIndex, float money[MAXNUMCUSTOMERS]) // ; -> )
{
float balance = -1.0;
float withdraw;
if (custIndex >= 0)
{
//get withdraw amount
printf ("Enter Withdraw Amount>");
scanf ("%f", &withdraw); // % -> &
money[custIndex] = money[custIndex] - withdraw ; // correct the formula
balance = money[custIndex];
}
return balance;
}

Error: expected ';' before '{' token

I was recently coding a short little fallout shelter survival game and I came across the error "expected ';' before '{' token". The error is around line 28, character 23:
#include <stdio.h>
int main(int argc, char *argv[])
{//setup changing variables
int X = 0;
int money = 300;
int resources[10];
resources[0] = 100;
resources[1] = 200;
printf("Welcome!\nFirst we will choose the extra supplies for your shelter!");
while(X < 1)
{
//Setup the input
char input[15];
//Introduction:
printf("You have $%d dollars\n1: Cards ($4)\n2: Ounce of Gold ($20)\n3: 10 Pounds of Spare Parts($20)\n4: Ham Radio ($75)\n5: Calendar ($5)\n6: Book of Blueprints ($10)\n7: (2 Gallons) Extra Water Storage ($6)\n8: (1 gallon) Fuel ($10)\n9: Check Current Supplies\n10:Start\n", money);
//Have player buy Items
if(input == 1){
if(money > 3){
money = money - 4;
resources[2] = resources[2] + 1;
printf("You have purchased a deck of cards...\n");
}
else{
printf("You can not afford this item\n");
}
}
elif(input == 2)
{
if(money > 19)
{
money = money - 20;
resources[3] = resources[3] + 1;
printf("You have purchased an Ounce of Gold...\n");
}
else
{
printf("You do not have enough money\n");
}
}
elif(input == 3){
if(money > 19){
money = money - 20;
resources[4] = resources[4] + 10;
printf("You have purchased 10 pounds of Scrap Parts...\n");
}
else{
printf("You do not have enough money\n");
}
}
elif(input == 4){
if(money > 74){
money = money - 75;
resources[5] = resources[5] + 1;
printf("You have purchased 1 Ham Radio...\n");
}
else{
printf("You do not have enough money.\n");
}
}
elif(input == 5)
{
if(money > 4)
{
money = money - 5;
recources[6] = resources[6] + 1;
printf("You have just purchased a calendar...\n");
}
else
{
printf("You can not afford the calendar.\n");
}
}
elif(input == 6)
{
if(money > 9){
money = money - 10;
resources[7] = resources[7] + 1;
printf("You have just purchased a Book filled with Blueprints...\n");
}
else{
printf("You can not afford the Book of Blueprints.\n");
}
}
elif(input == 7){
if(money > 5){
int money = money - 6;
int resources[8] = resources[8] + 2;
printf("You have purchased 2 extra gallons of water storage...");
}
else{
printf("You do not have enough money...");
}
}
elif(input == 8){
if(money > 9){
money = money - 10;
resources[9] = resources[9] + 1;
printf("You have purchased one gallon of fuel...");
}
else{
printf("You can not afford the fuel...");
}
}
elif(input == 9){
printf("You currently have: \n%s Pounds of Food\n%s Gallons of Water\n%s Decks of Cards\n%s Ounces of Gold\n%s Pounds of Spare Parts\n%s Ham Radios\n%s Calendars\n%s Books of Blueprints\n%s Gallons of Fuel", resources[0], resources[8] + resources[1], resources[2], resources[3], resources[4], resources[5], resources[6], resources[7], resources[9]);
fgets(input, sizeof(input), stdin);
}
elif(input == 10){
break;
}
else
{
printf("Input Invalid");
fgets(input, sizeof(input), stdin);
}
}
int h = 100;
while(int h > 0)
{
printf("%d", h);
h = h - 1;
}
}
You cannot use elif in C. You should use else if instead.
In printf, %s is not for printing integers. You should use %d instead.
With char input[15];, comparations like input == 1 make no sense. You may use int char;.
input is uninitialized. You should read some data to that.
recources is undefined. It should be typo of resources.
I changed the type of input to int and put scanf before the branches, so remove fgets(input, sizeof(input), stdin);
Remove excess ints in int money = money - 6;, int resources[8] = resources[8] + 2; and while(int h > 0)
The arguments aren't used, so change int main(int argc, char *argv[]) into int main(void). (optional to avoid warnings)
OK, now compile errors are gone.
#include <stdio.h>
int main(void)
{//setup changing variables
int X = 0;
int money = 300;
int resources[10];
resources[0] = 100;
resources[1] = 200;
printf("Welcome!\nFirst we will choose the extra supplies for your shelter!");
while(X < 1)
{
//Setup the input
int input;
//Introduction:
printf("You have $%d dollars\n1: Cards ($4)\n2: Ounce of Gold ($20)\n3: 10 Pounds of Spare Parts($20)\n4: Ham Radio ($75)\n5: Calendar ($5)\n6: Book of Blueprints ($10)\n7: (2 Gallons) Extra Water Storage ($6)\n8: (1 gallon) Fuel ($10)\n9: Check Current Supplies\n10:Start\n", money);
//Have player buy Items
if(scanf("%d", &input) != 1) return 1;
if(input == 1){
if(money > 3){
money = money - 4;
resources[2] = resources[2] + 1;
printf("You have purchased a deck of cards...\n");
}
else{
printf("You can not afford this item\n");
}
}
else if(input == 2)
{
if(money > 19)
{
money = money - 20;
resources[3] = resources[3] + 1;
printf("You have purchased an Ounce of Gold...\n");
}
else
{
printf("You do not have enough money\n");
}
}
else if(input == 3){
if(money > 19){
money = money - 20;
resources[4] = resources[4] + 10;
printf("You have purchased 10 pounds of Scrap Parts...\n");
}
else{
printf("You do not have enough money\n");
}
}
else if(input == 4){
if(money > 74){
money = money - 75;
resources[5] = resources[5] + 1;
printf("You have purchased 1 Ham Radio...\n");
}
else{
printf("You do not have enough money.\n");
}
}
else if(input == 5)
{
if(money > 4)
{
money = money - 5;
resources[6] = resources[6] + 1;
printf("You have just purchased a calendar...\n");
}
else
{
printf("You can not afford the calendar.\n");
}
}
else if(input == 6)
{
if(money > 9){
money = money - 10;
resources[7] = resources[7] + 1;
printf("You have just purchased a Book filled with Blueprints...\n");
}
else{
printf("You can not afford the Book of Blueprints.\n");
}
}
else if(input == 7){
if(money > 5){
money = money - 6;
resources[8] = resources[8] + 2;
printf("You have purchased 2 extra gallons of water storage...");
}
else{
printf("You do not have enough money...");
}
}
else if(input == 8){
if(money > 9){
money = money - 10;
resources[9] = resources[9] + 1;
printf("You have purchased one gallon of fuel...");
}
else{
printf("You can not afford the fuel...");
}
}
else if(input == 9){
printf("You currently have: \n%d Pounds of Food\n%d Gallons of Water\n%d Decks of Cards\n%d Ounces of Gold\n%d Pounds of Spare Parts\n%d Ham Radios\n%d Calendars\n%d Books of Blueprints\n%d Gallons of Fuel", resources[0], resources[8] + resources[1], resources[2], resources[3], resources[4], resources[5], resources[6], resources[7], resources[9]);
}
else if(input == 10){
break;
}
else
{
printf("Input Invalid");
}
}
int h = 100;
while(h > 0)
{
printf("%d", h);
h = h - 1;
}
}
Enjoy!
Sorry, I didn't enjoy but got annoyed.
In c elif is not there. So, it gives that error.
Change the elif to else if and do validations.

Resources