Question:
The program should have menu to allow the user perform the following functions:
semester subject planning
entering of subjects' grade
display of subjects' information for the semester
The program should only terminate when the user select to quit the program.
Please help me solve this problem.
When I run this C programming code it will appear this error:
Error 16 error C2143: syntax error : missing ';' before 'type' g:\mini project testing\cgpacalculation\cgpacalculation\cgpacalculation4.c 190
Error 17 error C2143: syntax error : missing ';' before 'type' g:\mini project testing\cgpacalculation\cgpacalculation\cgpacalculation4.c 224
Please help me correct my coding:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <Windows.h>
//declaration
void userdetail(void);
void mainmenu(void);
void menu(void);
void menu_two(void);
void lines(void);
void getSubject(void);
void getCalculation(void);
void about(void);
float gradesToGP(char grades);
//display
void main()
{
mainmenu();
}
//definition
void mainmenu(void)
{
int select;
lines();
printf("\t\t\t CGPA Calculation \n");
lines();
printf("Enter \"1\" - Student Detail and Subject Information\n");
printf("Enter \"2\" - About\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &select);
lines();
if (select == 1)
{
userdetail();
getSubject();
}
else if (select == 2)
{
about();
getch();
menu();
}
else if (select == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
lines();
getch();
system("cls");
mainmenu();
}
}
void menu(void)
{
int choice;
printf("Enter \"1\" - Back to Main Menu\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &choice);
if (choice == 1)
{
system ("cls");
mainmenu();
}
else if (choice == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
getch();
system("cls");
menu();
}
}
void lines(void)
{
printf("**********************************************************************");
}
void userdetail(void)
{
char name[100][100], id[100][10];
printf("Enter your full name :\n");
scanf("%s", &name);
printf("Enter your student ID :\n");
scanf("%s", &id);
}
void getSubject(void)
{
int loop,numSubject, credit[100];
float GP[100], TargetCGPA[100];
char SubjectCode[100][40], grade[100];
char name[100][100];
char id[100][40];
int sumCredit = 0;
double sumCreditxGP = 0;
system("cls");
lines();
printf("Enter total subject :");
scanf("%d", &numSubject);
lines();
for (loop = 0; loop <= numSubject-1; loop++)
{
printf("Subject %d \n", loop+1);
printf("Enter subject code :");
scanf("%s", &SubjectCode[loop]);
printf("Credit hour :");
scanf("%d", &credit[loop]);
printf("Enter your grade :");
scanf("%s", &grade[loop]);
GP[loop] = gradesToGP(grade[loop]);
lines();
}
printf("Enter your targeted CGPA for this semester :");
scanf("%f", &TargetCGPA);
lines();
printf("Press \" ENTER \" or any button");
getch();
system("cls");
menu_two();
void menu_two(void);
{
int choice;
printf("Enter \"1\" - CGPA Calculation\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &choice);
if (choice == 1)
{
system ("cls");
getCalculation();
}
else if (choice == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
getch();
system("cls");
menu_two();
}
}
void getCalculation(void);
{
system("cls");
lines();
printf("Student Name : %c\n", name);
printf("Student ID : %c\n", id);
lines();
printf("No. Subject Code Credit Hour Grade Grade Point");
lines();
for (loop = 0; loop <= numSubject-1; loop++)
{
printf("\n%d %s\t %d\t%s\t%.2f\n", loop+1, SubjectCode[loop], credit[loop], grade[loop], GP[loop]);
}
for (loop = 0; loop <= numSubject-1; loop++)
{
sumCredit = sumCredit + credit[loop];
sumCreditxGP = sumCreditxGP + credit[loop] * GP[loop];
}
lines();
printf("Total credit hour is = %d\n\n", sumCredit);
printf("Total credit hour X grade point is = %.2f\n\n", sumCreditxGP);
printf("CGPA is = %.2f", sumCreditxGP / sumCredit);
lines();
getch();
menu();
}
}
void about(void)
{
system("cls");
lines();
printf("\n\t\t\tMini Project\n");
lines();
printf("Develop by: Shah Rezza Bin Jasni\n");
printf("Institution: Universiti Tenaga Nasional\n\n\n");
lines();
printf("COPYRIGHT 2014");
lines();
}
float gradesToGP(char grades)
{
if (grades == 'A+')
{
return(float)4.00;
}
else if (grades == 'A')
{
return(float)4.00;
}
else if (grades == 'A-')
{
return(float)3.67;
}
else if (grades == 'B+')
{
return(float)3.33;
}
else if (grades == 'B')
{
return(float)3.00;
}
else if (grades == 'B-')
{
return(float)2.67;
}
else if (grades == 'C+')
{
return(float)2.33;
}
else if (grades == 'C')
{
return(float)2.00;
}
else if (grades == 'C-')
{
return(float)1.67;
}
else if (grades == 'D+')
{
return(float)1.33;
}
else if (grades == 'D')
{
return(float)1.00;
}
else if (grades == 'E')
{
return(float)0.00;
}
else
{
return(float)0.00;
}
}
It looks like you put the function menu_two inside another function. Your Visual C++ compiler doesn't accept local functions.
Same problem with getCalculation. According to your declarations, those should be in global scope.
You have few problems with your code-
menu_two();
} // here you need to close it
void menu_two(void) // remove the semicolon here
Because Local functions are not acceptable. Don't include any function inside another function.
void getCalculation(void) // remove semicolon here
Make the above two functions as a Global function. any try to pass the necessary information to that functions and try.
Related
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;
}
In this code at checkout function g_tot calculation brings garbage value. I think its because I'm calculating two variables from another two functions, but I don't know how to fix it. There's another error in restaurant function in if condition if I enter value more than 8 it'll bring garbage value to tot. But the most important one is
#include<stdio.h>
#include<conio.h>
// Global variables
int room,answr,days=0;
char name[20],choose;
int i=0,tot=0,p_tot=0,g_tot=0,z=0;
int p_price[2][5]={4000,10000,20000};
void screenheader()
{
printf("\n ::::::::::::::::::::::::::::::::::::::");
printf("\n :: ::");
printf("\n :: ############################ ::");
printf("\n :: # # ::");
printf("\n :: # WELCOME # ::");
printf("\n :: # TO # ::");
printf("\n :: # SURF HOTEL # ::");
printf("\n :: # # ::");
printf("\n :: ############################ ::");
printf("\n :: ::");
printf("\n ::::::::::::::::::::::::::::::::::::::");
}
void check_in()
{
int contact_No[20],NIC[10];
char first_name[10],last_name[10],Country[10];
system("cls");
screenheader();
printf("\n1. Packages");
printf("\n2. Room Allocation");
printf("\n3. Back\n\n");
scanf(" %d",&answr);
switch(answr)
{
case 1:{
system("cls");
printf("\n\n\nPer 2 Persons");
printf("\n\t\tPackage Name >>>> Couple");
printf("\n\t\tRs.4000/= per day");
printf("\n\t\tBed >>>> 1");
printf("\n\t\t *Tv Available");
printf("\n\n\n\n\t\t\nPer 4 Persons\n\t\tPackage Name >>>>
Family");
printf("\n\t\tRs.10,000/= per day");
printf("\n\t\tBed >>>> 2");
printf("\n\t\t*Tv Available \n\t\t*A/C \n\t\t*WIFI");
printf("\n\n\n\nPer 8 Persons\n\t\tPackage Name >>>> Deluxe");
printf("\n\t\tRs.20,000/= per day");
printf("\n\t\tBed >>>> 3 Large ");
printf("\n\t\t *Tv Available \n\t\t*A/C \n\t\t*WIFI\n\t\t*Local
Travel Guide\n\t\t*Balcony with a view\n\t\t*Writing desk");
printf("\n\n*Press 1 to go back");
getch();
check_in();
break;
}
case 2:{
printf("What package do you want?");
scanf(" %d",&i);
p_tot=p_tot+p_price[i-1];
if(i == 1)
{
printf("You have selected Couple package");
}
else if (i == 2)
{
printf("You have selected Family Package ");
}
else if (i == 3)
{
printf("You have selected Deluxe package");
}
else
{
printf("\n\nWrong input, please refer to packages and try
again.\nPress Enter to select another package");
getch();
check_in();
}
printf("\nEnter First Name:\n");
scanf(" %s",&first_name);
printf("Last Name:\n");
scanf(" %s",&last_name);
printf("How many days do you want to stay?");
scanf(" %d",&days);
printf("Enter your Country:");
scanf(" %s",&Country);
printf("Enter your NIC No:");
scanf(" %d",&NIC);
printf("Enter your Contact No:");
scanf(" %d",&contact_No);
printf("Hello %s %s you have booked a Room for
%d",&first_name,&last_name,days);
getch();
system("cls");
int main();
}
case 3: main();
}
}
void restaurant()
{
int fc[6];
char ans;
char food[8][30]={"Bread","Noodles","Salad","Popcorn","Chocolate ice
cream","Vanilla ice cream","Cold Coffee","Milk Shake"};
int price[8]={180,120,65,55,70,70,110,200};
printf("Press Enter To Continue To The Restaurant ");
getchar();
system("cls");
printf("\n\n\n\n\n\t *********");
printf("\n\t MENU CARD");
printf("\n\t *********\n\n\n");
printf("\n Food Code\t\tprice\t\t Food Name\n");
for(i=0;i<8;i++)
{
printf("\n\t\t%d",i+1);
printf("\t\tRs. %d",price[i]);
printf("\t\t%s",food[i]);
}
printf("\n\n\n\t *PRESS 0 TO GO TO THE MAIN MENU\n\t *PRESS 1 TO ORDER FOOD
: ");
scanf(" %d",&answr);
switch(answr)
{
case 0:
{
main();
break;
}
case 1:do
{
printf("\n\nENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf("%d", &z);
if (z < 1 || z > 8)
{
printf("Invalid food code\n");
}
tot=tot+price[z-1];
printf("total so far is Rs.%d\n",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c", &ans);
} while(ans=='y'|| ans=='Y');
printf("\n\nYour bill is Rs.%d",tot);
printf("\nYour bill will be added to the total bill at checkout");
printf("\n\nPress Enter to go back to main menu");
getch();
system("cls");
main();
}
}
void check_out()
{
system("cls");
screenheader();
printf("\n\nAre you sure you want checkout (Y/N)");
scanf(" %c",&choose);
if(choose=='n' || choose=='N')
{
main();
}
else if(choose== 'Y' || choose=='y')
{
system("cls");
screenheader();
g_tot=p_tot+tot;
printf("Total");
printf("%d",g_tot);
}
}
int main()
{
screenheader();
printf("\n\n\nPress Enter To Continue");
getchar();
system("cls");
screenheader();
printf("\n\n\n\n\t\t ************* \n");
printf("\t\t * MAIN MENU * \n");
printf("\t\t ************* \n\n\n");
printf("\t\t\t01. Check In \n");
printf("\t\t\t02.Restaurant\n");
printf("\t\t\t03.Checkout \n");
printf("\n\t\t\t04.Exit");
scanf(" %d",&answr);
switch(answr)
{
case 1:{
check_in();
break; }
case 2:{
restaurant();
break; }
case 3: {
check_out();
}
}
return 0;
}
if condition if I enter value more than 8 it'll bring garbage value to tot.
This is as expected. When z > 8, code attempts to access outside price[] range. Result: undefined behavior (UB). The prior if (z < 1 || z > 8) did not steer code away from price[z - 1]. Rest of code including g_tot = p_tot + tot; is now questionable.
int price[8] = {180, 120, 65, 55, 70, 70, 110, 200};
...
if (z < 1 || z > 8) {
printf("Invalid food code\n");
}
tot = tot + price[z - 1]; // UB here
...
g_tot = p_tot + tot;
Do not access price[z - 1] unless z in the range [1...8].
Other problems exist: Best to enable all compilers warnings and seem them (12+) yourself.
I am working on this cinema reservation project !! 2 problems i m facing here is with data saving.
1) : when i close the program and try to register a new user the login information of previous registered user deletes Even though i am opening file in "a+" mode. but if i directly log in with the already registered user it works. the thing is i am not able to register more than 1 user at a time.
2) : When i close the program and logged in again the information of user reservation deletes. i want to save the user's reservation information too.
How can I solve this ?
struct login
{
char fname[100];
char lname[100];
char username[20];
char password[20];
};
#include <stdio.h>
#define RVALUE 5
#define CVALUE 10
int i, j;
void DisplaySeats(void);
void ReserveSeats(void);
void ChooseSeat(void);
void CancelSeat(void);
void CheckCancelSeat(void);
void menu(void);
int Seats[RVALUE][CVALUE];
void registration();
void login();
int main()
{
int c;
int choice, menu;
printf("Welcome to our small Cinema!!!\n");
printf("\n");
//DisplaySeats();
printf("\n1 : register!!!\n");
printf("2 : login!!!\n");
printf("3 : quit\n");
printf("Enter your choice : \n");
scanf("%d",&c);
switch (c)
{
case 1:
registration();
break;
case 2:
login();
break;
case 3:
printf("Thankyou for Choosing our small Cinema !! \n");
getch();
exit(1);
default :
system("CLS");
printf("Enter a valid number !!\n");
main();
}
getch();
}
void registration()
{
FILE *log;
log = fopen("login.txt", "a+");
struct login l;
printf("\nEnter first name : ");
scanf("%s", &l.fname);
printf("\nEnter last name : ");
scanf("%s", &l.lname);
printf("\nEnter your Username : ");
scanf("%s", &l.username);
printf("\nEnter your password : ");
scanf("%s", &l.password);
fwrite(&l, sizeof(l), 1, log);
fclose(log);
printf("\nYou are successfully registered!!");
printf("\nYour UserId is %s and your password is %s", l.username, l.password);
printf("\nNow login with your username and password!!");
printf("\nPress any key to continue ...");
getch();
system("CLS");
main();
}
void login()
{
char username[100];
char password[100];
FILE *log;
struct login l;
log = fopen("login.txt", "r");
if (log == NULL)
{
printf("FILE NOT FOUND!!!");
exit(1);
}
printf("\nUserID : ");
scanf("%s", &username);
printf("\nPassword : ");
scanf("%s", &password);
while (fread(&l, sizeof(l), 1, log));
{
if (strcmp(username, l.username) == 0 && strcmp(password, l.password)==0)
{
system("CLS");
printf("\nYou are successfully logged in !!\n");
menu();
}
else
{
printf("\nYour UserID or password is incorrect !!\n");
printf("Press any key to continue ...\n");
getch();
system("CLS");
login();
}
}
fclose(log);
}
void ChooseSeat(void)
{
int row, col,k;
printf("Which row do you want to choose? : ");
scanf("%d", &row);
printf("Which seat do you want to select? : ");
scanf("%d", &col);
if (row > RVALUE || col > CVALUE)
{
printf("Wrong Entry !! Try again\n");
ChooseSeat();
}
else if (Seats[row - 1][col - 1] != 0)
{
printf("Seat is already reserved try again !!\n");
ChooseSeat();
}
else
{
Seats[row - 1][col - 1] = 1;
printf("Congratulations!! Reservation Completed!!!\n");
DisplaySeats();
printf("\nPress any key to go to main menu!!");
getch();
system("CLS");
main();
}
}
void ReserveSeats(void)
{
int NoOfSeats,i;
printf("How many seats do you want to reserve?\n");
scanf("%d", &NoOfSeats);
DisplaySeats();
for (i = 1; i <= NoOfSeats; i++)
{
ChooseSeat();
}
}
void DisplaySeats(void)
{
printf("\t \t Seats\n");
printf("\t 1 2 3 4 5 6 7 8 9 10\n");
for (i = 0; i < RVALUE; i++)
{
printf("Rows %d : ", i + 1);
for (j = 0; j < CVALUE; j++)
printf("%d ", Seats[i][j]);
printf("\n");
}
printf("\n");
}
void CheckCancelSeat(void)
{
while (1)
{
for (i = 0; i < RVALUE; i++)
{
for (j = 0; j < CVALUE; j++)
{
if (Seats[i][j] == 0)
{
printf("There is no reserved seat available!!\n");
break;
}
else
{
CancelSeat();
}
break;
}
break;
}
system("CLS");
break;
}
}
void CancelSeat(void)
{
int r, c;
printf("\nWhich row do you want to cancell ? : ");
scanf("%d", &r);
printf("\nWhich column do you want to cancell ? : \n");
scanf("%d", &c);
if (Seats[r - 1][c - 1] != 0)
{
Seats[r - 1][c - 1] = 0;
printf("Your Seat is Cancelled !!\n");
system("CLS");
}
else
{
printf("Wrong Entry !!\n");
CancelSeat();
}
}
void menu(void)
{
int choice;
DisplaySeats();
printf("1 : reserve a seat\n");
printf("2 : cancell a seat\n");
printf("3 : Main Menu\n");
printf("Enter your choice : \n");
scanf("%d", &choice);
system("CLS");
switch (choice)
{
case 1:
ReserveSeats();
break;
case 2:
CheckCancelSeat();
break;
case 3:
main();
break;
default:
printf("Please enter a valid choice!!");
menu();
break;
}
}
The problem is that it is very difficult to save more than one structure to a binary file.
Note: Binary files do not have the file extension ".txt" but z. ".Bin", ".dat" or the like; Sometimes binary files are saved even without file extension.
I think you try to "spam" the password in the binary file (encrypt). Unfortunately, I have to tell you that this does not work. If you open the file with a text editor you will see the password.
I would suggest you save the data as a text file and then read the data step by step.
Also, you did not have all the functions declarated.
There are implicit declarations. This can lead to errors for larger programs. For a few, the solution would be to include stdlib.h.
If you want to save a file the following night please use the mode Subfix "b" z. Eg "a + b".
A few helpful links are possible:
http://www.cplusplus.com/reference/cstdio/fopen/
http://www.cplusplus.com/reference/cstdio/fwrite/
Reading/Writing a structure into a binary file
Although I could not solve the problems, but I hope I could help anyway.
If you still want to save the structures in a binary file, you can read this on the Internet (I just say Google).
For example, if you want to encrypt something with C, you can search for a crypt library or something similar.
The first screen, gotoxy code is working. But in second screen. Nothing happens, like, it does not read the gotoxy code at all. Please enlighten me about the problem.
Here is the 1st screen :
Here is the 2nd screen :
Here is the code. I would love to learn more about gotoxy.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy( int column, int line );
int main();
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int choice;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,8);
printf("1. Add Product\n\n");
gotoxy(22,10);
printf("2. Display Product\n\n");
gotoxy(22,12);
printf("3. Search Product\n\n");
gotoxy(22,14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22,16);
printf("5. Update Product\n\n");
gotoxy(22,20);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch(choice)
{
case 1 : addProduct();
break;
case 2 : displayProduct();
break;
case 3 : searchProduct();
break;
case 4 : reorderProduct();
break;
case 5 : updateProduct();
break;
default : printf("Wrong input. Please try again.");
system("cls");
main();
}
return (0);
}
int addProduct()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
do
{
fp = fopen("inventory.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
main();
}
else
{
do{
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
}while(checker != 'Y' && checker != 'N');
if(checker == 'Y'){addProduct();}
if(checker == 'N'){
system("cls");
main();}
}
return(0);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am also having the "undefined reference to ERPSGame()" error as well as the "Id returned 1 exit status". Did I declare something wrong? I am using Orwell Dev C++.
What seems to be the problem? Is it my compiler ?
Check out the whole code:
/* GameBox BETA v0.2 */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void ERPS();
void Menu();
void BJack();
void Log();
int ERPSGame();
void ERPSInstruct();
int main() {
char Choice;
system("cls");
printf("\t\t Welcome to Mark and Leroy's GameBox BETA v0.2 \n\n\n");
printf("--------------------------------------------------------------------------------\n");
printf("\t _________________________________________________________\n");
printf("\t ||_____________________________________________________|| \n");
printf("\t || \t\t|| \n");
printf("\t || \t\t|| \n");
printf("\t || \t\t|| \n");
printf("\t ||\t Please press 'A' to continue to the menu\t||\n");
printf("\t ||\t \t||\n");
printf("\t ||\t Otherwise, press 'F' to exit the program\t||\n");
printf("\t ||_____________________________________________________|| \n");
printf("\t ||_____________________________________________________||\n");
printf("--------------------------------------------------------------------------------\n\n");
/* Drawing Starts Here */
printf("\t\t ========================================\n");
printf("\t\t || || \n");
printf("\t\t || ||=============== || \n");
printf("\t\t || || || \n");
printf("\t\t || || ======= || \n");
printf("\t\t || || Gamebox || || \n");
printf("\t\t || || BETA v0.2 || || \n");
printf("\t\t || ||==============|| || \n");
printf("\t\t || || \n");
printf("\t\t || ||==== ||====|| == == || \n");
printf("\t\t || || || || || == == || \n");
printf("\t\t || ||===|| || || == || \n");
printf("\t\t || ||___|| ||====|| == == || \n");
printf("\t\t || == == || \n");
printf("\t\t || Artwork by: Mark Sanchez || \n");
printf("\t\t ========================================\n");
/* Drawing Ends Here */
scanf("%c", &Choice);
switch (Choice) {
case'A':case'a':{
Menu();
break;
}
case'F':case'f':{
exit(0);
}
default: {
main();
}
}
return 0;
}
void Menu() {
char Choice1;
system("cls");
printf("\t\t Welcome to Mark and Leroy's GameBox BETA v0.2 \n\n\n");
printf("--------------------------------------------------------------------------------\n\n");
printf("\t\t Press 'R' to play the Enhanced Rock-Paper-Scissors game\n\n\n");
printf("\t\t Press 'B'to play Blackjack\n\n\n");
printf("\t\t Press 'C' to view the changelog\n\n\n");
printf("--------------------------------------------------------------------------------\n\n");
scanf("%c", &Choice1);
switch(Choice1) {
case'R':case'r': {
ERPS();
break;
}
case'B':case'b': {
BJack();
break;
}
case'C':case'c': {
Log();
break;
}
default: {
Menu();
}
}
}
void ERPS() {
system("cls");
char Instruct1;
char Instruct2;
/* Menu of ERPS */
printf("\t\t\t Welcome to ENHANCED Rock Paper Scissors\n\n\n");
printf("\t\t\t Press A to start the game\n\n");
printf("\t\t\t Press S to view the instructions\n\n");
printf("\t\t\t Press 'P' to return to the main menu\n");
scanf("%c", &Instruct1);
scanf("%c", &Instruct2);
switch(Instruct2) {
case'A':case'a': {
ERPSGame();
break;
}
case'S':case's': {
ERPSInstruct();
break;
}
case'P':case'p': {
Menu();
break;
}
default:{
ERPS();
}
}
}
int rand_i(int n)
{
int rand_max = RAND_MAX - (RAND_MAX % n);
int ret;
while ((ret = rand()) >= rand_max);
return ret/(rand_max / n);
}
int weighed_rand(int *tbl, int len)
{
int i, sum, r;
for (i = 0, sum = 0; i < len; sum += tbl[i++]);
if (!sum) return rand_i(len);
r = rand_i(sum) + 1;
for (i = 0; i < len && (r -= tbl[i]) > 0; i++);
return i;
}
int ERPSGame(int argc, const char *argv[]) /* THIS IS THE ERPSGame */
{
char umove[10], cmove[10], line[255];
int user, comp;
int tbl[]={0,0,0};
int tbllen=3;
printf("Hello, Welcome to rock-paper-scissors\nBy The Elite Noob\n");
mainloop:
while(1)
{ // infinite loop :)
printf("\n\nPlease type in 1 for Rock, 2 For Paper, 3 for Scissors, 4 to quit\n");
srand(time(NULL));
comp = (weighed_rand(tbl, tbllen) + 1) % 3;
fgets(line, sizeof(line), stdin);
while(sscanf(line, "%d", &user) != 1) //1 match of defined specifier on input line
{
printf("You have not entered an integer.\n");
fgets(line, sizeof(line), stdin);
}
if( (user > 4) || (user < 1) )
{
printf("Please enter a valid number!\n");
continue;
}
switch (comp)
{
case 1 :
strcpy(cmove, "Rock");
break;
case 2 :
strcpy(cmove, "Paper");
break;
case 3 :
strcpy(cmove, "Scissors");
break;
default :
printf("Computer Error, set comp=1\n");
comp=1;
strcpy(cmove, "Rock");
break;
}
switch (user)
{
case 1 :
strcpy(umove, "Rock");
break;
case 2 :
strcpy(umove, "Paper");
break;
case 3 :
strcpy(umove, "Scissors");
break;
case 4 :
printf("Goodbye! Thanks for playing!\n");
return 0;
default :
printf("Error, user number not between 1-4 exiting...");
goto mainloop;
}
if( (user+1)%3 == comp )
{
printf("Comp Played: %s\nYou Played: %s\nSorry, You Lost!\n", cmove, umove);
}
else if(comp == user)
{
printf("Comp Played: %s\nYou Played: %s\nYou Tied :p\n", cmove, umove);
}
else
{
printf("Comp Played: %s\nYou Played: %s\nYay, You Won!\n", cmove, umove);
}
tbl[user-1]++;
}
}
void ERPSInstruct() {
system("cls");
char Instruct3;
printf("\t\t\t Instructions of the Game \n\n");
printf("\t\t\t There will be 5 choices to choose from\n");
printf("\t\t\t 1. Rock\n");
printf("\t\t\t 2. Paper\n");
printf("\t\t\t 3. Scissors\n");
printf("\t\t\t 4. Lizard\n");
printf("\t\t\t 5. Spock\n\n\n\n");
printf("\t\t This is the mechanism of the choices: \n\n");
printf("\t\t Scissor cuts paper. \n\n");
printf("\t\t Paper covers rock. \n\n");
printf("\t\t Rock crushes lizard. \n\n");
printf("\t\t Lizard poisons spock. \n\n");
printf("\t\t Spock smashes scissors. \n\n");
printf("\t\t Scissors decapitate lizard. \n\n");
printf("\t\t Lizard eats paper. \n\n");
printf("\t\t Paper disproves spock. \n\n");
printf("\t\t Spock vaporizes rock. \n\n");
printf("\t\t Rock crushes scissors. \n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct3);
switch(Instruct3) {
case'P':case'p': {
Menu();
break;
}
default: {
ERPSInstruct();
}
}
}
void BJack() {
system("cls");
char Instruct2;
printf("BLACKJack \n\n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct2);
switch(Instruct2) {
case'P':case'p': {
Menu();
break;
}
default: {
BJack();
break;
}
}
getch();
}
void Log() {
system("cls");
char Instruct3;
printf("CHANGELOG: \n\n\n");
printf(" 0.1 -- Fixed a bug wherein the program forces\n to do 'default' upon choosing a game\n\n\n");
printf("-----------------------------------------------\n\n");
printf(" 0.2 -- Fixed display in CMD: Added a few dividers \nas well as correcting a few grammatical mistakes\n");
printf(" Added awesome graphics \n\n\n");
printf("-----------------------------------------------\n\n");
printf("\t\t\t Press 'P' to return to the menu\n");
scanf("%c", &Instruct3);
switch(Instruct3) {
case'P':case'p': {
Menu();
break;
}
default: {
Log();
break;
}
}
}
Is it my way of declaring ERPSGame? I used int.
Function declaration is not matched with function definition.
Declaration:
int ERPSGame();
Definition
int ERPSGame(int argc, const char *argv[])
In Dev-C++, under Tools -> Compiler Options, put "-Wall" in the "...commands when calling compiler" box. It will be helpful for you to get warnings and do effective coding and saves your time too.
When you take in instruct1 and then use instruct2 in the switch...
scanf("%c", &Instruct1);
scanf("%c", &Instruct2);
switch(Instruct2) {
case'A':case'a': {
ERPSGame();
This could be giving you an odd value given the fact that users response is in instruct1. Potentially.