calling a function on another fuction in the same file - c

i have an error massage but i don't undestand what is the problem. i'm deperately searching for the problem.
i tried to create a libary function, and include one of the function on another function in the same file. Is this posibble ? if so please expalain.
#include "head.h"
void fungsiCetak()
{
int i,n;
system("cls");
system("color 1f");
FILE*nip,*nama,*golongan,*anak,*kk,*birth;
nip=fopen("nip.txt","a");
nama=fopen("nama.txt","a");
anak=fopen("jum_anak.txt","a");
kk=fopen("jum_kk.txt","a");
birth=fopen("birth.txt","a");
golongan=fopen("golongan.txt","a");
//////////////////////////////////////////////////////////////////
fflush(stdin);
printf("Berapa data yang ingin di inputkan ? ");
scanf("%d",&n);
i=1;
do{
fflush(stdin);
printf(" Masukan NIP : ");
gets(pegawai.nip);
fprintf(nip,"%s\n",pegawai.nip);
printf("Masukan nama Pegawai : ");
gets(pegawai.nama);
fprintf(nama,"%s\n",pegawai.nama);
printf("Masukan golongan : ");
scanf("%d",&pegawai.gol);
fprintf(golongan,"%d\n",pegawai.gol);
printf("Jumlah anak : ");
scanf("%d",&pegawai.jum_anak);
fprintf(anak,"%d\n",pegawai.jum_anak);
printf("Jumlah anggota keluarga : ");
scanf("%d",&pegawai.jum_kk);
fprintf(kk,"%d\n",pegawai.jum_kk);
printf("Tanggal lahir (dd-mm-yyyy) : ");
scanf("%d-%d-%d",&pegawai.birth.hari,
&pegawai.birth.bulan,
&pegawai.birth.tahun);
fprintf(birth,"%d-%d-%d\n",pegawai.birth.hari,
pegawai.birth.bulan,
pegawai.birth.tahun);
printf("\n");
i++;
} while(i<=n);
fclose(nip);
fclose(nama);
fclose(golongan);
fclose(anak);
fclose(kk);
fclose(birth);
}
void fungsiBaca()
{
system("cls");
int count=0;
int nip2,nip1[20],anak1[20],kk1[20];
int gol[20];
char birth1[30][30];
char bufer[100],buffer1[100];
char nama1[30][30];
struct pegawai;
FILE*nip,*nama,*golongan,*anak,*kk,*birth;
nama=fopen("nama.txt","r");
i=0;
while(fgets(bufer,sizeof bufer,nama)!=NULL)
{
strcpy(nama1[i],bufer);
i++;
}
a=0;
golongan=fopen("golongan.txt","r");
while(a<=i)
{
fscanf(golongan,"%d",&gol[a]);
a++;
}
nip=fopen("nip.txt","r");
a=0;
while(a<i)
{
fscanf(nip,"%d",&nip1[a]);
a++;
}
anak=fopen("jum_anak.txt","r");
a=0;
while(a<i)
{
fscanf(anak,"%d",&anak1[a]);
a++;
}
kk=fopen("jum_kk.txt","r");
a=0;
while(a<i)
{
fscanf(kk,"%d",&kk1[a]);
a++;
}
birth=fopen("birth.txt","r");
a=0;
while(fgets(buffer1,sizeof buffer1,birth)!=NULL)
{
strcpy(birth1[a],buffer1);
a++;
}
search :
fflush(stdin);
printf("----------------------------------------\n");
printf("masukan NIP : ");
scanf("%d",&nip2);
printf("----------------------------------------\n");
for(a=0; a<=i; a++)
{
if(nip1[a]==nip2){
printf("Nama pegawai : %s",nama1[a]);
printf("Golongan : %d\n",gol[a]);
printf("Jumlah anak : %d\n",anak1[a]);
printf("Jumlah KK : %d\n",kk1[a]);
printf("Tanggal Lahir : %s\n",birth1[a]);
count=1;
}
}
if (count==0)
{
printf("Maaf data tidak ditemukan!\n");
}
fflush(stdin);
printf("Ingin mencari data kembali ? [y/n]");
scanf("%c",&kembali);
if (kembali=='y')
{
system("cls");
goto search;
}
fclose(nip);
fclose(nama);
fclose(golongan);
fclose(kk);
fclose(anak);
fclose(birth);
}
void fungsiGaji ()
{
int gol,absen;
float tunj_makan,tunj_anak,tunj_kes,total,gajih_pokok;
fungsiBaca();
//////////////////////////////////////////////////Kondisi gajih pokok
if (gol[a]==1)
{
gajih_pokok=GOl1;
} else if (gol[a]==2)
{
gajih_pokok=GOL2;
} else if (gol[a]==3)
{
gajih_pokok=GOl3;
} else
{
gajih_pokok=GOL4;
}
printf("Gajih pokok : %.2f",gajih_pokok);
/////////////////////////////////////////////// kondisi tunjangan
}
the error message is
I use CodeBlocks to create the program

