How do i send a string to another function using c? - 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

Related

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");
}
}

Access a field of a structure that is within the union of it

This program must handle data that represents the products used in the factory. It is desired that the product represents the following characteristics: Code, description unit of measure, and price; In turn, if the product is imported you want to know the origin of the product, while if it is purchased in the square you want to know the name of the provider's phone number.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <stdbool.h>
#include <winsock.h>
void clrscr();
typedef enum {IMPORTED, LOCAL} type;
typedef struct
{
int code;
char description [20];
char MeasureUnit [5];
float price;
type discriminant;
union
{
char origin [20];
char destination [20];
int telephone;
} impoExpo;
} Product;
//this procedure fails
void loadProduct (Product *p)
{
printf("\nEnter the code:");
scanf("%d",&Product.code); //<-error: expected expression before 'Product'
fflush(stdin);
printf("\nEnter the description:");
scanf("%s",Product.description);
printf("Indicate the unit of measure:");
scanf("%s",Product.MeasureUnit);
printf("Enter the price:");
scanf("%f",&Product.price);
int i;
printf("\nInsert 1 if imported");
scanf("%d", &i);
if(i == 1)
{
p->discriminant = IMPORTED;
}
else
{
p->discriminant = LOCAL;
}
if(p->discriminant == IMPORTED)
{
printf("\nEnter source: ");
gets(p->impoExpo.origin);
}
else
{
printf("\nEnter the phone");
scanf("%d", &p->impoExpo.telephone);
}
}
//it is also
void showProduct (Product p)
{
printf("\nCode: %d", p.code); //<----- error: request for member 'code' in something not a structure or union
printf("\nDescription");
printf("%s", p.description);
printf("\nMeasurement unit:");
printf("%s", p.MeasureUnit);
printf("\nPrice:% .2f", p.price);
printf("\nType:");
if (p.discriminant == IMPORTED)
{
printf("Imported:");
printf("\nOrigin: %s", p.impoExpo.origin);
printf("%s", p.impoExpo.origin);
}
else
{
printf("Local:");
printf("\nTelephone: %d", p.impoExpo.telephone);
}
}
//this one also
bool areequal (Product p1, Product p2)
{
bool equal = false;
if ((p1.code == p2.code) && (p1.description == p2.description))
{
if ((p1.MeasureUnit == p2.MeasureUnit) && (p1.price == p2.price))
{
if (p1.discriminant == p2.discriminant)
{
if (p1.discriminant == IMPORTED)
{
if (p1.impoExpo.origin == p2.impoExpo.origin)
{
equal = true;
}
}
else
{
if (p1.impoExpo.telephone == p2.impoExpo.telephone)
{
equal = true;
}
}
}
}
}
return equal;
}
//this funciĆ³n ok
void copy (Product * const destination, const Product * const origin)
{
destination->code = origin->code;
(*destination->description) = (*origin->description);
(*destination->MeasureUnit) = (*origin->MeasureUnit);
destination->price = origin->price;
destination->discriminant = origin->discriminant;
if(destination->discriminant == IMPORTED)
(*destination->impoExpo.origin) = (*origin->impoExpo.origin);
else
destination->impoExpo.telephone = origin->impoExpo.telephone;
}
//and the latter also
int main ()
{
int option;
do
{
clrscr();
printf("Welcome to the program\n");
printf("Enter an option\n");
printf("1. Load a product\n");
printf("2. Show product\n");
printf("3. Check if two products are the same\n");
printf("0. Exit");
printf("Enter the option, and press ENTER");
scanf("%d",&option);
switch (option)
{
case 1:
loadProduct(&p);
getch();
break;
case 2:
showProduct(p);
getch();
break;
case 3:
printf("Enter the name of the product 1");
scanf("%d",&p1);
printf("Enter the name of the product 2");
scanf("%d",&p2);
printf("% d",areequal (p1, p2));
getch();
break;
}
} while (option != 0);
getch();
return 0;
}
void clrscr()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
SetConsoleCursorPosition(hStdOut, coord);
}
Product is a pointer, so you have to access its members using the arrow notation:
scanf("%d",&p->code); //<-error: expected expression before 'Product'

Find specifi ID info using structure

