Remove user in array Struct program - c

I'm working in a struct array program to make some simple user applications. Below is a compilable code, it is not the full code, but it shows that the function to remove a user is not working. I'm new to C and I am not sure about how to do this user remotion. I thought about something related to a null array, but it didn't work. The program asks to enter the name, so the user will be removed.
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define max_users 50
int num = 0; //variable to count the number of users
typedef struct
{
char name[50];
int age;
}user;
user number[max_users];
user aux[max_users];
user read_user()
{
user user;
printf("\nEnter your name: ");
scanf("%s", &(user.name));
printf("\nEnter your age: ");
scanf("%d",&(user.age));
return user;
}
void printUser(user user)
{
printf("\n-----------------------\n");
printf("\nName: %s", user.name);
printf("\nAge: %d", user.age);
}
void enterUser()
{
number[num] = read_user();
printUser(number[num]);
num++;
}
void printAllUsers()
{
for(int i = 0; i < num; i++)
{
printUser(number[i]);
}
}
void performUserRemotion(char name[])
{
char null[50] = {"\0"};
for (int i = 0; i < num; i++) {
if (strcmp(name, number[i].name) == 0)
{
strcpy(number[i].name, null);
printf("\n*** User removed ***");
i--;
}
else
{
printf("\n*** No user ***");
break;
}
}
}
void removeUser()
{
char name_search[50];
int i;
printf("Enter the user to be removed: ");
scanf("%s", &name_search);
performUserRemotion(name_search);
}
int main()
{
int choice;
while(1)
{
printf("\n\nRECORD: ");
printf("\n1) Enter User");
printf("\n2) Remove User");
printf("\n3) Print all users");
printf("\n4) Exit");
printf("\n\nEnter the command >> ");
scanf(" %d", &choice);
switch(choice)
{
case 1:
enterUser();
break;
case 2:
removeUser();
break;
case 3:
printAllUsers(num);
break;
case 4:
printf("\n*** Exit ***\n");
exit(0);
break;
}
}
}
In the function below I planned to remove all the information about the user, so when I enter 3) Print all users, the user removed does not appear anymore and the array is reduced by 1.
void performUserRemotion(char name[])
{
char null[50] = {"\0"};
for (int i = 0; i < num; i++) {
if (strcmp(name, number[i].name) == 0)
{
strcpy(number[i].name, null);
printf("\n*** User removed ***");
i--;
}
else
{
printf("\n*** No user ***");
break;
}
}
}
Pictures A and B are the user's registrations. Picture C is when you enter the command to remove a user, in this case, I registered 'user' and 'name' and wanted 'user' to be removed, so when I enter 3) Print all users, the screen only shows 'name'.
The program still has some basic problems, such as it can't read a name with space, but I will work on it later. The main problem is that I can't perform the remotion of a user.

The logic of removing a user is wrong, because when you print all users, you print the whole content of the array. So to remove a user, you must have somewhere num = num - 1;.
A simple way is to find the index of the user to remove, and then consistently erase every record with next one:
void performUserRemotion(char name[])
{
char null[50] = { "\0" };
int i;
// find the name
for (i = 0; i < num; i++) {
if (strcmp(name, number[i].name) == 0)
{
break;
}
}
if (i == num) {
printf("\n*** No user ***");
}
else {
// remove the user from the array
for (; i < num - 1; i++) {
memcpy(number + i, number + i + 1, sizeof(*number));
}
num -= 1;
printf("\n*** User removed ***");
}
}
You still have a number of minor problems like unused variables, so when everything will work, you should ask for a full review on Code Review

Related

Can you help me find where the problem is and fix it? The while loop is not working perfectly