The problem is here:
int gol,absen; <--- gol is integer
...
if (gol[a]==1) <---- gol[a] ?

Related

How do i send a string to another function using c?

i am trying to send a string that is "genrecode" back to my main function from the my other function known as char Vote. At first, i used the 'int' data type for the variable "genreCode" since its just integers but i wanted to see if i can send it as a string. How do i do that?
#include <stdio.h>
#include<string.h>
void View_Genre();
char Vote();
void View_Result(int, int, int);
void Exit(int, int , int);
int main ()
{
int option;
char genreCode[2];
int rock=1;
int pop=1;
int country=1;
int Trock=0;
int Tcountry=0;
int Tpop=0;
printf("WELCOME TO MUSIC GENRE VOTING SYSTEM\n");
printf("View Genre\n");
printf("Vote\n");
printf("View Result\n");
printf("Exit\n");
do
{
printf("\nInput option : ");
scanf("%d", &option);
switch(option)
{
case 1 : View_Genre();
break;
case 2 : genreCode=Vote();
if(strcmp(genreCode,"11")==0)
{
Trock=Trock+rock;
}
else if(strcmp(genreCode,"22")==0)
{
Tpop=Tpop+pop;
}
else if(strcmp(genreCode,"33")==0)
{
Tcountry=Tcountry+Tpop;
}
break;
case 3 : View_Result(Trock, Tpop, Tcountry);
break;
case 4 :Exit(Trock, Tpop, Tcountry);
}
}while(option==1 || option==2 || option==3);
printf("\nThank you.");
return 0;
}
void View_Genre()
{
printf("GENRE CODE\n");
printf("ROCK 11 \n");
printf("Pop 22 \n");
printf("Country 33 \n\n");
}
char Vote()
{
char genreCode[2];
printf("Enter genre code : ");
scanf("%d", &genreCode);
if(strcmp(genreCode,"11")==0)
{
printf("Your favourite genre is Rock");
}
else if(strcmp(genreCode,"22")==0)
{
printf("Your favourite genre is Pop");
}
else if(strcmp(genreCode,"33")==0)
{
printf("Your favourite genre is Country");
}
return genreCode;
}
void View_Result(int Nrock, int Npop, int Ncountry)
{
printf("GENRE VOTES\n");
printf(" Rock %d \n", Nrock);
printf(" Pop %d \n", Npop);
printf(" Country %d \n", Ncountry);
}
void Exit(int Nrock,int Npop,int Ncountry)
{
char popular[10];
if(Nrock!=0 || Npop!=0 || Ncountry!=0)
{
if(Nrock>Npop)
{
if(Nrock>Ncountry)
{
strcpy(popular,"Rock");
}
else
{
strcpy(popular,"Country");
}
}
else if(Npop>Ncountry)
{
strcpy(popular,"Pop");
}
else
{
strcpy(popular,"Country");
}
printf("The most popular music genre is : %s\n", popular);
}
else
{
printf("\nYou have not vote any genre\n");
}
}
The rest are all correct. I just need to find out how do i send back a string to my main function.
You could do this by allocating space in the heap for your genreCode variable.
//...
#include <stdlib.h>
//...
int main ()
{
// ...
//allocate space for 3 chars in the heap
//the third char stores the string terminator '\0'
char* genreCode = malloc(sizeof(char)*3);
// ...
do
{
//...
switch(option)
{
//...
case 2 : Vote(genreCode);
//...
break;
//...
}
}while(option==1 || option==2 || option==3);
//...
free(genreCode); //Free the allocated memory
return 0;
}
//...
void Vote(char* genreCode)
{
printf("Enter genre code : ");
scanf("%s", genreCode);
//...
}
//...
Reference:
malloc() - Tutorialspoint