I could not find specific student_Id info from a rage of given info.suppose, I am taking input from 1 to 100 all student info. now I want to find only 50th number student_ID all info.i could not do it. how it possible.here show my code. what's wrong with my code and how fixed it.thanks
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
struct student
{
char student_id[100];
char name[10];
int m[50],credit[100],sub[100];
int total,sumCredit;
float GP[100];
char result[5];
char grade[100][10];
double sumCreditxGP;
}*p,*s;
float gradesToGP(float marks);
char *markToG(float gp);
void lines(void);
void main()
{
int i,j,l,n,sub,k;
// clrscr();
printf("Enter the no. of students : ");
scanf("%d",&n);
p=(struct student*) malloc(n*sizeof(struct student));
//printf("%d",p);
//exit(0);
s=p;
for(i=0; i<n; i++)
{
printf("Enter a student id : ");
scanf("%s",&p->student_id);
printf("Enter a name : ");
scanf("%s",&p->name);
printf("Enter the no. of subject : ");
scanf("%d",&p->sub[i]);
p-> total=0;
p-> sumCredit=0;
p-> sumCreditxGP=0;
l=0;
for(j=0; j<p->sub[i]; j++)
{
one:
printf("Enter Marks of %d Subject\t%d : ",j+1,p->sub[i]);
scanf("%d",&p->m[j]);
printf("Enter Credit Hour: ");
scanf("%d",&p->credit[j]);
p->GP[j] = gradesToGP((float)p->m[j]);
strcpy(p->grade[j],markToG(p->m[j]));
if((p->m[j])>100)
{
printf("---------------Wrong Value Entered-----------\n");
goto one;
}
p->total+=p->m[j];
p->sumCredit+=p->credit[j];
p->sumCreditxGP+=p->credit[j]*p->GP[j];
if(p->m[j]<40)
l=1;
}
if(l==0)
strcpy(p->result,"PASS");
else
strcpy(p->result,"FAIL");
p++;
}
char search_id[10];
printf("Enter id to find specific student ");
scanf("%s",search_id);
//PROBLEM START HERE
for(i=0; i<n; i++)
{
if(p->student_id==search_id){
printf("found");
printf("%s",s->student_id);
}else{
printf("Not found");
}
s++;
}
getch();
}
float gradesToGP(float marks)
{
if (marks>=80 && marks<=100)
{
return(float)4.00;
}
else if (marks>=75 && marks<=79)
{
return(float)3.67;
}
else if (marks>=70 && marks<=74)
{
return(float)3.33;
}
else if (marks>=65 && marks<=69)
{
return(float)3.00;
}
else if (marks>=60 && marks<=64)
{
return(float)2.67;
}
else if (marks>=55 && marks<=59)
{
return(float)2.33;
}
else
{
return(float)5.00;
}
}
char *markToG(float marks)
{
if (marks>=80 && marks<=100)
{
return "A";
}
else if (marks>=75 && marks<=79)
{
return "A-";
}
else if (marks>=70 && marks<=74)
{
return "B+";
}
else if (marks>=65 && marks<=69)
{
return "B";
}
else if (marks>=60 && marks<=64)
{
return "B-";
}
else if (marks>=55 && marks<=59)
{
return "C+";
}
else
{
return "null";
}
}
void lines(void)
{ printf("**********************************************************************");
}
Please tell me how can I fixed it.thanks in advanced.
if(p->student_id==search_id){
printf("found");
Now, that's not how you compare strings in C. Use the strcmp() function for string comparison. You may read about strcmp() here.
The issue is your equality check: if(p->student_id==search_id)
Because both student_id and search_id are char arrays, the types will decay to pointers (char *) and this will never work as you expect. Instead, you need to use strcmp (or better, strncmp).
if(strncmp(p->student_id, search_id, 10) == 0) { /* equality */ }

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");
}

Linked List inside Linked List

I'm trying to use nested linked lists, but I can't seem to access the inner part of the linked list.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int option;
struct nodeAddr
{
int idAddress;
char address[50];
struct nodeAddr *nextAddr;
}*headAddr,*tailAddr,*newAddr,*helpAddr;
struct nodePrs
{
int idPersonel;
char personel[50];
struct nodeAddr *headAddr,*tailAddr,*newAddr,*helpAddr ;
struct nodePrs *nextPrs;
}*headPrs,*tailPrs,*newPrs,*helpPrs;
void signUp()
{
clrscr();
printf("\t============================\n");
printf("\t= Sign Up New Personel =\n");
printf("\t============================\n\n");
newPrs=(nodePrs*)malloc(sizeof(struct nodePrs));
newPrs->newAddr=(nodeAddr*)malloc(sizeof(struct nodeAddr));
printf("Name : ");
scanf("%s",newPrs->personel);
printf("Address : ");
scanf("%s",newPrs->newAddr->address);
newPrs->nextPrs = NULL;
newPrs->newAddr->nextAddr = NULL;
if (headPrs==NULL)
{
headPrs = newPrs;
headPrs->headAddr = newPrs->newAddr;
}
else
{
tailPrs->nextPrs=newPrs;
}
tailPrs=newPrs;
tailPrs->tailAddr=newPrs->newAddr;
}
void printList()
{
helpPrs = headPrs;
nodePrs *temp = headPrs;
if (headPrs==NULL)
printf("Empty");
else
{
printf("the Outputs:\n");
while(helpPrs!=NULL)
{
printf("%s", helpPrs->personel);
printf(" ");
helpPrs->helpAddr = temp->headAddr;
while (helpPrs->helpAddr !=NULL)
{
printf("%s", helpPrs->helpAddr->address);
printf(" ");
helpPrs->helpAddr = helpPrs->helpAddr->nextAddr;
}
helpPrs = helpPrs->nextPrs;
temp=temp->nextPrs;
printf("\n");
}
}
}
void main()
{
do
{
clrscr();
printf("\t==================================\n");
printf("\t= Linked list inside Linked List =\n");
printf("\t==================================\n\n");
printf("1. Sign\n");
printf("2. Show\n");
printf("3. Exit\n\n");
printf("Input (1-3) : ");
scanf("%d",&option);
if (option == 1)
{
signUp();
}
else if (option == 2)
{
printList();
getch();
}
else if(option>2 || option <1)
{
printf("Wrong choice!!!");
}
} while(option!=5);
getch();
}
The problem is in the printList() function. It only prints the first list. After that, it can't show its address of the second entry and so on.

Resources