Banking Program, withdraw function doesn't work? (C) - 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;
}

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

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)

Program that does not return anything

I am working on a program in C that can return loan payment.
yearInt is year interest
loanAmt is total amount of loan
monthlyPay is monthly payment
numberPay is number of monthly payment
For some reasons, when I run the program, there is nothing shows up, even I type in a negative number.
Is there anyway to fix it?
#include <stdio.h>
int main(void)
{
float yearInt = -1;
int loanAmt = -1;
float monthlyPay = -1;
int numberPay = -1;
int count = 0;
while (loanAmt<0)
{
printf("Please enter valid loan value: \n");
scanf("%f", &loanAmt);
}
while (yearInt<0)
{
printf("Please enter valid yearly interest value: \n");
scanf("%f", &yearInt)
}
while (monthlyPay<0)
{
printf("Please enter valid monthly payment value: \n");
scanf("%f", &monthlyPay);
}
while (numberPay<0)
{
printf("Please enter valid number of monthly payments: \n");
scanf("%f", &numberPay);
}
if(loanAmt>monthlyPay)
{
while(count<numberPay)
{
loanAmt = loanAmt*(1 + (yearInt/12)) - monthlyPay;
count += count+1;
}
printf("The amount of last payment is: %.2f\n", loanAmt);
else
printf("The amount of last payment is: %.2f\n", loanAmt);
}
return 0;
}
You are using the wrong format specifier to read loanAmount and numberPay. Instead of "%f", use "%d".
scanf("%d", &loanAmt);
and
scanf("%d", &numberPay);
Also, always check the return value of scanf to make sure that it was able to assign data to all the variables.
Change the loop:
while (loanAmt<0)
{
printf("Please enter valid loan value: \n");
scanf("%d", &loanAmt);
}
to
while (loanAmt<0)
{
printf("Please enter valid loan value: \n");
if ( scanf("%d", &loanAmt) != 1 )
{
// Discard the rest of the line.
int c;
while ( (c = fgetc(stdin)) = '\n' && c != EOF );
}
}
It will be still better to put all the checks in a function and call the function from main.
int readInt(char const* prompt)
{
int val = -1;
printf("%s\n", prompt);
while ( scanf("%f", &val) != 1 || val < 0)
{
// Discard rest of the line.
int c;
while ( (c = fgetc(stdin)) = '\n' && c != EOF );
// If EOF is reached, we have a problem.
if ( c == EOF )
{
exit(0);
}
printf("%s\n", prompt);
}
return val;
}
and then, use:
loanAmount = readInt("Please enter valid loan value: ");
Add a similar function to read floats and call it for reading the variables that are of type float.
scanf("%f", &loanAmt); // loanAmt is int
...
scanf("%f", &numberPay); //numberPay is int
In both wrong argument is passed to %f , therefore , causes UB . Use %d specifier .
And in this one } is missing -
if(loanAmt>monthlyPay)
{
while(count<numberPay)
{
loanAmt = loanAmt*(1 + (yearInt/12)) - monthlyPay;
count += count+1;
}
printf("The amount of last payment is: %.2f\n", loanAmt); //use %d
// ADD '}' here
else // ADD '{' here
printf("The amount of last payment is: %.2f\n", loanAmt); //use %d
}
Put a } before else {.

Too few arguments to function.. What does it mean where do I go wrong?

Here's my code:
#include <stdio.h>
int validate(int low,int high);
int main ()
{
//Declare Variables
float strength, speed, defense, intelligence;
float strengthRatio, speedRatio, defenseRatio, intelligenceRatio;
int strength_F, speed_F, defense_F, intelligence_F;
float sum;
int luck;
float PStrength=10;
float PDefense=20;
float PIntelligence=40;
int PHP=10;
float EStrength=30;
float EDefense=40;
float EIntelligence=25;
int EHP=10;
int sel;
float attackp;
float magicp;
int low=1;
int high=3;
int Valid_Selection;
//Clear Screen
system("cls");
//Display Logo
printf("+----------------------+\n");
printf("| |\n");
printf("| CODE QUEST |\n");
printf("| |\n");
printf("+----------------------+\n\n");
//Main Menu
printf("--Main Menu--\n");
printf("\n");
printf("1 - New Game\n");
printf("2 - Load Game\n");
printf("3 - Exit\n");
printf("\n");
printf("Selection: \n\n");
//Validate user input using a functuion
Valid_Selection = validate(int low,int high);
//Game_Selection = validate();
printf ("Character Creation \n");
printf ("Please enter your desired stats for your character:\n");
printf ("\n");
printf ("Enter strength: ");
scanf ("%f", &strength);
printf ("Enter speed: ");
scanf ("%f", &speed);
printf ("Enter defense: ");
scanf ("%f", &defense);
printf ("Enter intelligence: ");
scanf ("%f", &intelligence);
//Calculate the sum of all attributes
sum = strength + speed + defense + intelligence;
//Calculate ratios for each attribute
strengthRatio = strength / sum;
speedRatio = speed / sum;
defenseRatio = defense / sum;
intelligenceRatio = intelligence / sum;
//Calculate the final Stats as whole number
strength_F = strengthRatio * 100;
speed_F = speedRatio * 100;
defense_F = defenseRatio * 100;
intelligence_F = intelligenceRatio * 100;
//Calculate the player's luck
luck = (int)sum % 30;
//Create an empty line for beauty
printf ("\n");
//Display to the user the finalized player stats
printf ("Your player's final stats are:");
printf ("\n");
printf ("Strength: %d\n", strength_F);
printf ("Speed: %d\n", speed_F);
printf ("Defense: %d\n", defense_F);
printf ("Intelligence: %d\n", intelligence_F);
printf ("Luck: %d\n", luck);
//Display "Battle Starts"
printf("Battle Start!\n\n");
while (PHP>=0 && EHP>=0) {
//Display user and enemy HP and asks user which attack execute
printf("Your HP: %d, Enemy HP: %d\n", PHP, EHP);
printf("1 - Attack Power\n");
printf("2 - Magic Power\n");
scanf("%d",&sel);
//Option 1, Option 2
if (sel==1){
attackp = (PStrength/EDefense)*5;
EHP = EHP - attackp;
printf("You Attacked The Enemy\n");
}
else{
magicp = (PIntelligence/EIntelligence)*5;
EHP = EHP - magicp;
printf("You bewitched the enemy!\n");
}
//Enemy reaction based on his own HP
if (EHP<=0)
printf("You Won!\n");
else {
attackp = (EStrength/PDefense)*5;
PHP = PHP - attackp;
printf("The Enemy Attacked You\n");
}
//Indicates if user lost the game
if (PHP<=0)
printf("You Lost!\n");
printf("\n");
}
return 0;
}
int validate(int low, int high) {
int s;
do{
scanf("%d", s);
if (s<1 || s>3)
printf("innvalid Input, try again:");
} while (s<1 || s>3);
return s;
}
int validate(int low, int high) {
int selection;
do {
scanf("%d", selection);
if (selection <0 || selection>3)
printf("Invalid Input, try again: ");
} while (0<selection<4);
return selection;
}
I want the user to enter a number between 1 and 3 and I need the validate function to verify it. Can anyone explain me where I go wrong please? what is the proper way to do it?
Your validate must be called as:
//Validate user input using a functuion
Valid_Selection = validate(1, 3);
as your menu has choices between 1 and 3, and your function should be:
int validate(int low, int high) {
int s=0;
char buf[128];
do {
if (fgets(buf,128,stdin)==0 || sscanf(buf, "%d", &s)!=1 || (s<low || s>high))
printf("invalid Input, try again:");
else
return s;
} while (1);
}
and there can be only one function with that name, so remove the second one. Btw, while (0<selection<4) in the second one, must be written as: while (0<selection && selection<4).
Edit: note the check for the return value of sscanf. It tells us whether sscanf could read the value type specified, %d, and note to pass the address of the integer where to store the result. The initialization of s to zero ensures the while loop will not be exited while there was invalid input.
Edit: fixed Chux's comment to consume stdin.

Payslip generation program in 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);
}
}

Resources