cannot write/'w' FILE in function - c

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.

Related

Unresolved external symbol in c for user-define function

im very new to programming and for my course we learning c language so have mercy on my badly written code and the compiler just wont run the program because of the error so its hard for me to identify whats wrong.Back to main question im getting unresolved external symbol...referenced in function_main and i dont really know where im messed up
#include<stdio.h>
char DisplayMenu(void);
void ViewBooks();
void SearchBookPrice();
void UpdateBookPrice();
int main()
{
char userchoice;
int bookID[5] = { 200,230,500,540,700 };
char bookTitle[5][50] = { {"Using Information Technology 12th Edition" }, { "Beyond HTML" },{"Build Your Own PC"},{"Instant Java Servlets"},{"DIgital Image: A Practical Guide"} };
float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };
do
{
userchoice = DisplayMenu();
if (userchoice == 'V')
ViewBooks();
else if (userchoice == 'S')
SearchBookPrice();
else if (userchoice == 'U')
UpdateBookPrice();
} while (userchoice != 'E');
printf("Thank you for using our system. Have a nice day!\n");
return 0;
}
char DisplayMenu(void)
{
char choice;
printf("*************************************************************************\n");
printf("V:View Books S:Search Book Price U:Update Book Price E:Exit");
printf("*************************************************************************\n");
do
{
printf("Enter your choice: ");
scanf(" %c", &choice);
if (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E')
printf("Invalid Choice\n");
} while (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E');
return choice;
}
void ViewBooks(int bookID[],float bookPrice[])
{
printf("%d Using Information Technology 12th Edition $%f\n", bookID[0], bookPrice[0]);
printf("%d Beyond HTML $%f\n", bookID[1], bookPrice[1]);
printf("%d Build Your Own PC $%f\n", bookID[2], bookPrice[2]);
printf("%d Instant Java Servlets $%f\n", bookID[3], bookPrice[3]);
printf("%d Digital Image: A Pratical Guide $%f\n", bookID[4], bookPrice[4]);
return;
}
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i= 0, match = -1;
printf("*************************************************************************\n");
printf("Search by book ID\n");
printf("*************************************************************************\n");
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i<5 && match==-1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("Please refer to the customer service for assitance");
else
{
printf("The book id is : %d\n", &idsearch);
printf("The price of %s is %f", bookTitle[match], bookPrice[match]);
}
return;
}
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i = 0, match = -1;
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i < 5 && match == -1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("The book ID is not found. Please make sure the book ID is correct");
else
{
printf("Current book price is %f\n", bookPrice[match]);
printf("Enter new price for (%d-%s): ", bookID[match], bookTitle[match]);
scanf("%f", &bookPrice[match]);
}
return;
}
Your function declaration is not matched with function body.
This is working code.
#include<stdio.h>
char DisplayMenu(void);
void ViewBooks(int bookID[],float bookPrice[]);
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5]);
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5]);
int main()
{
char userchoice;
int bookID[5] = { 200,230,500,540,700 };
char bookTitle[5][50] = { {"Using Information Technology 12th Edition" }, { "Beyond HTML" },{"Build Your Own PC"},{"Instant Java Servlets"},{"DIgital Image: A Practical Guide"} };
float bookPrice[5] = { 100.30,50.40,47,83.30,22.90 };
do
{
userchoice = DisplayMenu();
if (userchoice == 'V')
ViewBooks(bookID, bookPrice);
else if (userchoice == 'S')
SearchBookPrice(bookID, bookTitle, bookPrice);
else if (userchoice == 'U')
UpdateBookPrice(bookID, bookTitle, bookPrice);
} while (userchoice != 'E');
printf("Thank you for using our system. Have a nice day!\n");
return 0;
}
char DisplayMenu(void)
{
char choice;
printf("*************************************************************************\n");
printf("V:View Books S:Search Book Price U:Update Book Price E:Exit");
printf("*************************************************************************\n");
do
{
printf("Enter your choice: ");
scanf(" %c", &choice);
if (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E')
printf("Invalid Choice\n");
} while (choice != 'V' && choice != 'S' && choice != 'U' && choice != 'E');
return choice;
}
void ViewBooks(int bookID[],float bookPrice[])
{
printf("%d Using Information Technology 12th Edition $%f\n", bookID[0], bookPrice[0]);
printf("%d Beyond HTML $%f\n", bookID[1], bookPrice[1]);
printf("%d Build Your Own PC $%f\n", bookID[2], bookPrice[2]);
printf("%d Instant Java Servlets $%f\n", bookID[3], bookPrice[3]);
printf("%d Digital Image: A Pratical Guide $%f\n", bookID[4], bookPrice[4]);
return;
}
void SearchBookPrice(int bookID[5],char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i= 0, match = -1;
printf("*************************************************************************\n");
printf("Search by book ID\n");
printf("*************************************************************************\n");
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i<5 && match==-1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("Please refer to the customer service for assitance");
else
{
printf("The book id is : %d\n", &idsearch);
printf("The price of %s is %f", bookTitle[match], bookPrice[match]);
}
return;
}
void UpdateBookPrice(int bookID[5], char bookTitle[5][50], float bookPrice[5])
{
int idsearch, i = 0, match = -1;
printf("Enter book ID: ");
scanf("%d", &idsearch);
while (i < 5 && match == -1)
{
if (bookID[i] == idsearch)
match = i;
i++;
}
if (match == -1)
printf("The book ID is not found. Please make sure the book ID is correct");
else
{
printf("Current book price is %f\n", bookPrice[match]);
printf("Enter new price for (%d-%s): ", bookID[match], bookTitle[match]);
scanf("%f", &bookPrice[match]);
}
return;
}

Need to get the output only when the loop ends

Here, i want to get the output only after the user enters the letter 'Q', which is the last task of my program. However, when i enter 'R' which is another case for my program, it directly prints the output for me.
int main() {
float amountOfTurkishLira = 0;
float amountOfBtc = 0;
float amountOfEth = 0;
char operationCode;
char operationTurkishLira;
// getting the amount of money for the initial money;
scanf(" %c", &operationTurkishLira);
if (operationTurkishLira != 'T') {
printf("Error: Operation could not be completed.");
} else {
scanf(" %f", &amountOfTurkishLira);
scanf(" %c", &operationCode);
while (operationCode != 'Q') {
switch (operationCode) {
case 'R':
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("Our account holds %.2f TRY", amountOfTurkishLira);
} else {
printf("Our account holds %.2f TRY | %.2f BTC | %.2F ETH.", amountOfTurkishLira, amountOfBtc,
amountOfEth);
}
scanf(" %c", &operationCode);
break;
}
}
printf("Bye..");
}
return 0;
}
This happens when i enter the input:
T
1000
R
Our account holds 1000.00 TRY
Bye...
But I want it to be like this:
T
1000
R
Q
after i enter all my input, it should give me
Our account holds 1000.00 TRY
Bye...
Take the printing code out of the loop and do it at the end.
And take scanf(" %c", &operationCode); out of the switch, since it should be done no matter what the previous code was.
int main() {
float amountOfTurkishLira = 0;
float amountOfBtc = 0;
float amountOfEth = 0;
char operationCode;
char operationTurkishLira;
// getting the amount of money for the initial money;
scanf(" %c", &operationTurkishLira);
if (operationTurkishLira != 'T') {
printf("Error: Operation could not be completed.");
} else {
scanf(" %f", &amountOfTurkishLira);
scanf(" %c", &operationCode);
while (operationCode != 'Q') {
switch (operationCode) {
case 'R':
break;
}
scanf(" %c", &operationCode);
}
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("Our account holds %.2f TRY", amountOfTurkishLira);
} else {
printf("Our account holds %.2f TRY | %.2f BTC | %.2F ETH.", amountOfTurkishLira, amountOfBtc,
amountOfEth);
}
printf("Bye..");
}
return 0;
}
Take a look on my version. I have some comments in it. You just have to add your logic for the case you pressing 'R' or an other key (I suppose you will add some more key press listeners).
#include <stdio.h>
int main() {
float amountOfTurkishLira = 0;
float amountOfBtc = 0;
float amountOfEth = 0;
char operationCode;
char operationTurkishLira;
// getting the amount of money for the initial money;
scanf(" %c", &operationTurkishLira);
if (operationTurkishLira != 'T') {
printf("Error: Operation could not be completed.");
}
else {
scanf(" %f", &amountOfTurkishLira);
scanf(" %c", &operationCode);
while (operationCode != 'Q') {
switch (operationCode) {
case 'R':
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("LOGIC FOR amountOfBtc == 0 && amountOfEth == 0\n");
} else {
printf("LOGIC FOR amountOfBtc != 0 || amountOfEth != 0\n");
}
break;
case 'X': // X, Y, or WHATEVER
// LOGIC FOR 'X'
break;
}
scanf(" %c", &operationCode);
}
if (operationCode == 'Q') {
if (amountOfBtc == 0 && amountOfEth == 0) {
printf("Our account holds %.2f TRY\n", amountOfTurkishLira);
}
else {
printf("Our account holds %.2f TRY | %.2f BTC | %.2F ETH.\n", amountOfTurkishLira, amountOfBtc, amountOfEth);
}
printf("Bye..");
}
}
return 0;
}

