How to properly make a dynamic command-line menu? - c

More precisely.. How do I add a "go back to main menu" function as all other softwares and games have?
void showMenu()
{
puts( "1. Create school\n"
"2. Add room\n"
"3.Add student to room\n"
"4.Find student\n"
"5. Show students in room\n"
"\n" "6. Exit");
}
int main()
{
clrscr();
studentList *foundStudent;
int input;
showMenu();
while( scanf("%d", &input) )
{
if(input == 6)
{
periods("Exiting");
break;
}
if(input == 1)
{
school *school;
school = createSchool();
}
if(input == 2)
{
int room, roomNr;
printf("Enter room Nr. and Class:");
scanf("%d %d", &room, &roomNr);
}
}
return 0;
}
Anything I attempted didn't work and just created more redundancy, never expected how goto can be so confusing.
Although switch makes more sense, I don't believe it fixes my problem.

Here is a working example.
The solution for this problem was simply making a "menu within a menu" kind of, I did use some gotos but as far as commandline menu goes, this may be all.
#define clrscr() printf("\e[1;1H\e[2J")
void periods(char* message)
{
const int trigger = 500; // ms
const int numDots = 3;
while (1)
{
// Return and clear with spaces, then return and print prompt.
printf("\r%*s\r%s", sizeof(message) - 1 + numDots, "", message);
fflush(stdout);
// Print numDots number of dots, one every trigger milliseconds.
for (int i = 0; i < numDots; i++)
{
usleep(trigger * 1000);
fputc('.', stdout);
fflush(stdout);
}
break;
}
}
void showmenu()
{
clrscr();
puts("1. New Game\n"
"2. Load Game\n"
"3. Credits\n\n"
"4. Exit\n" );
}
int checkString(char *str)
{
int status = 0;
int ln = strlen(str);
for(int i = 0; i < ln; i++)
{
if(isdigit( str[i] ) )
status = 1;
break;
}
return status;
}
int main(){
char choice;
clrscr();
int status, isNum = -101;
char *name, *buffer, YN;
showmenu();
while(1)
{
scanf(" %c", &choice);
if( isdigit(choice) )
{
break;
}
else
{
fflush(stdin);
printf("Please only enter numbers!\n");
sleep(1);
showmenu();
}
}
do{
switch(choice)
{
case '1':
{
createG:;
clrscr();
printf("Enter name or press 0 to return\n");
scanf("%s", buffer);
status = checkString(buffer);
if(status == 1)
{
clrscr();
break;
}
clrscr();
name = (char*)malloc(strlen(buffer)+1);
strcpy(name, buffer);
printf("New game created, welocome %s!\n");
for(int i = 0; i < 5; i++)
{
sleep(1);
printf("%d\n", i);
}
break;
}
case '2':
{
int what;
caseL:;
clrscr();
printf("No saves!\n Create new game? [Y/N] \n to return press 0\n");
scanf(" %c", &YN);
if(YN == 'N') what = 0;
if(YN == '0') what = -1;
if(YN == 'Y') what = 1;
switch(YN)
{
case 'N':
{
goto caseL;
}
case '0':
{
break;
}
case 'Y':
{
choice = 1;
goto createG;
break;
}
}
break;
}
case '3':
{
periods("Hello World");
break;
}
case '4':
{
clrscr();
periods("Goodbye");
clrscr();
exit(1);
}
default:
{
printf("Wrong input\n Try again");
sleep(1);
break;
}
}
}while(choice != -2);
return 0;
}
This may still need a lot of error checking and handling for "unexpected" inputs but it answers the problem.

Related

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

C program query

So this is my program and the number, name, address doesn't print again after single execution and also If the seat is already taken there should be "seat is taken already, please try again" which I'm confused about
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct passenger
{
char name[20];
char address[30];
int age;
};
struct passenger data;
struct rwcl
{
int row;
char col;
};
int clmn, i, j;
int arr[5][5];
struct rwcl number;
for(i=0;i<5;i++)
{
printf("Enter Your Name: ");
scanf("\n");
gets(data.name);
printf("Enter Your Address: ");
scanf("\n");
gets(data.address);
printf("Enter Your Age: ");
scanf("%d", &data.age);
printf("\nAll aboard! You may now choose your desired seat/s.");
while(i<5){
for(j=0;j<5;j++){
if (j == 0) {
arr[i][j] = i+1;
}
if(j == 1){
arr[i][j] = 'A';
}
if(j== 2){
arr[i][j] = 'B';
}
if(j == 3){
arr[i][j] = 'C';
}
if(j== 4){
arr[i][j] = 'D';
}
}
i++;
}
printrwcl:
printf("\n\n");
for(i=0;i<5;i++){
for(j=0;j<5;++j){
if(j == 0 ){
printf("%-5d", arr[i][j]);
}
else {
printf("%-5c", arr[i][j]);
}
}
if(j==5) {
printf("\n");
}
}
printf("\n");
rowselect:
printf("Choose a row between 1,2,3,4,5 or 6 for cancellation: ");
scanf("%d", &number.row);
if(number.row < 0 || number.row > 6) {
printf("\nPlease, re-enter. Thank you.\n");
goto rowselect;
}
if(number.row == 6) {
printf("Recorded, thank you.");
exit(0);
}
columnselect:
printf("Choose a letter between A,B,C,D: ");
scanf("\n");
scanf("%c", &number.col);
switch(number.col)
{
case 'A':
clmn = 1;
break;
case 'B':
clmn = 2;
break;
case 'C':
clmn = 3;
break;
case 'D':
clmn = 4;
break;
}
if(arr[number.row-1][clmn] == 'X')
{
printf("Seat is taken. Please choose a different one.");
}
else
{
printf("Seat %d%c has been reserved.", number.row, number.col);
arr[number.row-1][clmn] = 'X';
}
goto printrwcl;
}
}

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

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

