So I'm writing a code for a program with multiple menus that write different struct datas to a file and then with another menu that displays the data written in those files. Here's the code for the menu:
void displayall()
{
FILE *fp;
int choice=0;
struct depart loc = {0};
struct arrive loc1 = {0};
struct travel trav = {0};
fp=fopen("traveldat.dat","r");
while (1)
{
fread(&loc,sizeof(loc),1,fp);
fread(&loc1,sizeof(loc1),1,fp);
fread(&trav,sizeof(trav),1,fp);
double distance,time;
distance = sqrt(pow((loc1.x2-loc.x1),2)+pow((loc1.y2-loc.y1),2));
time = distance/trav.spd;
if (feof(fp))
{
break;
}
printf("\tYour departure location is : %s\n",loc.dep);
printf("\tWith the x-coordinate : %.2f\n",loc.x1);
printf("\tAnd the y-coordinate : %.2f\n\n",loc.y1);
printf("\tYour destination location is : %s\n",loc1.dest);
printf("\tWith the x-coordinate : %.2f\n",loc1.x2);
printf("\tAnd the y-coordinate : %.2f\n\n",loc1.y2);
printf("\tThe distance between these two locations is : %.2fkm\n\n",distance);
printf("\tYour preferred travel method is : %s\n",trav.mthd);
printf("\tWhich has a top speed of : %.2f km/h\n\n",trav.spd);
printf("\tYour expected travel time is : %.2f hours*\n\n",time);
printf("\t*Estimation,actual travel times may vary depending on certain conditions\n\n");
printf("\tThe system will now display the Main Menu\n\n");
}
fclose(fp);
}
The problem I'm facing is that if I go to the menu that writes loc1 or trav before the menu that writes loc, the display menu doesn't work, returns to the main menu, and then refuses to open whenver I try to access it. Is it because fread(&loc) is placed before the other freads? Or is there something I'm missing? Apologies in advance if this code is an eyesore or if I'm asking wrongly, I've only been learning programming for about a month.
Edit: loc1 and loc code as requested
void arrival_location_menu()
{
FILE *fp;
int choice=0;
struct arrive loc1;
fp=fopen("traveldat.dat","a");
printf("Please select your option (Destination location)\n");
printf("1.HOME\n");
printf("2.Andromeda Galaxy\n");
printf("3.The Library\n");
printf("4.Cprogramming.com\n");
printf("5.Return to main menu\n");
scanf("%d",&choice);
fflush (stdin);
switch (choice)
{
case 1: loc1.x2 = 3750;
loc1.y2 = 3450;
loc1.dest = "HOME";
system("CLS");
break;
case 2: loc1.x2 = 9870;
loc1.y2 = 5660;
loc1.dest = "Andromeda Galaxy";
system("CLS");
break;
case 3: loc1.x2 = 1367;
loc1.y2 = 3123;
loc1.dest = "The Library";
system("CLS");
break;
case 4: loc1.x2 = 2133;
loc1.y2 = 4767;
loc1.dest = "stackoverflow.com";
system("CLS");
break;
case 5: system("CLS");
break;
default: printf("Invalid option! Returning you to main menu...\n");
}
fwrite(&loc1,sizeof(loc1),1,fp);
fclose(fp);
return;
}
//DEPARTURE MENU
void departure_location_menu()
{
FILE *fp;
int choice=0;
struct depart loc;
fp=fopen("traveldat.dat","w");
printf("Please select your option (Departure location)\n");
printf("1.UTAR\n");
printf("2.PLUTO\n");
printf("3.IDK\n");
printf("4.UMM\n");
printf("5.Return to main menu\n");
scanf("%d",&choice);
fflush (stdin);
switch (choice)
{
case 1: loc.x1 = 1738;
loc.y1 = 1997;
loc.dep = "UTAR";
system("CLS");
break;
case 2: loc.x1 = 9850;
loc.y1 = 5675;
loc.dep = "PLUTO";
system("CLS");
break;
case 3: loc.x1 = 1363;
loc.y1 = 3125;
loc.dep = "IDK";
system("CLS");
break;
case 4: loc.x1 = 2130;
loc.y1 = 4785;
loc.dep = "UMM";
system("CLS");
break;
case 5:
system("CLS");
break;
default: printf("Invalied option!\n");
}
fwrite(&loc,sizeof(loc),1,fp);
fclose(fp);
return;
}
You seem to be asking if you can read data from anywhere in the file. Yes you can.
There is a function called fseek() to adjust the file pointer. The file pointer is the location next read from or written too.
There is also a function called ftell() to read the current file pointer. That's important if you're going to change the file pointer and want to restore it later.
I would also suggest you get into the habit of initializing variables ( even if it's to NULL or zero ), and of checking return values from functions. These two simple things can make debugging so much simpler.
I believe user #m-m has already explained the coding logic error.
Related
I'm currently still practicing my c programming skills but there are so many errors here that I'm confuse on what is wrong and how to fix it. It's for a database program that I was practicing on.
It keeps showing:
new2.c:86: error: request for member ‘previousreading’ in something not a structure or union
and
new2.c:94: error: ‘Break’ undeclared (first use in this function)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int custid;
char custname;
float currentreading;
float previousreading;
double charge;
int choice;
unsigned cust;
int revenue, meterdifference, BILL;
printf("----------------------------------\n");
printf("Electricity Management System\n");
printf("----------------------------------\n");
printf("\n1. Record Usage");
printf("\n2. Add Customer");
printf("\n3. Edit Customer");
printf("\n4. Delete Customer");
printf("\n5. Show Customer");
printf("\n6. Show Total monthly income");
printf("\n7. Exit");
scanf("%d",&choice);
if(choice >=1 || choice <=7)
{
switch(choice)
{
case 1: //Record Usage
printf("Enter Customer ID\n");
FILE *cfPtr;
if ((cfPtr = fopen("customer.txt", "r"))== NULL)
puts("This file could not be opened");
else
{
puts("Enter the customer ID, name.");
scanf("%d%29s", &cust.custid, cust.custname);
puts("Enter the current reading in kWh");
scanf("%d", cust.currentreading);
if(cust.currentreading < cust.previousreading)
puts("Input invalid");
else
{
if (cust.currentreading>=200)
{
cust.charge = (cust.currentreading - cust.previousreading)*21.80;
printf("\nThe charge is RM%f\n", &cust.charge);
}
else
{
if (cust.currentreading>=300)
{
cust.charge= ((cust.currentreading - cust.previousreading)*33.40)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (cust.currentreading>=600)
{
cust.charge= ((cust.currentreading - cust.previousreading)*51.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (currentreading>=900)
{
cust.charge = ((cust.currentreading - cust.previousreading)*54.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
cust.charge = ((cust.currentreading - cust.previousreading)*57.10)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
}
}
}
}
}
Break;
case2: //Add Customer
puts("This option allows user to add new customer");
printf("Enter Customer ID and name.");
scanf("%d%c", &cust.custid, cust.custname);
puts("To return to menu");
Break;
case 3: //Edit Customer
puts( "This option allows user to edit customer info");
Break;
case 4: //delete customer
puts( "This option allows user to delete customer");
Break;
case 5: //Show Customer
printf("To show customer information\n");
FILE*tPtr;
char custid[100],custname[100];
int previousreading,currentreading;
double charge;
printf("\n Show Customer\n");
if((tPtr= fopen("customer.txt","r"))==NULL){
puts("File not found");
}
else{
printf("%-15s%-25s%-20s%-15s%-15s\n","ID","Name","Previous Reading","Current Reading","Charges");
while(!feof(tPtr)){
fscanf(tPtr,"%[^;];%[^;];%d;%d;%lf",cust.custid,cust.custname,&cust.previousreading,&cust.currentreading,&cust.charge);
printf("%s\t\t%-25s%-20d%-15d%-15.2lf",cust.custid,cust.custname,cust.previousreading,cust.currentreading,cust.charge);
}
fclose(tPtr);
}
printf("\n\n");
Break;
case 6: //Show total income(monthly)
puts("To show monthyly income");
printf("total usagekWh, meterdifference");
printf("%-15s%-35.2d\n", "Total UsagekWh","meterdifference");
scanf("%-16dtotal usage(kWh)%-24d: %.2f",&meterdifference);
printf("%-13dtotal revenue%-24d: %.2f",BILL);
revenue=BILL;
printf("revenue is %.2f", BILL);
Break;
case 7: //Exit
Break;
}
}
else
printf("\nError. Number not in choices.");
return 0;
}
typedef struct{
int custid[50];
char custname[100];
int previousreading;
int currentreading;
float charges;
}cust;
Put the typedef before main. typedefs must occure before you use them just as vaiables.
Replace unsigned cust; by cust cust;. unsigned cust; is the same as unsigned int cust; and declares an unsigned integer, you want to declare a cust.
Replace float charges; by float charge; in the typedef
Replace Break; by break;. Case matters in C. Break is not Break, just as Int is not int.
Then it compiles.
Now if it it runs correctly or not is another story.
There is not a single structure in your code, not in the form of a variable declaration nor as a type definition1, and you are treating cust which is simply an unsigned int as if it was a structure, perhaps you mean
struct {
float previousreading;
float currentreading;
/* And so on */
} cust;
Also, there is no Break keyword in c, it's break, all lower case.
But,
Don't do it, create a new struct so that you can use declare variables of type struct Costumer for example. Like at the end of your code, except that the compiler needs to know about it before using it, and the cust variable should have it's type.
A char is not a string type, if you want a string you need an array of char, so char custname; is not going to work for the name string.
Use meaningful names for your variables, and the members if your structure and the type name too. Like costumer instead of cust.
Additional NOTE
See Why while (!foef(file)) is always wrong. Your code will always attempt a read with fscanf() that will fail but it proceeds to print the data, it's very likely that your last row is printed twice once you make the code compile.
Instead, check the return value of fscanf(), if you don't know what it returns and don't fully understand it you can always read fscanf(3) documentation.
1At least not before you attempt to use it.
Actually, I am here asking you to help debug. So, if you don't have any interest in helping me debugging, you can stop here any go back. However, I believe that my code only contain a small bug because I have tested most part of it.
I have do a little test myself. The code work successfully when I commented out the function pinrtOneNode(), which mean that I only input data into that binary tree. So probably the only bug is in the printOnrNode() function.
And also, the compile time error is
c:\users\...\Local\temp\ccwCUbY.oprompt
c:\users\...\.../bst.c:45printOneNode
collect2.exe:error:ld returned 1 exit status
By the way, the bst.c is where my source code in
So here is the code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct BSTnode{
struct BSTnode* leftchild;
struct BSTnode* rightchild;
char data[20];
}BSTnode;
void prompt(); //this function always prompt the user for selection everytime
//after he input data( inputData() ) or print data( printOneNode() )
void inputData();
void printOneNode();
BSTnode firstNode;
BSTnode* MyNode=&firstNode; //I don't know if it's good to initialize it this way
int main()
{
//MyNode=malloc(sizeof(BSTnode));
MyNode->leftchild=NULL;
MyNode->rightchild=NULL;
strcpy(MyNode->data,"");
while(1)
prompt(); // I use a while loop to prompt user for selection continuously
return 0;
}
/* this function is used to prompt user for selection input*/
void prompt(){
int i=0;
printf("Please select:\n");
printf("1.input a data\n2.print only one data\n3.Exit\n");
scanf("%d",&i);
switch(i){
case 1:
inputData();
break;
case 2:
printOneNode();
break;
case 3:
exit(0);
default:
printf("Please input a valid number!(1-3)");
}
}
/* this is used to input data into the binary tree*/
void inputData(){
char data[20]={0};
printf("Input your data here(character only / less than 19 characters!): ");
scanf("%s",data);
BSTnode** ptr=&MyNode;
while(1){
if(strcmp(data,(*ptr)->data)){
if((*ptr)->rightchild!=NULL){
ptr=&((*ptr)->rightchild);
continue;
}
else{
(*ptr)->rightchild=malloc(sizeof(BSTnode));
ptr=&((*ptr)->rightchild);
(*ptr)->rightchild=NULL;
(*ptr)->leftchild=NULL;
strcpy((*ptr)->data,data);
break;
}
}
else{
if((*ptr)->leftchild!=NULL){
ptr=&((*ptr)->leftchild);
continue;
}
else{
(*ptr)->leftchild=malloc(sizeof(BSTnode));
ptr=&((*ptr)->leftchild);
(*ptr)->leftchild=NULL;
(*ptr)->rightchild=NULL;
strcpy((*ptr)->data,data);
break;
}
}
}
printf(" Your data have been input successfully!\n");
return;
}
/* this is used to print data, only one at a time*/
void PrintOneNode(){
BSTnode** ptr=&MyNode;
printf("The first node is%s\n",(*ptr)->data);
while(1){
printf("Select which side of node do you want to print now(l/r)?(q for quit) ");
//I ask user whether he wanna print out the data in node in the right side
// of the current node or left side
char a;
scanf("%c",&a);
switch(a){
case 'l':
if((*ptr)->leftchild!=NULL){
ptr=&((*ptr)->leftchild);
printf("\t%s\n",(*ptr)->data);
}
else
printf("There is no more leftchild");
break;
case 'r':
if((*ptr)->rightchild!=NULL){
ptr=&((*ptr)->rightchild);
printf("\t%s\n",(*ptr)->data);
}
else
printf("There is no more rightchild!");
break;
case 'q':
return;
default:
return;
}
}
}
Please help. And any improvement to this snippet of code beside the bug will be also appreciated vert much. Thanks.
Restauraunt.c
This program allows you to create a restaurant menu, stores it in a file, and then rates each item on the menu. It uses file functions to output everything into a file that can then be viewed through almost any program. When the program gets to the line in nametofile() 'fprintf(restauraunt, "%s Restauraunt\n\n",name);' the program gives a segmentation fault. I do not know why it is doing this, I have attempted several different methods of debugging, but none have worked. If you have any suggestions, please comment them below.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
FILE *restauraunt;
char name[20];
char item[20];
char price[20];
int count=0;
void nametofile();
void rate();
void itemtofile();
void counter();
void renamefile();
int main()
{
int i,j;
int num;
printf("Restauraunt Creator\n\n");
printf("Enter the name of your restauraunt:\n");
scanf("%s",&name);
nametofile();
printf("\nEnter the number of items to be included in your menu:\n");
scanf("%d", &num);
/* Cycles through each entry to the menu */
for(i=0;i<num;i++)
{
counter();
fpurge(stdin);
printf("\nPlease enter the name of item number %d:\n",count);
scanf("%s", &item);
printf("\nPlease enter the price of item number %d:\n",count);
scanf("%s", &price);
itemtofile();
rate();
}
renamefile();
}
/*void nametofile()
{
restauraunt = fopen("restauraunt","w");
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}*/
/* The function that sends the restaurant name to the file */
void nametofile()
{
int i;
i = strlen(name);
name[i+1] = '\0';
restauraunt = fopen("restauraunt","w");
/* the line that gives a segmentation fault */
fprintf(restauraunt, "%s Restauraunt\n\n",name);
fclose(restauraunt);
}
/* rates each menu item */
void rate()
{
int rating;
srandom((unsigned)time(NULL));
restauraunt = fopen("restauraunt", "a");
rating = random() % 5 + 1;
fprintf(restauraunt,"Your food's rating was:\t%d stars!",rating);
switch(rating)
{
case 1:
{
fprintf(restauraunt," Here's why: Your food was not very good tasting and the price was ridiculously high.\n");
break;
}
case 2:
{
fprintf(restauraunt," Here's why: Your food was mildly good tasting and the price was too high.\n");
break;
}
case 3:
{
fprintf(restauraunt," Here's why: Your food was somewhat good tasting and the price was fair.\n");
break;
}
case 4:
{
fprintf(restauraunt," Here's why: Your food was quite good tasting and the price was very nice.\n");
break;
}
case 5:
{
fprintf(restauraunt," Here's why: Your food was very delicious and the price was amazingly low.\n");
break;
}
}
}
/* sends each item to the file */
void itemtofile()
{
restauraunt = fopen("restauraunt","a");
fprintf(restauraunt, "%s: $%s\nRating:",item,price);
fclose(restauraunt);
}
/* counts up one each time function is called */
void counter()
{
count += 1;
}
/* renames the file at the end */
void renamefile()
{
int x,y;
char bridge[] = { "menu" };
name[0] = tolower(name[0]);
x = strcat(name,bridge);
y = rename("restauraunt",name);
}
name is a char array. When you pass it to scanf or other functions, it decays to a pointer, so you do not need the & operator:
scanf("%19s", name);
When you read strings with scanf, it is a good idea to pass the size limit: this lets you avoid buffer overruns. Since name is declared as char[20], you pass 19, because one more char needs to be reserved for the null terminator.
I'm coding a contact manager using a doubly linked list that is manipulated by functions using pointers that reads in a contactList.txt.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>
//functions
listelement * getFirst(listelement *listpointer,string query[MAX]);
void getLast();
void getEmail();
void getCompany();
void getNumber();
void editCon();
void delCon();
void addCon();
void listAll();
void sortCon();
void Menu (int *choice);
#define MAX 20
//struct to order contactList
struct contact
{
string firstName[MAX],lastName[MAX],email[MAX],companyName[MAX];
long phoneNum[MAX];
struct listelement *link
struct contact *next;
struct contact *prev;
}list;
int main()
{
listelement listmember, *listpointer;
string query;int iChoice = 0;
listpointer = NULL;
Menu (&iChoice);
int iChoice;
fflush(stdin);
scanf_s("%d", &iChoice);
// user enters one of 9 values
// options are as follows: get first name,last name,list all contacts,search through contacts,add a new contact,edit/delete or sort contacts.
switch(iChoice)
{
case 1:
{
printf ("Enter contact first name to get details ");
scanf ("%d", &query);
listpointer = getFirst (listpointer, query);
break;
}
case 2:
{
getLast();
break;
}
case 3:
{
listAll();
break;
}
case 4:
{
getEmail();
break;
}
case 5:
{
getCompany();
break;
}
case 6:
{
getNumber();
break;
}
case 7:
{
addCon();
break;
}
case 8:
{
editCon();
break;
}
case 9:
{
delCon();
break;
}
case 10:
{
sortCon();
break;
}
case 11: // exit
{
printf("\n\nProgram exiting!...");
exit(0);//terminates program
break;
}
default:
printf ("Invalid menu choice - try again\n");
break;
}//end of switch
return(iChoice);
}//end of main
//menu function to test if invalid input was entered in a menu choice.
void Menu (int *iChoice)
{
char local;
system("cls");
printf("\n\n\t\\n\n");
printf("\n\n\t\tWelcome to my Contact Manager\n\n");
printf("\n\t\t1. First name");
printf("\n\t\t2. Last name");
printf("\n\t\t3. List all contacts");
printf("\n\t\t4. Search email");
printf("\n\t\t5. Search company name");
printf("\n\t\t6. Search number");
printf("\n\t\t7. Add contact");
printf("\n\t\t8. Edit contact");
printf("\n\t\t9. Delete contact");
printf("\n\t\t10. Sort contacts");
printf("\n\t\t11. Exit");
printf("\n\n\t\tEnter your menu choice: ");
do
{
local = getchar ();
if ( (isdigit(local) == FALSE) && (local != '\n') )
{
printf ("\nYou must enter an integer.\n");
printf ("");
}
} while (isdigit ((unsigned char) local) == FALSE);
*iChoice = (int) local - '0';
}
//function to get a contact by entering first name
listelement * getFirst (listelement *listpointer, string query)
{
//variables
char query[MAX],firstName[MAX];
FILE *fp, *ft;
int i,n,ch,l,found;
system("cls");
do
{
found=0;
l=strlen(query);
fp=fopen("ContactList.txt","r");
system("cls");
printf("\n\n..::Search result for '%s' \n===================================================\n",query);
while(fread(&list,sizeof(list),1,fp)==1)
{
for(i=0;i<=l;i++)
firstName[i]=list.firstName[i];
firstName[l]='\0';
if(stricmp(firstName,query)==0)
{
printf("\n..::First Name\t: %s\n..::Second Name\t: %ld\n..::Email\t: %s\n..::CompanyName\t: %s\n..::Number\t: %s\n",list.firstName,list.lastName,list.email,list.companyName.list.phoneNumber);
found++;
if (found%4==0)
{
printf("..::Press any key to continue...");
getch();
}
}
}
if(found==0)
printf("\n..::No match found!");
else
printf("\n..::%d match(s) found!",found);
fclose(fp);
printf("\n ..::Try again?\n\n\t[1] Yes\t\t[11] No\n\t");
scanf("%d",&ch);
}while(ch==1);
}
Anyone have any idea as to where I'm going wrong in the code?Thanks
Your errors are because:
1) you don't define listelement anywhere
2) you don't define string anywhere (and it's not a type in C)
3) You need to move the #define MAX up above before you use it.
4) You don't define FALSE anywhere (and it's not a type in C)
5) You're redefining elements too, in getFirst() you've passed in query as a "string", then you
define a new query as a char array
6) You get redefinition errors because you've got more than one define. That's somewhat #5 but there's more as well. In your main you declare iChoice here: string query;int iChoice = 0;
then you declare it again int iChoice; right after your Menu() call
7) Please don't do fflush(stdin) that's undefined behavior as per the C standard
I'm working on a small homework assignment and I'm supposed to make a food menu. Anyways, my switch isn't working. I'm trying to use a simple function that I can pass a value of "fish", "drink", or "chips" to and then it will output:
"Are you ordering FISH?" (or chips/drink)
I can't get the switch to work, it's supposed to detect what I pass into it and then output a printf based on the switch case.
CODE:
#include <stdio.h>
void menu() {
printf("\nWelcome to Sunny FISH & CHIPS!\n\n");
printf("######## Fish : Haddock(K) Large(L) | $5.00\n");
printf("# FOOD # Halibut(T) Large(L) | $4.00\n");
printf("######## Chips: Cut(C) Large(L) | $2.00\n");
printf(" Ring(R) Large(L) | $3.00\n");
printf(" | \n");
printf("########## Soft Drinks(S) Large(L) | $2.00\n");
printf("# DRINKS # Coffee(C) Large(L) | $1.75\n");
printf("########## Tea(T) Large(L) | $1.50\n");
printf("---------------------------------------------\n");
printf("Note: Medium price: 80%% of large.\n");
printf(" Small price: 60%% of large.\n");
printf("TAX is 10%%.\n");
printf("More than 5 fish, 10%% discount on drink.\n");
printf("Every 10 fish purchased, get 1 free softdrink.\n");
printf(" - size of drink is according to size of fish\n");
}
void question (char choice[5]) {
switch (choice[5])
{
case choice["fish"]:
printf("Do you order FISH?\n");
case choice["drink"]:
printf("Do you order CHIPS?\n");
case choice["chips"] :
printf("Do you order DRINKS?\n");
default :
printf("Enter a valid choice: \n");
}
}
main() {
// menu();
question("fish");
}
C does not support that kind of switch, but if it would, the syntax would be
switch(choice)
{
case "fish":
something();
break;
case "drink":
other_thing();
break;
}
A switch to me is often clearer than a (long) list of if - else ifs. Even though in this case it seems overcomplicated, I would prefer approaches like this:
#include <stdio.h>
#include <string.h>
enum menu_items { FISH, DRINK, CHIPS, UNKNOWN };
struct items
{
char *name;
enum menu_items id;
} items_list[] = {
{ "fish", FISH },
{ "drink", DRINK },
{ "chips", CHIPS }
};
int main(void)
{
int i;
enum menu_items mid;
struct items *choice = NULL;
// ...
for(i = 0, choice = NULL; i < sizeof items_list/sizeof (struct items); i++)
{
if (strcmp(answer, items_list[i].name) == 0)
{
choice = items_list + i;
break;
}
}
mid = choice ? choice->id : UNKNOWN;
// the following would be enough to obtain the output of your example;
// I've not embodied the code into a func, but it's easy to do if you need
if ( mid != UNKNOWN )
{
// the function a_func transforms the string of the food
// e.g. to uppercase, or it could map it to whatever according to some
// other data... or expand the struct to hold what you want to output
// with "fish", "drink", "chips", e.g. choice->screen_name
printf("Do you order %s?\n", a_func(choice->name));
}
else
{
printf("Enter a valid choice:\n");
}
// ---------
// or if you prefer the switch you have something like:
switch(mid)
{
case FISH:
printf("fish\n");
break;
case DRINK:
printf("drink\n");
break;
case CHIPS:
printf("chips\n");
break;
default:
printf("unknown choice\n");
break;
}
return 0;
}
If you choose properly the approach, your code could be always the same and only your data grow.
In addition to the other answers, if you find that your list of choices all begin with a unique letter (or have a unique letter in another position) then you can switch on that letter:
switch (choice[0]) {
case 'f':
// they chose fish
break;
case 'c':
// they chose chips
break;
case 'd':
// they chose drink
}
This will be faster than using strcmp (although it doesn't matter for your case) and is less maintainable. However, it's good to know all the options and realise how you can use some of these things.
You cannot use switch statement with strings.
You may consider using strcmp to compare strings.
if (strcmp(choice,"fish")==0) {
//fish
}
else if (strcmp(choice,"drink")==0) {
//drink
}
.
.
.
C doesn't support switches on strings...you should use strcmp()
switch doesn't work like that in C. You'll need to make an if statement construct and use strcmp() to compare the strings.