Search function in CRUD with C

I'm making a contact book in C and I've already done the parts of registering contact, listing all contacts and deleting a contact, but I need to elaborate the part of searching the contact, and also searching contacts that start with a given string, but I'm not succeeding... I'll leave my code below to see if anyone can help me identify where I'm going wrong:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 50
typedef struct
{
char name[MAX_LENGTH];
char number[MAX_LENGTH];
int bd;
char bdm[MAX_LENGTH];
} ContactBook;
void ListContacts(ContactBook **c, int quant)
{
int i;
printf("\n List of contacts: \n");
for (i = 0; i < quant; i++)
{
printf("\t%d = birthday: %2d month %s\t name: %s \t number: %s\n", i + 1, c[i]->bd, c[i]->bdm, c[i]->name, c[i]->number);
}
}
int addContacts(ContactBook **c, int quant, int size)
{
if (quant < size)
{
ContactBook *new = malloc(sizeof(ContactBook));
printf("\nenter contact name: ");
scanf("%s", new->name);
printf("\nenter number: ");
scanf("%s", new->number);
printf("\nenter the birthday ");
scanf("%d", &new->bd);
printf("\n enter the month birthday: ");
scanf("%s", new->bdm);
c[quant] = new;
return 1;
}
else
{
printf("\n full list.\n");
return 0;
}
}
int deleteContact(ContactBook **c, int quant)
{
int id;
ListContacts(c, quant);
printf("\n\t Enter the id you want to delete: \n");
scanf("%d", &id);
getchar();
id--;
if (id >= 0 && id < quant)
{
free(c[id]);
if (id < quant - 1)
{
c[id] = c[quant - 1];
}
return -1;
}
else
{
printf("\n\t wrong code;\n");
return 0;
}
}
void SearchContact(ContactBook **c, int quant)
{
int i;
char searchedName[30];
for (i = 0; i < quant; i++)
{
printf("\n Search name: \n");
scanf("%s", searchedName);
getchar();
if (strcmp(searchedName, c[i]->name) == 0)
{
printf("the name: %s was found", c[i]->name);
}
else
{
printf("name not found");
}
}
}
int main()
{
ContactBook *contacts[50];
int option, size = 50, quant = 0;
do
{
printf(" \n\t0 - exit\n\t1 - register contact\n\t2 - Remove contact\n\t3- List contacts\n\t4- Search contact\n\t");
scanf("%d", &option);
getchar();
switch (option)
{
case 1:
quant += addContacts(contacts, quant, size);
break;
case 2:
quant += deleteContact(contacts, quant);
break;
case 3:
ListContacts(contacts, quant);
break;
case 4:
SearchContact(contacts, quant);
break;
}
} while (option != 0);
return 0;
}

assignment to expression with array type error in c