assigning variable score1 & score2 to another variable respectively before resetting them to 0

so i'm trying to do a badminton scorekeeper.
and having a problem.
#include<stdio.h>
int scorekeeper();
char home[21]="HOME",away[21]="AWAY",choice3;
void scoreboard();
int score1=0,score2=0,set1=0,set2=0,m=1;
int main()
{
int choice1,choice2,setnum;
char buf[50];
printf("\t\t\t/////////////////////////////////\n\t\t\t/ BADMINTON SCOREKEEPER\t/\n\t\t\t/////////////////////////////////\n\n");
do {
printf("Do you want to enter players' or teams' names?\n1-Yes\n2-No\n:>>");
scanf("%d",&choice1);
switch(choice1) {
case 1: {
do {
printf("\nPlease enter HOME player's or team's name(max 20 characters including space)\n:>>");
fgets(buf, sizeof buf, stdin);
scanf(" %20[^\n]",&home);
printf("\nPlease enter AWAY player's or team's name(max 20 characters including space)\n:>>");
fgets(buf, sizeof buf, stdin);
scanf(" %20[^\n]",&away);
do {
printf("\n\n%.20s VS %.20s\n\n1.Confirm\n2.Edit\n:>>",home,away);
fgets(buf, sizeof buf, stdin);
scanf(" %d",&choice2);
if(choice2!=1&&choice2!=2)
printf("\n***ERROR. INVALID INPUT***\n\n");
} while(choice2!=1&&choice2!=2);
} while(choice2==2);
break;
}
case 2: {
printf("\nSet up to default:\n%s VS %s\n\n",home,away);
break;
}
default: {
printf("\n***ERROR. INVALID SELECTION***\n\n");
break;
}
}
} while(choice1!=1&&choice1!=2);
do {
printf("\n\nHow many sets you want to play?\n(enter 1,3 or 5)\n:>>");
scanf("%d",&setnum);
if(setnum!=1&&setnum!=3&&setnum!=5)
printf("\n\n***INVALID INPUT. PLEASE RE-ENTER***\n\n");
} while(setnum!=1&&setnum!=3&&setnum!=5);
printf("\n\nSTART THE MATCH\n===============\n\n");
while(setnum>0) {
--setnum;
score1=0;
score2=0;
while(score1<21&&score2<21) {
scoreboard();
scorekeeper();
}
if (score1==21&&score2==20||score1==20&&score2==21)
while(score1!=score2+2&&score2!=score1+2) {
scoreboard();
scorekeeper();
if(score1==29&&score2==29)
break;
}
if(score1==29&&score2==29) {
scoreboard();
scorekeeper();
}
if(score1>score2)
++set1;
else
++set2;
scoreboard();
++m;
printf("\n\nEND OF SET\n\n");
if(set1==set2+2||set2==set1+2)
break;
}
return 0;
}
void scoreboard()
{
int a,b,c,d,e,f,g,h,i,j;//here comes the problem
if(m==1) {
a=score1;
b=score2;
printf("\n\n\t\t S\n----------------------------\n|%-20s|%2d|%2d|\n----------------------------\n|%-20s|%2d|%2d|\n----------------------------\n\n",home,set1,a,away,set2,b);
} else if(m==2) {
c=score1;
d=score2;
printf("\n\n\t\t S\n-------------------------------\n|%-20s|%2d|%2d|%2d|\n-------------------------------\n|%-20s|%2d|%2d|%2d|\n-------------------------------\n\n",home,set1,a,c,away,set2,b,d);
} else if(m==3) {
e=score1;
f=score2;
printf("\n\n\t\t S\n-------------------------------\n|%-20s|%2d|%2d|%2d|\n-------------------------------\n|%-20s|%2d|%2d|%2d|\n-------------------------------\n\n",home,set1,a,c,e,away,set2,b,d,f);
}
return;
}
int scorekeeper()
{
printf("\n\nHOT KEYS LIST:\nA-%s SCORES\nL-%s SCORES\n\n",home,away);
scanf(" %c",&choice3);
switch(choice3) {
case 'A':
case 'a': {
++score1;
break;
}
case 'L':
case 'l': {
++score2;
break;
}
default: {
printf("\n\n***ERROR. INVALID INPUT***\n\n");
break;
}
}
return score1,score2;
}
what i am trying to do is when in set 1, the program will assign value of score1 & score2 to a&b respectively. then going to next set, it'll overwrite value of score1& score2 back to 0 before assigning them to c&d. how am i suppose to do this without affecting value stored in a&b?

Resources