In checkout function g_tot calculation brings garbage value - c

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.

Related

Yes/No loop using while loop in C with a math operation program

First of all, I am a total beginner to both C (and any programming) and Stack Overflow, so sorry if something like this has been asked before.
So I've been having trouble with my math operations code in the loop part. When I type in N or Y, the program acts as if I typed in a different option and says I have to type in Y or N.
Here's the code. Sorry if it's a jumbled mess, this is all I know so far.
#include <stdio.h>
int main() {
while (1) {
int choice1, choice2, num1, num2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%d", &choice2);
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
break; }
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
break; }
else {
printf("Pick y or n only");
break;
}
}
return 0;
}
the issue here is that in the if statement you are using break which breaks the flow, instead you should be using continue
major changes: (I've added comment line to make the changes more obvious for you)
int check = 1; //added a check variable
while (check) { //used check variable for while loop
printf("\n Try another operation [y/n]: ");
scanf("%c", &choice2); //changed %d to %c as the input is a char
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
continue; } //changed break with continue
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
check= 0; } //updated check so next iteration fails and terminates the loop
else {
printf("Error wrong input");
check= 0; //there cannot be a 2nd try using if else statement
}
there will be few more changes which I'll mark in the code below
#include <stdio.h>
int main() {
int check = 1; //added a check variable
while (check) { //used check variable for while loop
int choice1, choice2, num1, num2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%c", &choice2); //changed %d to %c as the input is a char
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
continue; } //changed break with continue
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
check= 0; } //updated check so next iteration fails and terminates the loop
else {
printf("Error wrong input");
check= 0; //there cannot be a 2nd try using if else statement
}
}
return 0;
}
for more detail, you can read the docs
I think you will have to do some major changes as shown below
#include <stdio.h>
int main() {
while (1) {
int choice1, num1, num2;
char choice2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%s", choice2);
if (choice2 == 'y' || choice2 == 'Y')
{
scanf("%d", &choice1);
}
else;
{
printf("Exiting...");
}
}
return 0;
}

Problem: Scholarship Endowment Fund Part 2 (fund2.c)

Problem: Scholarship Endowment Fund Part 2 (fund2.c)
How do I return back to main menu and add a counter for the total number of donations and investment made, here's what i got?
Then, your program should allow the user the following options:
Make a donation
Make an investment
Print balance of fund
Quit
#include <stdio.h>
#include <stdlib.h>
//main functions
int main() {
// variables
int donation, investment, fund, total, tdonations, tinvestments;
// prompt user
printf("Welcome!\n");
printf("What is the initial balance of the fund\n");
scanf("%d", &fund);
int ans;
printf("What would you like to do?\n");
printf("\t1- Make a Donation\n");
printf("\t2- Make an investment\n");
printf("\t3- Print balance of fund\n");
printf("\t4- Quit\n");
scanf("%d", &ans);
if (ans == 1) {
printf("How much would you like to donate?\n");
scanf("%d", &donation);
}
if (ans == 2) {
printf("How much would you like to invest?\n");
scanf("%d", &investment);
return main();
}
if (ans == 3) {
total = donation + fund - investment;
if (total < fund) {
printf("You cannot make an investment of that amount\n");
return main();
}
else {
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
}
}
if (ans == 4) {
printf("Type 4 to quit\n");
}
else {
printf("Not a valid option.\n");
}
//switch
switch (ans) {
case 1:
printf("How much would you like to donate?\n");
scanf("%d", &donation);
return main();
case 2:
printf("How much would you like to invest\n");
scanf("%d", &investment);
return main();
case 3:
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
return main();
case 4:
break;
}
return 0;
}
You should put in a loop the part that you want to be repeated, using a varaible to decide when to stop.
here is an example, also in the switch costruct you should usa a default option, maybe to warn the user that the option they inserted is not valid.
int main() {
// variables
int donation, investment, fund, total, tdonations, tinvestments;
// prompt user
printf("Welcome!\n");
printf("What is the initial balance of the fund\n");
scanf("%d", &fund);
//loop until the user decide so
int exit = 0;
while(exit == 0){
int ans;
printf("What would you like to do?\n");
printf("\t1- Make a Donation\n");
printf("\t2- Make an investment\n");
printf("\t3- Print balance of fund\n");
printf("\t4- Quit\n");
scanf("%d", &ans);
//switch
switch (ans) {
case 1:
printf("How much would you like to donate?\n");
scanf("%d", &donation);
break;
case 2:
printf("How much would you like to invest\n");
scanf("%d", &investment);
break;
case 3:
total = donation + fund - investment;
if (total < fund) {
printf("You cannot make an investment of that amount\n");
}
else {
printf("The current balance is %d\n", total);
printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
}
break;
case 4:
exit = 1;
break;
default:
printf("Incorrect option\n");
break;
}
}
return 0;
}