So I'm trying to make a knock off version of Instagram, just for fun purposes, and the whole program is fine, when I select one it opens to create an account or if I select two, it opens to sign up. But if I select random numbers, it brings up the error message to select a number between one and two but when I select 1 or 2 after that message it just ends my program. What did I do wrong?
#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 15 // Setting the board size
#define CHAIN 5
void init_board(char board[BOARD_SIZE][BOARD_SIZE], int size); /* Creating the functions */
void print_board(char board[BOARD_SIZE][BOARD_SIZE], int size);
struct CREATEacc
{
char username[16]; // Struct for creating an account
char password[20];
}ca;
struct signIn
{
char userACC[16]; // Struct for signing in
char kodikos[20];
}si;
int main(int argc, char* argv)
{
struct CREATEacc userchoice; // Users actions stored here
struct signIn details; // Users actions stored here
int choice, uchoice, uschoice, size, col = 17, row = 19; // Some integers that are needed for below
char board[BOARD_SIZE][BOARD_SIZE];
size = BOARD_SIZE;
printf("Welcome to Diogram\n");
printf("1. Create an account.\n"); // Diogram menu
printf("2. Already have an account? Sign in.\n");
scanf("%d", &choice);
// If user selects 1 then this is the code for creating an account
if (choice == 1)
{
printf("Username: \n");
scanf("%15s", &userchoice.username);
fgets(ca.username, sizeof(ca.username), stdin);
printf("Password: \n");
scanf("%19s", &userchoice.password);
fgets(ca.password, sizeof(ca.password), stdin);
printf("Username: %s\nPassword: %s\n", userchoice.username, userchoice.password);
printf("Account succesfully created!\n\n\n");
printf("Welcome, %s\n");
printf("1.HOME 2.SEARCH 3.PROFILE");
init_board(board, size);
print_board(board, size);
do
{
printf("\n\nHOME SEARCH PROFILE\n"); // Printing parts of the app
scanf("%d", &uchoice); // Scans what the user selects
if (uchoice == 1)
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE"); // If selected, User's Diogram Home opens
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
else if (uchoice == 2) // If selected, User's Diogram Search opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE");
printf("\nSearch an account here: ");
scanf("%s");
printf("Oops! Looks like you're the only one using Diogram :(\nGo back?\n");
}
else if (uchoice == 3) // If selected, User's Diogram Profile opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n");
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", userchoice.username);
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
while (uchoice == 4) // If selected, user has the option to exit, and therefore end the program
{
char ch; // Character for checking Y/N
printf("\n\nAre you sure you want to log out(y/n)?\n");
scanf (" %c", &ch); // Scanning users anwser
// If nor y or n pressed, it does nothing until user finally picks y or n
while (ch !='y' && ch !='n')
{
scanf("%c", &ch);
}
if ( ch == 'y')
{
printf("Logged out succesfully!\n ");
exit(EXIT_SUCCESS); // Use exit() function to terminate the execution of the program
}
if (ch == 'n') // If user picks 'n' then it reverts back to his Diogram Profile
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n");
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", userchoice.username);
init_board(board, size);
print_board(board, size);
printf("\n\nHOME SEARCH PROFILE\n EXIT\n");
scanf("%d", &uchoice);
}
}
} while (uchoice !=4); // All of that on a do while loop so it never ends unless user decides to exit
}
// If user selects 2 then this is the code for signing in
else if (choice == 2)
{
printf("Username: \n");
scanf("%15s", &details.userACC);
fgets(si.userACC, sizeof(si.userACC), stdin);
printf("Password: \n");
scanf("%s", &details.kodikos); // User details
printf("Logged in succesfully!\n");
printf("Welcome back %s!\n", details.userACC);
printf("1.HOME 2.SEARCH 3.PROFILE");
init_board(board, size);
print_board(board, size);
do
{
printf("\n\nHOME SEARCH PROFILE\n"); // Printing parts of the app
scanf("%d", &uschoice); // Scans what the user selects
if (uschoice == 1) // If selected, User's Diogram Home opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE"); // Printing parts of the app
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
else if (uschoice == 2) // If selected, User's Diogram Search opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE");
printf("\nSearch an account here: ");
scanf("%s");
printf("Oops! Looks like you're the only one using Diogram :(\nGo back?\n");
}
else if (uschoice == 3) // If selected, User's Diogram Profile opens
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n"); // Printing parts of the app
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", details.userACC);
init_board(board, size); // Calling function to initiate the board
print_board(board, size); // Printing the board
}
while (uschoice == 4) // If selected, user has the option to exit, and therefore end the program
{
char ch; // Character for checking Y/N
printf("\n\nAre you sure you want to log out(y/n)?\n");
scanf (" %c", &ch); // Scanning users anwser
// If nor y or n pressed, it does nothing until user finally picks y or n
while (ch !='y' && ch !='n')
{
scanf("%c", &ch);
}
if ( ch == 'y')
{
printf("Logged out succesfully!\n ");
exit(EXIT_SUCCESS); // Use exit() function to terminate the execution of a program
}
// If user picks 'n' then it reverts back to his Diogram Profile
if (ch == 'n')
{
printf("\n\n1.HOME 2.SEARCH 3.PROFILE 4.EXIT\n");
printf(" ____\n| |\n|____|\n%s\n\nPOSTS\n", details.userACC);
init_board(board, size);
print_board(board, size);
printf("\n\nHOME SEARCH PROFILE\n EXIT\n");
scanf("%d", &uschoice);
}
}
} while (uschoice !=4); // All of that on a do while loop so it never ends unless user decides to exit
}
while (choice != 1 && choice != 2)
{
printf("Oops! Looks like you did something wrong!\n");
printf("Try picking between 1 - 2.\n");
scanf("%d", &choice);
}
return 0; // End of the main program
}
void init_board(char board[BOARD_SIZE][BOARD_SIZE], int size)
{
for (int row = 0; row < BOARD_SIZE; ++row)
{
for (int col = 0; col < BOARD_SIZE; ++col) // Function for initiating the board
{
board[row][col] = '+';
}
}
}
// Function for printing the board
void print_board(char board[BOARD_SIZE][BOARD_SIZE], int size)
{
printf("\n ");
int row, col;
for (int i = 0; i < BOARD_SIZE + 6; ++i)
{
printf("---");
}
printf("\n");
for (int row = 0; row < BOARD_SIZE + 2; ++row)
{
printf("| ");
}
printf("\n");
for (int row = 0; row < BOARD_SIZE; ++row)
{
printf("|--");
for (int col = 0; col < BOARD_SIZE; ++col)
{
printf("-");
printf("%c", board[row][col]);
printf("--");
}
printf("-|");
printf("\n");
printf("|");
if (row != BOARD_SIZE - 1)
{
printf(" | | | | | | | | | | | | | | | |\n");
}
if (row == BOARD_SIZE - 1)
{
for (int i = 0; i < BOARD_SIZE + 1; ++i)
{
printf(" |");
}
printf("\n ");
for (int i = 0; i < BOARD_SIZE + 6; ++i)
{
printf("---");
}
}
}
}
I was expecting that the while loop actually works, because if you see the while loop, it has no flaws, the problem is somewhere else, but I can't seem to find where... Any help? :D
It's totally normal, after while(choice != 1 && choice != 2) you have return 0;
What you need to do is change the structure of your code, to something like that:
main() {
while(;;){
scanf()
if (choice == 1) { ... } // Choice #1
if (choice == 2) { ... } // Choice #2
if (choice == 0) { break } // Exit the app
else { printf("Ops! Tray again."); } // Wrong choice
}
}