I am getting this error as shown in the image pls tell me how to fix it.
the error is in line 115 of the code
#include<stdio.h>
#include<string.h>
#define MAX 100
#define product_limit 50
typedef struct {
int p_id;
char p_name[product_limit + 1];
int p_quantity;
int unit_price;
char type_product[50];
} product;
int main()
{
int ok = 1, menu;
do
{
menu = display_menu();
if(menu < 1 || menu > 7)
{
printf("Invaid option.\n");
}
else
{
switch(menu)
{
case 1:
add_product();
break;
case 2:
change_product();
break;
case 3:
display_all_products();
break;
}
}
}
while(ok);
printf("Exiting Program.\n");
return 0;
}
int display_menu()
{
int menu;
printf("Choose option from below.\n");
printf("1. Add new product.\n");
printf("2. Update product.\n");
printf("3. Display products.\n");
printf(". Exit.\n");
scanf("%d", &menu);
return menu;
}
product products[MAX];
int c_p = 0;
int product_exists(int id)
{
int x;
for(x=0;x<c_p;x++)
{
if(products[x].p_id == id)
{
return x;
}
}
return -1;
}
int add_product()
{
int price,id,quantity,type;
char name[product_limit + 1];
printf("Product ID: ");
scanf("%d", &id);
if(product_exists(id) != -1)
{
printf("ID: %d already exists.\n", id);
return;
}
printf("Enter Product Name: ");
scanf("%s", name);
printf("Enter Quantity: ");
scanf("%d", &quantity);
printf("Enter Price: ");
scanf("%d", &price);
printf("Enter Product Type:");
scanf("%s",type);
products[c_p].p_id = id;
strcpy(products[c_p].p_name, name);
products[c_p].p_quantity = quantity;
products[c_p].unit_price = price;
c_p++;
printf("Product added Successfully\n");
}
int change_product()
{
int id, exists,name;
char z[2];
printf("Product ID: ");
scanf("%d", &id);
exists = change_product(id);
if(exists == -1) {
printf("ID: %d not exists.\n", id);
printf("Type Y to try once more or N back to menu: ");
scanf("%s", z);
if(strcmp(z, "Y") == 0)
{
change_product();
}
} else {
printf("Product found successfully\n");
printf("Product Name: %s\n", products[exists].p_name);
printf("Type new name: ");
scanf("%d", name);
products[exists].p_name += name;
printf("Successfully updated.\n");
}
}
int display_products()
{
int x;
if(c_p == 0)
{
printf("No products were added\n");
return;
}
printf("Products\n\n");
for(x = 0; x < c_p; x++)
{
printf("Product ID: %d\n", products[x].p_id);
printf("Product Name: %s\n", products[x].p_name);
printf("Quantity: %d\n", products[x].p_quantity);
printf("Product price: %d\n", products[x].unit_price);
printf("Product type:%s\n",products[x].p_name);
printf("\n");
}
}
You're trying to add an integer to a string. This is not allowed in C:
products[exists].p_name += name;
Looking at the change_product method it seems like maybe you want to update the name field or concatenate it. I think you should do these things:
Read all compiler warnings and consider addressing them, They are addressing valid concerns like not declaring your functions first, not returning any int value in add_product, display_product etc which is supposed to return int. Provide a function body for display_all_products (Did you mean display_products()?)
Use a string variable for updating name char newName[product_limit + 1] and use library functions from C string library to either concatenate(strcat) or copy(strcpy) newName taken as user input to products[exists].p_name:
char newName[product_limit + 1];
// ...
} else {
printf("Type new name: ");
fgets(newName, product_limit + 1, stdin);
strcpy(products[exists].p_name, newName);
}
}
You can't add integer to string.
You have used string functions like strcpy() and should do the same for this.
You have also made other errors :
declaring int for string(char array)
wrong return data type
calling wrong funtion
I have fixed those errors, do side by side comparison.
#include<stdio.h>
#include<stdlib.h>//here
#include<string.h>
#define MAX 100
#define product_limit 50
//here
void add_product(void);
void change_product(void);
int display_menu(void);
void display_products(void);
int product_exists(int);
typedef struct {
int p_id;
char p_name[product_limit + 1];
int p_quantity;
int unit_price;
char type_product[50];
} product;
int main()
{
int ok = 1, menu;
do
{
menu = display_menu();
if(menu < 1 || menu > 4)
{
printf("Invaid option.\n");
}
else
{
switch(menu)
{
case 1:
add_product();
break;
case 2:
change_product();
break;
case 3:
display_products();//here
break;
case 4:
exit(0);//here
}
}
}
while(ok);
printf("Exiting Program.\n");
return 0;
}
int display_menu()
{
int menu;
printf("Choose option from below.\n");
printf("1. Add new product.\n");
printf("2. Update product.\n");
printf("3. Display products.\n");
printf("4. Exit.\n");
scanf("%d", &menu);
return menu;
}
product products[MAX];
int c_p = 0;
int product_exists(int id)
{
int x;
for(x=0;x<c_p;x++)
{
if(products[x].p_id == id)
{
return x;
}
}
return -1;
}
void add_product() // here
{
int price,id,quantity;
char type[product_limit + 1];
char name[product_limit + 1];//here
printf("Product ID: ");
scanf("%d", &id);
if(product_exists(id) != -1)
{
printf("ID: %d already exists.\n", id);
return;
}
printf("Enter Product Name: ");
scanf("%s", name);
printf("Enter Quantity: ");
scanf("%d", &quantity);
printf("Enter Price: ");
scanf("%d", &price);
printf("Enter Product Type:");
scanf("%s",type);
products[c_p].p_id = id;
strcpy(products[c_p].p_name, name);
products[c_p].p_quantity = quantity;
products[c_p].unit_price = price;
strcpy(products[c_p].type_product,type);//here
c_p++;
printf("Product added Successfully\n");
}
void change_product() //here
{
int id, exists;
char z[2];
char name[product_limit + 1];//here
printf("Product ID: ");
scanf("%d", &id);
exists = product_exists(id);//here
if(exists == -1)
{
printf("ID: %d not exists.\n", id);
printf("Type Y to try once more or N back to menu: ");
scanf("%s", z);
if(strcmp(z, "Y") == 0)
{
change_product();
}
}
else
{
printf("Product found successfully\n");
printf("Product Name: %s\n", products[exists].p_name);
printf("Type new name: ");
scanf("%s", &name);//here
strcpy(products[exists].p_name, name);//here
printf("Successfully updated.\n");
}
}
void display_products() //here
{
int x;
if(c_p == 0)
{
printf("No products were added\n");
return;
}
printf("Products\n\n");
for(x = 0; x < c_p; x++)
{
printf("Product ID: %d\n", products[x].p_id);
printf("Product Name: %s\n", products[x].p_name);
printf("Quantity: %d\n", products[x].p_quantity);
printf("Product price: %d\n", products[x].unit_price);
printf("Product type:%s\n",products[x].type_product);//here
printf("\n");
}
}