Getting stuck in getIntLimited function

i have a problem in getting stuck in getIntLimited function. In adjustQuantity i need it to check if user entered the right number, not more or less than needed and not letters. I didn't do that to "Stock" option, only in "Check" option. But my first problem is that i have to type the number twice to get a response from it and the second is that i can't get out of the getIntLimited loop.
Here's the image of the output:
and the part of my code:
void GroceryInventorySystem(void){
struct Item Items[MAX_ITEM_NO];
int opt, records;
int loop = 0;
welcome();
while(!loop){
if(opt == 3){
adjustQuantity(Items, records, CHECKOUT);
saveItems(Items, DATAFILE, records);
if(0 == saveItems(Items, DATAFILE, records)){
printf("Could not update data file %s\n", DATAFILE);
}
pause();
}
}
int getInt(void){ // Check if user entered the character and breaks, returns the value if a number //
char letter = 'x';
int value;
while(1){
scanf("%d%c", &value, &letter);
if(letter != '\n'){
printf("Invalid integer, please try again: ");
flushKeyboard();
}else{
return value;
}
}
}
int getIntLimited(int lowerLimit, int upperLimit){ // Check if user typed the value higher/lower and repeats. Returns the value if user entered the right number //
int Value;
while(1){
Value = getInt();
if(Value <= lowerLimit || Value >= upperLimit){
printf("Invalid value, %d < value < %d: ", lowerLimit, upperLimit);
}else{
return Value;
}
}
}
void adjustQuantity(struct Item item[], int NoOfRecs, int stock) {
int check, sku, index, opt;
char tostock[] = "to stock", tocheck[] = "to checkout";
printf("Please enter the SKU: ");
scanf("%d", &sku);
check = locateItem(item, NoOfRecs, sku, &index);
if (check == 0) {
printf("Item not found!\n");
} else {
displayItem(item[index], FORM);
if (stock == STOCK) {
printf("Please enter the quantity %s; Maximum of %d or 0 to abort: ", tostock, MAX_QTY - item[index].quantity);
scanf("%d", &opt);
if (opt == 0) {
printf("--== Aborted! ==--\n");
} else {
item[index].quantity += opt;
printf("--== Stocked! ==--\n");
}
} else {
printf("Please enter the quantity %s; Maximum of %d or 0 to abort: ", tocheck, item[index].quantity);
scanf("%d", &opt);
if (opt == 0) {
printf("--== Aborted! ==--\n");
} else if (item[index].quantity < opt){
opt = getIntLimited(item[index].quantity, 0);
} else {
item[index].quantity -= opt;
printf("--== Checked out! ==--\n");
if (item[index].quantity <= opt) {
printf("Quantity is low, please reorder ASAP!!!\n");
}
}
}
}
}
i did a mistake in opt = getIntLimited(0, item[index].quantity); it should be the opposite like this opt = getIntLimited(item[index].quantity, 0); now it exits out of the loop whenever i type the right number in between 0 and 5 .
I also putted opt = getIntLimited(0, item[index].quantity); instead of scanf, and removed else if statements

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)

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