C Program are closing automatically

I created a C Program that will take all the order of the user then generate the grandtotal of the orders.
But when I will order another food, the program is closing automatically.
I don't know if this is about my getch or the breaks in my switch method. Sometimes, it will proceed to take another error but it automatically outputs "INVALID FOOD".
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void menu();
void foods();
void main();
char food;
int quantity;
float price;
float total;
float grandtotal;
int choice;
void main()
{
clrscr();
menu();
foods();
getch();
}
void menu(){
food = ' ';
quantity = 0;
price = 0;
total = 0;
choice = 0;
printf("Please select food from the following:\n");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches \n");
printf("Enter food:");
scanf("%c", &food);
}
void foods(){
switch(food)
{
case 'B':
printf("You selected Burger!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 95.50;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
case 'F':
printf("You selected French Fries!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 47.75;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
case 'P':
printf("You selected French Pizza!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 105.00;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
menu();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
case 'S':
printf("You selected Sandwiches\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 75.50;
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
total = price*quantity;
if(choice == 1){
main();
break;
}
else if (choice == 2){
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
break;
}
default:
printf("INVALID FOOD!");
break;
}
}
I wish someone could help or guide me. Thanks in advance.
In you code you have duplicated multiple times:
...
if(choice == 1){
menu();
break;
} ...
...
So when you choose choice = 1 then menu() get's displayed, and then the code breaks out of foods(). I think you meant to do the foods section again:
...
if(choice == 1){
menu();
foods();
break;
} ...
...
Yet another problem in your code is the %c scanf modifier. It will not eat up leading whitespaces, so it will read a newline (inputted on the last scanf). Use a leading space " %c" to tell scanf to read up leading whitespaces and ignore the leading newline, in scanf(" %c", &food);
Indent your code.
Don't duplicate statements. The whole scanf(... &choice); if (choice == 1) ... else if (choice == 2) could be placed outside the while switch not beeing duplicated 4 times.
Nesting functions using recursive calls can make your stack run out. Better just use a while loop.
Try not to use global variables. They are misleading and lead to maintainable code.
A slightly modified version of you code with a bit of indententation, added a do ... while loop and removed global variables and code duplication, may look like this:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char menu(void);
float foods(char food);
void main()
{
clrscr();
float grandtotal = 0;
int choice = 0;
do {
// print menu and choose the food
char food = menu();
// choose food quantity and get it's price
float total = foods(food);
// print the total price
grandtotal = grandtotal + total;
printf("\n Total Price is: %0.2f", grandtotal);
// do you want to continue?
printf("\n Do you want to order more? [1] Yes [2] No:");
if (scanf("%d", &choice) != 1) {
perror("scanf error");
abort();
}
// continue until choice is equal to 1
} while (choice != 1);
}
char menu(void)
{
char food;
printf("Please select food from the following:\n");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches \n");
printf("Enter food:");
if (scanf(" %c", &food) != 1) {
perror("scanf error");
abort();
}
return food;
}
float foods(char food){
float price = 0;
switch (food) {
case 'B':
printf("You selected Burger!\n");
price = 95.50;
break;
case 'F':
printf("You selected French Fries!\n");
price = 47.75;
break;
case 'P':
printf("You selected French Pizza!\n");
price = 105.00;
break;
case 'S':
printf("You selected Sandwiches\n");
price = 75.50;
break;
default:
fprintf(stderr, "INVALID FOOD!\n");
abort();
}
printf("Enter quantity:");
int quantity;
if (scanf("%d", &quantity) != 1) {
perror("scanf error");
abort();
}
return (float)price * (float)quantity;
}
When you call menu after user input is [1] yes. with menu() function show menu and after menu you should show call food() function.
HERE WHAT YOU WANT
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void menu();
void foods();
void main();
char food;
int quantity;
float price;
float total;
float grandtotal;
int choice;
void main()
{
clrscr();
do {
menu();
foods();
printf("\n Do you want to order more? [1] Yes [2] No:");
scanf("%d", &choice);
getchar(); // <== remove newline
grandtotal = grandtotal + total;
} while (choice == 1);
printf("\n Total Price is: %0.2f", grandtotal);
getch();
}
void menu() {
food = ' ';
quantity = 0;
price = 0;
total = 0;
choice = 0;
printf("Please select food from the following:\n");
printf(" B = Burger, F = French Fries, P = Pizza, S = Sandwiches \n");
printf("Enter food:");
scanf("%c", &food);
}
void foods() {
switch (food)
{
case 'B':
printf("You selected Burger!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 95.50;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
//getchar(); // <== remove newline
total = price*quantity;
break;
//if (choice == 1) {
// menu();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
case 'F':
printf("You selected French Fries!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 47.75;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
total = price*quantity;
break;
//if (choice == 1) {
// menu();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
case 'P':
printf("You selected French Pizza!\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 105.00;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
total = price*quantity;
break;
//if (choice == 1) {
// menu();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
case 'S':
printf("You selected Sandwiches\n");
printf("Enter quantity:");
scanf("%d", &quantity);
price = 75.50;
//printf("\n Do you want to order more? [1] Yes [2] No:");
//scanf("%d", &choice);
total = price*quantity;
break;
//if (choice == 1) {
// main();
// break;
//}
//else if (choice == 2) {
// grandtotal = grandtotal + total;
// printf("\n Total Price is: %0.2f", grandtotal);
// break;
//}
default:
printf("INVALID FOOD!");
break;
}
}

Saving data in files in c

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.

C - 2 or more input validation inside while loop

I have a program in which it will ask for the user to record the student's name and grade. One of the functions I used is for the user to edit the record of the student. The code works but now what I'm scratching my head is how to implement a validation of input by the user. I tried searching but I can't find the exact answer I need.
I already implemented the first validation and it works, but now what I want for the second validation is to go back and ask again instead of breaking the loop.
Here's my code:
void update(struct data list[80], int num)
{
int count, tmp, rolltmp, option;
system("cls");
while(tmp < 1 || tmp > num)
{
printf("How many student records do you want to edit?: ");
scanf("%d",&tmp);
if(tmp >=1 && tmp <=num)
{
printf("\nEditing the Student Record: ");
for(count=0;count<tmp;count++)
{
printf("\nEnter Roll Number for Student #%d: ",count+1);
scanf("%d",&rolltmp);
if(rolltmp >=1 && rolltmp <= num)
{
fflush(stdin);
printf("\n[CHOICES]");
printf("\n[1] - Edit the Name\n[2] - Edit the Grade\n[3] - Edit Both Name and Grade");
printf("\nEnter Choice: ");
scanf("%d",&option);
switch(option)
{
case 1:
fflush(stdin);
printf("\nEnter New Name: ");
gets(list[rolltmp-1].name);
break;
case 2:
printf("\nEnter New Grade: ");
scanf("%d",&list[rolltmp-1].grades);
break;
case 3:
fflush(stdin);
printf("\nEnter New Name: ");
gets(list[rolltmp-1].name);
fflush(stdin);
printf("\nEnter New Grade: ");
scanf("%d",&list[rolltmp-1].grades);
break;
}
}
else
{
printf("\nNot Valid. Please enter from 1 to %d\n",num);
printf("Press any key to enter again...\n\n");
getch();
break;
}
}
}
else
{
printf("Not Valid. Please enter from 1 to %d",num);
getch();
break;
}
}
}

Resources