Linear probing not working for collision

I am making a hash table based on reg no. The insertion function is working fine but the search and deletion doesn't work in case of collision. It's not doing anything at all. There aren't any compilation errors either. Any help would be appreciated.
int size=4;
struct students
{
char name[50];
int regno;
int age;
char city[50];
}stud[4];
void insertion()
{
int reg,k,i;
printf("\nenter the student details you want to insert:\n");
printf("\nenter regno: ");
scanf("%d",&reg);
k=reg%size;
if (stud[k].regno==0)
{
stud[k].regno=reg;
printf("\nenter name: ");
scanf("%s",&stud[k].name);
printf("\nenter age: ");
scanf("%d",&stud[k].age);
printf("\nenter city: ");
scanf("%s",&stud[k].city);
}
else
{
for(i=k+1;i<size;i++)
{
if (stud[i].regno==0)
{
stud[i].regno=reg;
printf("\nenter name: ");
scanf("%s",&stud[i].name);
printf("\nenter age: ");
scanf("%d",&stud[i].age);
printf("\nenter city: ");
scanf("%s",&stud[i].city);
break;
}
else
for(i=0;i<k;i++)
{
if (stud[i].regno==0)
{
stud[i].regno=reg;
printf("\nenter name: ");
scanf("%s",&stud[i].name);
printf("\nenter age: ");
scanf("%d",&stud[i].age);
printf("\nenter city: ");
scanf("%s",&stud[i].city);
break;
}
}
}
}
}
void deletion()
{
int reg, k;
int f=0;
printf("\nenter the student reg no you want to delete");
scanf("%d",&reg);
k=reg%size;
if(stud[k].regno==0)
{
printf("\nIt is empty");
}
else if(stud[k].regno==reg)
{
printf("The removed student is %d",stud[k].regno);
stud[k].regno=0;
stud[k].name[0]='\0';
stud[k].age=0;
stud[k].city[0]='\0';
printf("\n\n");
}
else
{
int i;
for (i=k+1;i<size;i++)
{
if(stud[k].regno==reg)
{
printf("The removed student is %d",stud[k].regno);
stud[k].regno=0;
stud[k].name[0]='\0';
stud[k].age=0;
stud[k].city[0]='\0';
printf("\n\n");
f=1;
break;
}
}
for(i=0;i<k;i++)
{
if (stud[i].regno==reg)
{
printf("The removed student is %d",stud[k].regno);
stud[k].regno=0;
stud[k].name[0]='\0';
stud[k].age=0;
stud[k].city[0]='\0';
printf("\n\n");
f=1;
break;
}
}
}
if (f==1)
{
printf("\nIt is not present");
}
}
void search()
{
int reg, k;
int f=0;
printf("\nenter the student reg no you want to search");
scanf("%d",&reg);
k=reg%size;
if(stud[k].regno==0)
{
printf("\nIt is empty");
}
else if(stud[k].regno==reg)
{
printf("The student found is: \n");
printf("\nreg no: %d",stud[k].regno);
printf("\nname: %s",stud[k].name);
printf("\nage: %d",stud[k].age);
printf("\ncity: %s",stud[k].city);
printf("\n\n");
}
else
{
int i;
for (i=k+1;i<size;i++)
{
if(stud[k].regno==reg)
{
printf("The student found is: \n");
printf("\nreg no: %d",stud[k].regno);
printf("\nname: %s",stud[k].name);
printf("\nage: %d",stud[k].age);
printf("\ncity: %s",stud[k].city);
printf("\n\n");
f=1;
break;
}
}
for(i=0;i<k;i++)
{
if (stud[i].regno==reg)
{
printf("The student found is: \n");
printf("\nreg no: %d",stud[k].regno);
printf("\nname: %s",stud[k].name);
printf("\nage: %d",stud[k].age);
printf("\ncity: %s",stud[k].city);
printf("\n\n");
f=1;
break;
}
}
if (f==1)
{
printf("\nIt is not present");
}
}
}
void display()
{
int i;
for(i=0;i<size;i++)
{
printf("\nreg no: %d",stud[i].regno);
printf("\nname: %s",stud[i].name);
printf("\nage: %d",stud[i].age);
printf("\ncity: %s",stud[i].city);
printf("\n\n");
}
}
void main()
{
int i,c;
for(i=0;i<size;i++)
{
stud[i].regno=0;
stud[i].name[0]='\0';
stud[i].age=0;
stud[i].city[0]='\0';
}
do {
printf (" Enter 1 for insert \n");
printf (" Enter 2 for deletion \n");
printf (" Enter 3 for search \n");
printf (" Enter 4 for display \n");
scanf ("%d", &c);
switch(c)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: search();
break;
case 4: display();
}
} while(c>0 && c<=4);
}
The problem is in your inner loops.
else
for(i=0;i<k;i++)
{
i is already in use, so you must use a new variable:
else
for(int j=i+1;j<k;j++)
{
This error is present in many places.
I believe your code can be "compressed". I believey our if..else { for..if..else { for..if can be made into a single loop that, starting from reg%size searches for the first empty entry. That would make an elegant solution.

Why `system("cls")` works only if it is called by a certain function?

I am developing a simple hotel reservation management system, but I've encountered a little problem. The system("cls"); in mainMenu() function doesn't work, only if the mainMenu() function is called by the bookRoom() function. I tried works just fine with other function, I have no idea why this happens.
Where is my mistake?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
char exitOpt[1],cfm[1];;
int mainMenu_Opt;
int chk = 0;
int advance[4] = {750, 500, 250, 125};
int roomFee[4] = {1500, 1000, 500 ,250};
int rT[4] = {1,2,3,4};
int roomAvail[4] = {1,2,2,5};
struct guest{
char id[5];
char name[30];
int age;
int r_type;
int chk_in_date;
int chk_out_date;
int per;
int totPay;
int paid;
int balance;
};
struct guest grec;
FILE *fguest,*ftemp;
void main();
void mainMenu();
void checkRoom();
void putRAV();
int bookRoom();
void vldRT();
void readData();
void exitProgram ();
void mainMenu()
{
system("cls");
for(;;)
{
fguest = fopen("guest_list.dat","rb");
while(fread(&grec,sizeof(grec),1,fguest)==1 )
{
if (grec.r_type == chk)
roomAvail[chk-1]--;
}
chk = 0;
fclose(fguest);
printf("\n Welcome to HRMS \n\n");
printf("\tMain Menu\n\n");
printf("1. Check Room's Availability\n\n");
printf("2. Book A Room\n\n");
printf("3. Check Out a Room Guest\n\n");
printf("4. Edit Reservation\n\n");
printf("5. Search\n\n");
printf("6. Exit\n\n");
printf("Please, enter your choice (1-6): ");
scanf("%d",&mainMenu_Opt);
fflush(stdin);
switch(mainMenu_Opt)
{
case 1: { putRAV();
break; }
case 2: {
bookRoom();
break; }
// case 3: { chkoRoom();
// break; }
//
// case 4: { editRes();
// break; }
//
case 5: { readData();
break; }
case 6: { exitProgram();
break; }
default: printf("\nInvalid Input. Please try again with valid input (whole number between 1 - 6).\n ");
}
}
}
void checkRoom()
{
system("cls");
fguest = fopen("guest_list.dat","rb");
while(fread(&grec,sizeof(grec),1,fguest)==1 )
{
switch (grec.r_type)
{
case 1: {
roomAvail[0]--;
break; }
case 2: {
roomAvail[1]--;
break; }
case 3: {
roomAvail[2]--;
break; }
case 4: {
roomAvail[3]--;
break; }
}
}
fclose(fguest);
}
void putRAV()
{
system("cls");
int j;
for (j = 0; j < 4; j++)
{
printf("%d\n", roomAvail[j]);
}
printf("Back to main (Y/N)?: "); gets(cfm); fflush(stdin);
if ((strcmp(cfm,"Y")==0) || (strcmp(cfm,"y")==0))
{ printf("Returning to main menu...\n");
Sleep(1000);
mainMenu(); }
else if ((strcmp(cfm,"N")==0) || (strcmp(cfm,"n")==0))
{ putRAV(); }
else
{ printf("\nInvalid Input. Returning to ReadData\n");
putRAV(); }
}
int bookRoom()
{
system("cls");
fflush(stdin);
grec.totPay = 0;
fguest = fopen("guest_list.dat","ab+");
printf("\n\tBook A Room");
printf("\n\nGuest\'s ID\t\t: "); scanf("%s",grec.id); fflush(stdin);
printf("Guest\'s Name\t\t: "); scanf("%30s",grec.name); fflush(stdin);
printf("Guest\'s Age\t\t: "); scanf("%d",&grec.age); fflush(stdin);
printf("Room\'s Type\t\t: "); scanf("%d",&grec.r_type); fflush(stdin);
vldRT();
printf("Check-in Date\t\t: "); scanf("%d",&grec.chk_in_date); fflush(stdin);
printf("Check-out Date\t\t: "); scanf("%d",&grec.chk_out_date); fflush(stdin);
printf("Staying Period\t\t: "); scanf("%d",&grec.per); fflush(stdin);
grec.totPay = (roomFee[grec.r_type - 1] * grec.per) - advance[grec.r_type-1];
printf("Total Payment\t\t: %d\n", grec.totPay);
printf("Total Paid\t\t: "); scanf("%d",&grec.paid); fflush(stdin);
grec.balance = grec.totPay - grec.paid;
printf("Balance\t\t\t: %d \n\n",grec.balance);
printf("\t\t Confirm Booking (Y/N)?: "); gets(cfm); fflush(stdin);
if ((strcmp(cfm,"Y")==0) || (strcmp(cfm,"y")==0))
{ fwrite(&grec,sizeof(grec),1,fguest);
fclose(fguest);
chk = grec.r_type;
printf("Room successfully booked...\n");
printf("Returning to main menu...\n");
Sleep(1000);
return chk;
}
else if ((strcmp(cfm,"N")==0) || (strcmp(cfm,"n")==0))
{ bookRoom(); }
else
{ printf("\nInvalid Input. Returning to Book A Room\n");
bookRoom(); }
mainMenu();
}
void vldRT()
{
if (grec.r_type <= 0 || grec.r_type >4)
{
printf("Invalid input!! Input must be between 1 - 4\n");
printf("Please try again:\n");
printf("Room\'s Type\t\t: "); scanf("%d",&grec.r_type); fflush(stdin); }
}
void exitProgram ()
{
printf("\nExit program (Y/N)? "); gets(exitOpt); fflush(stdin);
if ((strcmp(exitOpt,"Y")==0) || (strcmp(exitOpt,"y")==0))
exit(0);
else if ((strcmp(exitOpt,"N")==0) || (strcmp(exitOpt,"n")==0))
mainMenu();
else
printf("\nInvalid Input. Please try again with valid input (Y/N). \n");
exitProgram();
}
void readData()
{
system("cls");
fguest = fopen("guest_list.dat","rb");
rewind(fguest);
while(fread(&grec,sizeof(grec),1,fguest)==1) //continue reading until there's no more struct data
{
printf("\n\nGuest\'s ID\t\t: %s", grec.id);
printf("\nGuest\'s Name\t\t: %s",grec.name);
printf("\nGuest\'s Age\t\t: %d",grec.age);
printf("\nRoom\'s Type\t\t: %d",grec.r_type);
printf("\nCheck-in Date\t\t: %d",grec.chk_in_date);
printf("\nCheck-out Date\t\t: %d",grec.chk_out_date);
printf("\nStaying Period\t\t: %d",grec.per);
printf("\nTotal Payment\t\t: %d", grec.totPay);
printf("\nTotal Paid\t\t: %d",grec.paid);
printf("\nBalance\t\t\t: %d \n\n",grec.balance);
}
fclose(fguest);
printf("Back to main (Y/N)?: "); gets(cfm); fflush(stdin);
if ((strcmp(cfm,"Y")==0) || (strcmp(cfm,"y")==0))
{ printf("Returning to main menu...\n");
Sleep(1000);
mainMenu(); }
else if ((strcmp(cfm,"N")==0) || (strcmp(cfm,"n")==0))
{ readData(); }
else
{ printf("\nInvalid Input. Returning to ReadData\n");
readData(); }
}
void main()
{
checkRoom();
mainMenu();
}
Why not make your own cls function that is portable?
void my_cls(void) {
int i = 5000;
while (i-->0)
printf("\n");
}

Resources