My Array item keeps getting overwritten in C programming

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 cant seem to understand how to restrict my scanf to only numbers of float

#include <stdio.h>
#include <string.h>
#define mL 5
#define NL 20
#define UL 6
struct LIST
{
char n[NL];
float am;
char u[UL];
};
struct array
{
struct LIST array;
};
void addCityInformation(struct array *add, int *items);
void printCities(struct array *all, int items);
int main(void)
{
struct array shopping[mL];
int choice, nrOfItemsAdded = 0;
do
{
printf("\nWhat du you want to do?");
printf("\n1 - add grocery");
printf("\n2 - print shopping list");
printf("\n3 - exit");
printf("\nYour choice: ");
scanf("%d", &choice);
while(getchar() != '\n');
switch (choice)
{
case 1:
addCityInformation(&shopping[nrOfItemsAdded], &nrOfItemsAdded);
break;
case 2:
printCities(shopping, nrOfItemsAdded);
break;
case 3:
printf("Exiting program\n\n");
break;
default:
printf("Invalid input\n\n");
break;
}
}
while(choice != 3);
return 0;
}
int clean_stdin()
{
while (getchar()!='\n');
}
void addCityInformation(struct array *add, int *items)
{
if(*items == mL)
printf("No more space in the list\n");
else
{
printf("Enter name: ");
fgets(add->array.n, NL, stdin);
add->array.n[strlen(add->array.n)-1] = '\0';
do {
printf("Enter amount: ");
}while (scanf("%f", &add->array.am )); //loop untill other than float
getchar();
printf("Enter unit: ");
fgets((add->array.u), UL, stdin);
add->array.u[strlen(add->array.u)-1] = '\0';
(*items)++;
}
}
void printCities(struct array *all, int items)
{
printf("\n\n%-20s %-15s %-9s | %-6s\n", "Name", "amount", "unit");
printf("--------------------------------------------------------\n");
for(int i = 0; i < items; i++)
printf("%-20s %-15.1f %-9.4s \n", all[i].array.n, all[i].array.am, all[i].array.u);
}
This is my loop beside that i am only showing a part of the code. It now just continues to give enter amount and letting me register it in the struct. I want to restrict the user to only entering positive numbers and no character at all. And if he types a character it should rerun the loop even if it is 123Av123 it should run the loop and only register the correct number
Edit: now showing the whole code//loop untill other than float is what i want help with
int check=scanf("%f", &add->array.am )
if(check!=1||add->array.am<0){
printf("Incorrect input");
return 1;
}
I think that will do it.
Edit: you wanted it to rerun after so use continue; instead of return;

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.

