Any idea on when I choose to add a client and as soon as I enter in a client ID the program crashes for that entry?
#include <stdio.h>
#include <stdlib.h>
struct client
{
int clID;
char cname;
char caddress;
char cemail;
int cfees;
int ceID;
char cename;
}typedef client;
struct employee
{
int empID;
char ename;
double erate;
double ehours;
double esalary;
int ecID;
}typedef employee;
void mainMenu();
void clientMenu();
void empMenu();
void getClient(client* pcli);
void getEmp(employee* pemp);
void payroll(employee* pemp);
void dispPay(employee* pemp);
void dispClient(client* pcli);
void dispEmployee(employee* pemp);
int main()
{
client cli[100];
client* pcli = &cli[0];
employee emp[20];
employee* pemp = &emp[0];
int answer = -1;
int mchoice;
int cchoice;
int echoice;
int ccount;
int ecount;
int input[9];
int* psearchclientID;
int i;
printf("Do you wish to start the program? 1 for yes 2 for no: ");
scanf("%d", &answer);
if(answer ==1)
{
while(mchoice != 3)
{
mainMenu();
scanf("%d", &mchoice);
switch(mchoice)
{
case 1: while(cchoice != 3)
{
clientMenu();
scanf("%d", &cchoice);
switch(cchoice)
{
case 1: getClient(pcli + i);
ccount++;
break;
case 2: printf("Enter the client ID to search for: ");
psearchclientID = fgets(input, 9, stdin);
strtok(input, "\n");
for(i = 0; i < 1; i++)
{
if(strcmpi(psearchclientID, (pcli->clID + i)) == 0)
printf("Client found at position %d\n", i);
else
printf("Client not found!");
}//end for
break;
}//end client switch
}//end client while
cchoice = 0;
break;
case 2: while(echoice != 4)
{
empMenu();
scanf("%d", &echoice);
switch(echoice)
{
case 1: getEmp(pemp + i);
ecount++;
break;
case 2: payroll(pemp + i);
dispPay(pemp + i);
break;
case 3:
break;
}//end emp switch
}//end emp while
echoice =0;
break;
}//end switch
}//end main while
}//end if
else if(answer ==2)
{
printf("Goodbye!");
exit(0);
}
return 0;
}//end main
void mainMenu()
{
printf("1-Client Menu\n"
"2-Employee Menu\n"
"3-Quit\n");
printf("Enter a choice from the menu: ");
}//end mainMenu
void clientMenu()
{
printf("1-Add a client\n"
"2-Search client\n"
"3-Go Back to Main Menu\n");
printf("Enter a choice from the menu: ");
}//end clientMenu
void empMenu()
{
printf("1-Add an Employee\n"
"2-Process an Employee(payroll)\n"
"3-Search Employee\n"
"4-Go Back to Main Menu\n");
printf("Enter a choice from the menu: ");
}//end empMenu
This is specifically the code for entering in the client info
void getClient(client* pcli)
{
printf("Enter client ID: ");
scanf("%d", &pcli->clID);
printf("Enter client name: ");
scanf("%s", &pcli->cname);
printf("Enter client address: ");
scanf("%s", &pcli->caddress);
printf("Enter client email: ");
scanf("%s", &pcli->cemail);
printf("Enter monthly service fees:" );
scanf("%d", &pcli->cfees);
}//end getClient
void getEmp(employee* pemp)
{
printf("Enter employee ID: ");
scanf("%d", &pemp->empID);
printf("Enter employee name: ");
scanf("%s", &pemp->ename);
printf("Enter employee hourly rate: ");
scanf("%lf", &pemp->erate);
printf("Enter employee hours worked: ");
scanf("%lf", &pemp->ehours);
}//end getEmp
void payroll(employee* pemp)
{
pemp->esalary = pemp->erate * pemp->ehours;
}//end payroll
void dispPay(employee* pemp)
{
printf("Employee ID %d\nEmployee Salary: %2.2f\n", pemp->empID, pemp->esalary);
}//end dispPay
This is where the information would be displayed
void dispClient(client* pcli)
{
printf("Client ID: %d\n Name: %s\n Address: %s\n Email: %s\n Monthly fees: %d\n Employee assigned: %d\n Employee name: %s\n", pcli->clID, pcli->cname, pcli->caddress, pcli->cemail, pcli->cfees, pcli->ceID, pcli->cename);
}//end dispClient
void dispEmployee(employee* pemp)
{
printf("Employee ID: %d\n Name: %s\n Hourly Rate: %2.2f\n Hours worked: %2.2f\n Salary: %2.2f\n Client(s) assigned: %s\n", pemp->empID, pemp->ename, pemp->erate, pemp->ehours, pemp->esalary, pemp->ecID);
}//end dispEmp
You don't include & (address) operator for %s (strings) while reading. For example, in function getClient, use
printf("Enter employee name: ");
scanf("%s", pemp->ename);
This is one of the problems in your program.
Your clientMenu switch statement to add a client calls:
case 1: getClient(pcli + i);
It is possible to be in that routine where i is uninitialized, so it could be anything, and very likely beyond the bounds of your 100 element array. In that case getClient will very likely be operating in memory it does not own, making your program liable to crash.
It's possible there's more than that issue at play, as well. As others have stated, more information about the crash would help.
Related
I'm trying to create a program that allows users to add information into a nameCard array using a function called AddNameCard. but when I try to add another set of input in, the previous items seems to get overwritten. and the listnamecard function only displays the last inputted items. Anyone know what i need to do to get around this problem? I'm learning C programming currently, go easy on me please :).
#include <stdio.h>
#include <stdlib.h>
# define Max_Size 5
typedef struct
{
int nameCardID;
char personName[20];
char companyName[20];
} NameCard;
NameCard nameCard[Max_Size];
void AddNameCard() {
int j;
printf("\n");
for (int i = j - 1; i < j; i++){
printf("Enter Name Card ID: \n");
scanf("%d", &nameCard[i].nameCardID);
printf("Enter Person Name: \n");
scanf("%s", &nameCard[i].personName);
printf("Enter Company Name : \n");
scanf("%s", &nameCard[i].companyName);
}
printf("\n");
return;
}
void ListNameCard() {
int j;
printf("\n");
printf("\nName_Card_ID Person_Name Company_Name \n");
for (int i = 0; i < j; i++){
printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
}
printf("\n");
return;
}
void GetNameCard() {
printf("%d %s %s", nameCard[1].nameCardID, nameCard[1].personName, nameCard[1].companyName);
}
int main()
{
int options;
while (options != 5) {
printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
printf("\n");
printf("What would you like to do? : ");
scanf("%d", &options);
switch (options)
{
case 1:
ListNameCard();
break;
case 2:
AddNameCard();
break;
case 3:
printf("Case3 ");
printf("\n");
break;
case 4:
GetNameCard();
break;
default:
printf("quit ");
}
}
#include <stdio.h>
#include <stdlib.h>
#define Max_Size 5
typedef struct {
int nameCardID;
char personName[20];
char companyName[20];
}
NameCard;
NameCard nameCard[Max_Size];
void AddNameCard() {
printf("\n");
int i;
printf("enter the number of person you want to add :");
scanf("%d", & i);
printf("Enter Name Card ID: \n");
scanf("%d", & nameCard[i - 1].nameCardID);
printf("Enter Person Name: \n");
scanf("%s", nameCard[i - 1].personName);
printf("Enter Company Name : \n");
scanf("%s", nameCard[i - 1].companyName);
printf("\n");
return;
}
void ListNameCard() {
printf("\n");
printf("\nName_Card_ID Person_Name Company_Name \n");
for (int i = 0; i < Max_Size; i++) {
printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
}
printf("\n");
return;
}
void GetNameCard() {
int n;
printf("\n");
printf("enter the number of the person");
scanf("%d", & n);
printf("%d %s %s", nameCard[n - 1].nameCardID, nameCard[n - 1].personName, nameCard[n - 1].companyName);
printf("\n");
}
int main() {
int options;
while (options != 5) {
printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
printf("\n");
printf("What would you like to do? : ");
scanf("%d", & options);
switch (options) {
case 1:
ListNameCard();
break;
case 2:
AddNameCard();
break;
case 3:
printf("Case3 ");
printf("\n");
break;
case 4:
GetNameCard();
break;
default:
printf("quit ");
}
}
}
Let's follow the comments' suggestions.
First we avoid using options variable uninitialized by changing:
while (options != 5) {
...
}
To:
do {
... (set options variable here)
} while (options != 5);
Secondly, we change the name of j variable to count and use it as function parameter/return so we keep track and update the number of added cards. For instance, AddNameCard becomes:
int AddNameCard(int count)
{
if (count < Max_Size) {
count++;
int i = count - 1;
printf("\n");
printf("Enter Name Card ID: ");
scanf("%d", &nameCard[i].nameCardID);
discard_newline();
printf("Enter Person Name: ");
scanf("%19[^\n]", nameCard[i].personName);
discard_newline();
printf("Enter Company Name: ");
scanf("%19[^\n]", nameCard[i].companyName);
discard_newline();
} else {
printf("Maximum number of cards reached\n");
}
printf("\n");
return count;
}
(discard_newline prevents newline characters to creep into the next scanf. "%19[^\n]" format prevents buffer overrun into 20 length strings.)
The name of an array variable is already taken as its address (or the address of its first element). So personName and companyName members shouldn't be preceded by &.
The code becomes:
#include <stdio.h>
#include <stdlib.h>
#define Max_Size 5
typedef struct
{
int nameCardID;
char personName[20];
char companyName[20];
} NameCard;
NameCard nameCard[Max_Size];
void discard_newline(void)
{
while( getchar() != '\n' );
}
int AddNameCard(int count)
{
if (count < Max_Size) {
count++;
int i = count - 1;
printf("\n");
printf("Enter Name Card ID: ");
scanf("%d", &nameCard[i].nameCardID);
discard_newline();
printf("Enter Person Name: ");
scanf("%19[^\n]", nameCard[i].personName);
discard_newline();
printf("Enter Company Name: ");
scanf("%19[^\n]", nameCard[i].companyName);
discard_newline();
} else {
printf("Maximum number of cards reached\n");
}
printf("\n");
return count;
}
void ListNameCard(int count) {
if (count > 0) {
printf("\nName_Card_ID Person_Name Company_Name\n");
for (int i = 0; i < count; i++){
printf("%d %s %s \n", nameCard[i].nameCardID, nameCard[i].personName, nameCard[i].companyName);
}
} else {
printf("Empty list: none added yet\n");
}
printf("\n");
return;
}
void GetNameCard() {
printf("%d %s %s", nameCard[1].nameCardID, nameCard[1].personName, nameCard[1].companyName);
}
int main()
{
int options;
int count = 0;
do {
printf("1:List Name Cards\n2:Add Name Card\n3:Remove Name Cards\n4:Get Name Cards\n5:quit\n");
printf("\n");
printf("What would you like to do? : ");
scanf("%d", &options);
switch (options)
{
case 1:
ListNameCard(count);
break;
case 2:
count = AddNameCard(count);
break;
case 3:
printf("Case3 ");
printf("\n");
break;
case 4:
GetNameCard();
break;
default:
printf("quit\n");
}
} while (options != 5);
}
Running it:
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 1
Empty list: none added yet
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 2
Enter Name Card ID: 123
Enter Person Name: John Smith
Enter Company Name: COMP_01
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 1
Name_Card_ID Person_Name Company_Name
123 John Smith COMP_01
1:List Name Cards
2:Add Name Card
3:Remove Name Cards
4:Get Name Cards
5:quit
What would you like to do? : 5
quit
You still have to complete the other options.
I'm having problems editing the fields of a structure from a seperate function, I'm trying to edit fields of my drone structure from the update droneinfofunction .basically i get the same error for all the arrows (invalid type argument of '->')
i'm sure this problem stems from my lack of understanding of pointers
any help would be greatly appreciated :)
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
//define struct info and variables
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {
int searchID, numdrones, i, drYrMan;
double drMass, drTopSpeed, drMaxDist, drLoadCap;
char drname[20];
numdrones = no_of_drones;
printf("what drone ID would you like to update?: ");
scanf("%d", &searchID);
printf("name: ");
scanf("%s", drname);
printf("year manufactured: ");
scanf("%d", &drYrMan);
printf("mass: ");
scanf("%lf", &drMass);
printf("top speed: ");
scanf("%lf", &drTopSpeed);
printf("max distance: ");
scanf("%lf", &drMaxDist);
printf("load capacity: ");
scanf("%lf", &drLoadCap);
droneinfo[searchID]->drone_number = searchID;
droneinfo[searchID]->drone_name = drname;
droneinfo[searchID]->year_manufactured = drYrMan;
droneinfo[searchID]->mass = drMass;
droneinfo[searchID]->top_speed = drTopSpeed;
droneinfo[searchID]->max_distance = drMaxDist;
droneinfo[searchID]->load_capacity = drLoadCap;
for(i=0; i < numdrones; i++){
}
return 0;
}
//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){
int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;
scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
if (!strcmp(namechoice, droneinfo[i].drone_name)) {
printf("found a match\n\nID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
found = 1;
}
}
if(found == 0){
printf("\nNo matches were found!\n");
}
return 0;
//make condition for all
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;
//open the drone.txt file where the drone info is stored
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//initialise the function that puts the struct in an array
while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
//print the dtone info in an array
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
do{
//program menu with appropriate menu items
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
//switch for the 7 available menu cases
switch(choice)
{
case 1:
//input drone function
updateDroneInfo(droneinfo, no_of_drones);
break;
case 2:
//search drone function
searchDroneName(droneinfo, no_of_drones);
break;
case 3:
//simulate drone function
break;
case 4:
//display simulation results
break;
case 5:
//save drone information
break;
case 6:
//save all results function
break;
case 7:
//exit/breaks the loop
break;
default:
printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
break;
}
} while(choice != 7);
return 0;
}
re
droneinfo[searchID]
has the type drone_info and not drone_info*, so you use . instead of ->.
In statements like this:
droneinfo[searchID]->drone_number = searchID;
the expression droneinfo[searchID] is not a pointer. It has the type drone_info because the pointer droneinfo was already dereferenced by the subscript operator.
You have to write:
droneinfo[searchID].drone_number = searchID;
Also arrays do not have the assignment operator. You need to copy element elements from one array to another.
Instead of this statement:
droneinfo[searchID]->drone_name = drname;
you have to write using the standard string function strcpy:
$include <string.h>
//...
strcpy( droneinfo[searchID].drone_name, drname );
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.
I'm having serious trouble with my program it is supposed to provide a menu and do all the functions the code is pretty explanatory my problem is I only have visual studios which doesnt allow scanf and scanf_s and messes with things so I use online compilers but those are still iffy. Can any one help and give me some tips. I'm having trouble with the math and function to list accounts, some whiles are empty as well I wasn't sure if that was best to use. I'm relatively new to this forum. It also can't have struct and can only be in C :-(
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
char option;
while(1){
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
option = getchar();
scanf("%c\n", &option);
switch(option)
{
case '1': open();
break;
case '2': list();
break;
case '3': deposit();
break;
case '4': withdraw();
break;
case '5': return 0;
default: exit();
break;
}
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
The problem with scanf was interesting. Here is an example for how to do without (although you shouldn't) such that you can play with your code easier.
#include <stdio.h>
#include <stdlib.h>
// only 5 accounts possible
int accounts[5];
// each its balance
float avail_bal[5];
void options();
// open(P) is a standard Posix function
void bopen();
void list();
void deposit();
void withdraw();
// exit(3) is a standard C function
void pexit();
int main(void)
{
char option;
while (1) {
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
options();
option = getc(stdin);
// swallow the '\n'
getc(stdin);
switch (option) {
case '1':
bopen();
break;
case '2':
list();
break;
case '3':
deposit();
break;
case '4':
withdraw();
break;
case '5':
pexit();
default:
pexit();
}
}
return 0;
}
void options()
{
puts("1. Open Account");
puts("2. List Accounts");
puts("3. Deposit");
puts("4. Withdraw");
puts("5. Exit");
}
void bopen()
{
int acc_num;
char c;
puts("Open new account(enter number 1-5)");
c = getc(stdin);
getc(stdin);
// assuming ASCII here where the digits 0-9 start at place 48 in the table
acc_num = (int) c - 48;
if (acc_num < 1 || acc_num > 5) {
puts("Account number must be between one and five inclusive");
return;
}
if (accounts[acc_num] != 0) {
printf("Account number %d is already taken\n", acc_num);
return;
}
// mark account as taken
accounts[acc_num] = 1;
// spend a fiver for the new client for being a new client
avail_bal[acc_num] = 5.0;
printf("Account number: %d\n", acc_num);
printf("Available balance: %f\n", avail_bal[acc_num]);
}
void list()
{
int i;
for (i = 0; i < 5; i++) {
if (accounts[i] != 0) {
printf("Account 000%d: %f\n", i, avail_bal[i]);
}
}
}
void deposit()
{
float add;
int acc_num;
char c;
char s[100];
puts("Which account do you want to deposit money in?");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
printf("Amount to deposit: ");
// to get a number without scanf() we have to read the input as a string
// (fgets() adds a '\0' at the end, so keep a seat free for it)
fgets(s, 99, stdin);
// and convert it to a double (atof() only for brevity, use strtod() instead)
add = atof(s);
avail_bal[acc_num] += add;
printf("Amount deposited %f\n", add);
}
void withdraw()
{
int acc_num;
float withdraw;
char c;
char s[100];
// all checks ommitted!
puts("Account to withdraw from: ");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
puts("Amount to withdraw from account: ");
fgets(s, 99, stdin);
withdraw = atof(s);
avail_bal[acc_num] -= withdraw;
printf("Current balance for account %d: %f\n", acc_num, avail_bal[acc_num]);
}
void pexit()
{
// place logic to save data here or use a function triggered by atexit() for that task
puts("Imagine all of your data would have been put in a safe place!");
exit(EXIT_SUCCESS);
}
Just replace the constructs with scanf before you pass your work for grading.
If you're using integer values as options why you are using character's
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
int option;
printf("****Banking System WELCOME****\n");
void options();
printf("Enter 1-5 of the following options: \n");
scanf("%d",&option);
switch(option)
{
case 1: open();
break;
case 2: list();
break;
case 3: deposit();
break;
case 4: withdraw();
break;
case 5: return 0;
default: exit();
break;
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
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.