Searching through struct array for string entered by user

This is just a function from my program that is supposed to search for the string or integer the user enters in the struct array. What I'm trying to do is add an error message and the ability to try again when that entered string or integer is not found in the struct array. It searches just fine if you enter the correct string or integer but nothing happens if you don't. I'm trying to change that but can't find the solution.
I've tried for a while now with one of the cases in my switch statement but I need to do it for all three. But so far I've only tried on case 3.
search(struct items aItems[], int *num_items)
{
int choice_of_search, b=0, found_word=0, search_num, i=0, j=0, k=0;
char search_phrase[20]; struct items search[MAX];
printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
scanf("%d", &choice_of_search);
while(choice_of_search < 1 || choice_of_search > 3)
{
printf("Wrong choice!\n");
printf("Choose what to search for? (1) Item number, (2) Name and (3) Balance. ");
scanf("%d", &choice_of_search);
}
switch(choice_of_search)
{
case 1:
printf("Item number?\n");
scanf("%d", &search_num);
for(i = 0; i < *num_items; i++)
{
if(search_num == aItems[i].itemnumber)
{
printf("Item number found!\n");
search[found_word]=aItems[i];
found_word+=1;
}
}
break;
case 2:
printf("Name?\n");
scanf("%s", search_phrase);
for(i = 0; i < *num_items; i++)
{
if(strstr(aItems[i].name, search_phrase))
{
printf("Name found!\n");
search[found_word]=aItems[i];
found_word+=1;
}
}
break;
case 3:
printf("Balance?\n");
scanf("%d", &search_num);
for(i = 0; i < *num_items; i++)
{
if(search_num == aItems[i].balance)
{
printf("Balance found!\n");
search[found_word]=aItems[i];
found_word+=1;
}
else
{
printf("Balance not found! Try again.\n");
printf("Balance?\n");
scanf("%d", &search_num);
}
}
break;
}
while(b < found_word)
{
printf("Item number: %d Name: %s Balance: %d\n", search[b].itemnumber, search[b].name, search[b].balance);
b++;
}
}
Maybe this can help
int done;
...
...
case 3:
done = 0;
while(1);
{
printf("Balance?\n");
scanf("%d", &search_num);
for(i = 0; i < *num_items; i++)
{
if(search_num == aItems[i].balance)
{
printf("Balance found!\n");
search[found_word]=aItems[i];
found_word+=1;
done = 1;
}
}
if (done) break;
printf("Balance not found! Try again.\n");
}
break;
But notice that the code isn't user friendly as it doesn't allow the user a way to stop the search without a match. So maybe you should consider adding a "Would you like try again" option.
A simple approach could be
printf("Balance not found! Would like to try again?.\n");
scanf(" %c", &some_char);
if (some_char != 'y') break;

Resources