I'm trying to use a menu in this C program but I keep getting an error:
3.c: In function 'main':
q3.c:99:3: error: expected declaration or statement at end of input
}
^
q3.c:99:3: error: expected declaration or statement at end of input
The code
#include <stdio.h>
int userInput();
void printList();
void editStud();
void delStud();
void addStud();
struct studentRec
{
char name[25];
char init[25];
char pNu[25];
int studNum;
float bBalance;
};
int main()
{
//no constants
struct studentRec students;
FILE *fp,*fw,*ft;
int sel = 1; //select number for menu
while(userInput)
{
sel = userInput();
switch(sel)
case1:
printList();
break;
case2:
editStud();
break;
case3:
delStud();
break;
case4:
addStud();
break;
case0:
break;
{
}
int userInput()
{
int choice;
printf("===========================\n");
printf("(1). View all students\n");
printf("(2). Edit student details\n");
printf("(3). Delete student\n");
printf("(4). Add new student\n");
printf("(0). Exit\n");
printf("===========================\n\n");
printf("Enter your choice Please\n");
scanf("%d", &choice);
return choice;
}
void printList()
{
printf("*Prints list*\n");
}
void editStud()
{
printf("*edits entry*\n");
}
void delStud()
{
printf("*deletes entry*\n");
}
void addStud()
{
printf("*adds entry\n");
}
Thanx for all the responses, what I meant to do:
.
.
.
int userInput();
void printList();
void editStud();
void delStud();
void addStud();
.
.
.
int sel = 1; //select number for menu
while(sel)
{
sel = userInput();
switch(sel)
{
case 1:
printList();
break;
case 2:
editStud();
break;
case 3:
delStud();
break;
case 4:
addStud();
break;
case 0:
break;
default:
printf("That is not a valid selection!\n");
}
}
.
.
.
errors were:
1. incorrect while braces
2. used while(userInput) instead of while(sel)
3. no space between case and number
what is case1? My only guess is that you mean to compare against 1, in which case you'd have to leave a space between the keyword case and the actual case value. Here's how your switch case should look like:
switch (sel) {
case 1:
printList();
break;
case 2:
editStud();
break;
case 3:
delStud();
break;
case 4:
addStud();
break;
case 0:
break;
}
1) Your braces inside while is meaningless (the one before ending while brace)
2) your switch statement does not have enclosing braces.
3) Also, there should be a space between case keyword and the switch input
Correct way is
while(userInput){
sel = userInput();
switch(sel){
case 1:
printList();
break;
case 2:
editStud();
break;
case 3:
delStud();
break;
case 4:
addStud();
break;
case 0:
break;
}
}
Change:
case1:
to
case 1:
etc.
also a switch statement requires a starting { and an ending }.
After a Switch statement, you want a open brace.
switch(sel) {
case is a keyword, which means that you probably want whitespace around it.
case 1:
case 2:
...
You have an open brace where you probably want a close brace. The number of open braces should match the number of close braces.
}
Your while statement is quite odd. You probably want to do something else with that...
So, main() should actually look like:
int main()
{
struct studentRec students;
FILE *fp,*fw,*ft;
int sel = 1; //select number for menu
while(true)
{
sel = userInput();
switch(sel) {
case 1:
printList();
break;
case 2:
editStud();
break;
case 3:
delStud();
break;
case 4:
addStud();
break;
case 0:
break;
}
}
}
Related
Okay so I'm writing a code for something and I've encountered a problem whilst testing switch function. It does all the cases incrementing from one's selection (my explanation). Could someone help me explain why is this so?
#include <stdio.h>
#include <stdlib.h>
#include "ratedzfunctions.h"
int main()
{
int selection, loop=1;
FILE* fajl;
//Opening the participants file
fajl=fopen("participants.txt","r+");
if (fajl==NULL)
{
printf("The file cannot be opened.\n");
}
//MENU
do
{
printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>");
scanf("%d", &selection);
switch (selection)
{
case 1:
ratedz(fajl);
case 2:
stats(fajl);
case 3:
loop=0;
}
}
while (loop==1);
fclose(fajl);
return 0;
}
//THIS IS FROM RATEDZFUNCTIONS.H
void ratedz(FILE *fajl)
{
printf("\nTEST RATEDZ");
}
void stats(FILE *fajl)
{
//Printing all participants
char *buffer=(char*) malloc(50);
while(fscanf(fajl,"%s %s %s", buffer)!=EOF)
{
printf("\n%s %s %s", buffer);
}
free(buffer);
}
You forgot to add a break; statement after each case.
case 2:
stats(fajl);
break; /* <---- */
You should put a break; after each case.
The switch/case rule is easy, after a mached case, all following cases will be executed until a break; or end of switch:
switch (selection)
{
case 1:
...
break;
case 2:
...
break;
case 3:
...
break; // Last break is not necessary
// but it's good practice to put it.
}
There are good situations which removing break; is reasonable:
switch(letter)
{
case 'i':
case 'a':
case 'o':
case 'u':
case 'e':
printf ("Vowel!");
break;
default :
printf ("Consonant!");
break;
}
If you do not add a break at the end of each case it will just fall through to the next case:
switch (selection)
{
case 1:
ratedz(fajl);
break ;
case 2:
stats(fajl);
break ;
/* ... */
}
A case in a switch statement is treated like a label (see C.11 § 6.8.1). . There is actually no requirement to have any cases at all (See C.11 § 6.8.4).
switch (0) { /* do nothing */ }
The above code will compile just fine.
Since a case is like a label, there are no implicit semantics attached to it that would cause it to automatically jump outside the switch. Just as break is used to leave a loop block early, break is also used to leave a switch block early.
Syntax for switch staement in C
switch(expression)
{
case (constant-expression) : staements
....
case (constant-expression) : staements
default : statements
}
To work with a particular case your last statement in that group of statement must be break.
Without the break statement , when the last statement in the case has been executed, control "falls through" to the first statement in the following case; the case label (const-expression) for the next case is ignored. Without break (or some jump statement), control will flow from one case into the next.
Some corrections,
#include <stdio.h>
#include <stdlib.h>
#include "ratedzfunctions.h"
int main()
{
int selection, loop=1;
FILE* fajl;
//Opening the participants file
fajl=fopen("participants.txt","r+");
if (fajl==NULL)
{
printf("The file cannot be opened.\n");
exit(1); //handle error when file cannot be opened...
}
//MENU
do
{
printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>");
scanf("%d", &selection);
switch (selection)
{
case 1:
ratedz(fajl);
break;
case 2:
stats(fajl);
break;
case 3:
loop=0;
break;
default:
break;
}
}
while (loop==1)
{
//do stuff here
}
fclose(fajl);
return 0;
}
Here is my code : a switch statement
#include <stdio.h>
#include <conio.h>
void main()
{
float a,b,ans;
int code;
printf("enter two no\n");
scanf("%f%f",&a,&b);
printf("select an expression \n1-addition \n2-substraction \n3multiplication \n4-divide\n ");
scanf("%d",&code);
switch(code)
{
case 1:ans=a+b;
printf("%f\n",ans);
break;
case 2:ans=a-b;
printf("%f\n",ans);
break;
case 3:ans=a*b;
printf("%f\n",ans);
break;
case 4:ans=a/b;
printf("%f\n",ans);
break;
}
getch();
}
Now after doing one calculation lets say 1-addition I want to again do another calculation without exiting the black screen.
how it can be done?
if I have to put a loop then where and how, please explain.
Following code will do the all operation for the given input till you choose to exit. But if you want to do the operation for different values the you can include the 7th and 8th line inside the while loop.
#include <stdio.h>
#include <conio.h>
void main()
{
float a,b,ans;
int code;
printf("enter two no\n");
scanf("%f%f",&a,&b);
while(1)
{
printf("select an expression \n1-addition \n2-substraction \n3multiplication \n4-divide \n5-Exit\n");
scanf("%d",&code);
switch(code)
{
case 1:
ans=a+b;
printf("%f\n",ans);
break;
case 2:
ans=a-b;
printf("%f\n",ans);
break;
case 3:
ans=a*b;
printf("%f\n",ans);
break;
case 4:
ans=a/b;
printf("%f\n",ans);
break;
}
if(code == 5)
return;
}
getch();
}
while(1){
printf("Press 0 to exit");
switch(code)
{
case 1:ans=a+b;
printf("%f\n",ans);
break;
case 2:ans=a-b;
printf("%f\n",ans);
break;
case 3:ans=a*b;
printf("%f\n",ans);
break;
case 4:ans=a/b;
printf("%f\n",ans);
case 0:
exit();
break;
}
}
I got a problem with this when I choose it always exit my program and when I enter a wrong choice got to exit to?
What is wrong to my loop? Please help with me as for looping I am having serious problems. This is a fundamental concept but my brain is old and stuck.
Could you help me understand looping to return to the start of the program.
Thanks
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
char choice,name[40],date[40],note[500],;
int password;
int ch;
printf("\n\n\t\tMAIN MENU:");
printf("\n\n\tADD RECORD\t[1]");
printf("\n\tVIEW RECORD\t[2]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tDELETE RECORD\t[4]");
printf("\n\tEDIT PASSWORD\t[5]");
printf("\n\tEXIT\t\t[6]");
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
do{
{
opcase 1:
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tEDIT RECORD\t[3]");
break;
case 2:
printf("\n\tEDIT RECORD\t[3]");
break;
case 3:
printf("\n\tEDIT RECORD\t[3]");
break;
case 4:
printf("\n\tEDIT RECORD\t[3]");
break;
case 5:
printf("\n\tEDIT RECORD\t[3]");
break;
case 6:
printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE BY:\n\n\tBIJAY PURI\n\n\tBHANU POUDEL\n\n\tNRIPASH AYER...");
getch();
exit(0);
default:
printf("\nYOU ENTERED WRONG CHOICE..");
printf("\nPRESS ANY KEY TO TRY AGAIN");
getch();
break;
}
}
while(ch!=6);
return 0;
getch();
}
Put your do..while loop outside switch case.
Hint: You might have solved it yourself if you had indented your code properly.
Correct Syntax:
do
{
//do something
switch (variable)
{
//case
break;
}
}
while (condition)
EDIT:
Below is a working version [on linux] of your code. Please notice the changes carefully.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char choice,name[40],date[40],note[500];
int password;
int ch;
printf("\n\n\t\tMAIN MENU:");
printf("\n\n\tADD RECORD\t[1]");
printf("\n\tVIEW RECORD\t[2]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tDELETE RECORD\t[4]");
printf("\n\tEDIT PASSWORD\t[5]");
printf("\n\tEXIT\t\t[6]");
do{
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEDIT RECORD\t[3]\n");
printf("\n\tEDIT RECORD\t[3]\n");
printf("\n\tEDIT RECORD\t[3]\n");
printf("\n\tEDIT RECORD\t[3]\n");
break;
case 2:
printf("\n\tEDIT RECORD\t[3]");
break;
case 3:
printf("\n\tEDIT RECORD\t[3]");
break;
case 4:
printf("\n\tEDIT RECORD\t[3]");
break;
case 5:
printf("\n\tEDIT RECORD\t[3]");
break;
case 6:
printf("\n\n\t\tTHANK YOU FOR USING THE SOFTWARE BY:\n\n\tBIJAY PURI\n\n\tBHANU POUDEL\n\n\tNRIPASH AYER...");
exit(0);
default:
printf("\nYOU ENTERED WRONG CHOICE..");
printf("\nPRESS ANY KEY TO TRY AGAIN");
break;
}
} while(1); //no need to check conditions here, redundant.
return 0 ;
}
Your do while and switch curly braces are tangled.
This is what you have:
switch (variable)
{
do {
}
}
while (condition);
and this is the correct way:
do {
switch (variable)
{
}
}
while (condition);
Also you must put getch before the return instruction otherwise it doesn't make sense.
you have to do something like
do{
//read your option
scanf("%d",&ch);
//do something with that option you get
switch(option){
}
}while(option != 6);
And this because always have to think , what actions you need to repeat
Okay so I'm writing a code for something and I've encountered a problem whilst testing switch function. It does all the cases incrementing from one's selection (my explanation). Could someone help me explain why is this so?
#include <stdio.h>
#include <stdlib.h>
#include "ratedzfunctions.h"
int main()
{
int selection, loop=1;
FILE* fajl;
//Opening the participants file
fajl=fopen("participants.txt","r+");
if (fajl==NULL)
{
printf("The file cannot be opened.\n");
}
//MENU
do
{
printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>");
scanf("%d", &selection);
switch (selection)
{
case 1:
ratedz(fajl);
case 2:
stats(fajl);
case 3:
loop=0;
}
}
while (loop==1);
fclose(fajl);
return 0;
}
//THIS IS FROM RATEDZFUNCTIONS.H
void ratedz(FILE *fajl)
{
printf("\nTEST RATEDZ");
}
void stats(FILE *fajl)
{
//Printing all participants
char *buffer=(char*) malloc(50);
while(fscanf(fajl,"%s %s %s", buffer)!=EOF)
{
printf("\n%s %s %s", buffer);
}
free(buffer);
}
You forgot to add a break; statement after each case.
case 2:
stats(fajl);
break; /* <---- */
You should put a break; after each case.
The switch/case rule is easy, after a mached case, all following cases will be executed until a break; or end of switch:
switch (selection)
{
case 1:
...
break;
case 2:
...
break;
case 3:
...
break; // Last break is not necessary
// but it's good practice to put it.
}
There are good situations which removing break; is reasonable:
switch(letter)
{
case 'i':
case 'a':
case 'o':
case 'u':
case 'e':
printf ("Vowel!");
break;
default :
printf ("Consonant!");
break;
}
If you do not add a break at the end of each case it will just fall through to the next case:
switch (selection)
{
case 1:
ratedz(fajl);
break ;
case 2:
stats(fajl);
break ;
/* ... */
}
A case in a switch statement is treated like a label (see C.11 § 6.8.1). . There is actually no requirement to have any cases at all (See C.11 § 6.8.4).
switch (0) { /* do nothing */ }
The above code will compile just fine.
Since a case is like a label, there are no implicit semantics attached to it that would cause it to automatically jump outside the switch. Just as break is used to leave a loop block early, break is also used to leave a switch block early.
Syntax for switch staement in C
switch(expression)
{
case (constant-expression) : staements
....
case (constant-expression) : staements
default : statements
}
To work with a particular case your last statement in that group of statement must be break.
Without the break statement , when the last statement in the case has been executed, control "falls through" to the first statement in the following case; the case label (const-expression) for the next case is ignored. Without break (or some jump statement), control will flow from one case into the next.
Some corrections,
#include <stdio.h>
#include <stdlib.h>
#include "ratedzfunctions.h"
int main()
{
int selection, loop=1;
FILE* fajl;
//Opening the participants file
fajl=fopen("participants.txt","r+");
if (fajl==NULL)
{
printf("The file cannot be opened.\n");
exit(1); //handle error when file cannot be opened...
}
//MENU
do
{
printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>");
scanf("%d", &selection);
switch (selection)
{
case 1:
ratedz(fajl);
break;
case 2:
stats(fajl);
break;
case 3:
loop=0;
break;
default:
break;
}
}
while (loop==1)
{
//do stuff here
}
fclose(fajl);
return 0;
}
Here is my main function:
int main(int argc, char **argv)
{
LoadFile();
Node *temp;
char *key;
switch (GetUserInput())
{
case 1:
temp = malloc(sizeof(Node));
printf("\nEnter the key of the new node: ");
scanf("%s", temp->key);
printf("\nEnter the value of the new node: ");
scanf("%s", temp->value);
AddNode(temp);
free(temp);
break;
case 2:
key = malloc(sizeof(char *));
printf("Enter the key of the node you want to delete: ");
scanf("%s", key);
DeleteNode(key);
free(key);
break;
case 3:
PrintAll();
break;
case 4:
SaveFile();
break;
case 5:
return 0;
break;
default:
printf("\nWrong choice!\n");
break;
}
return 0;
}
The only problem with it is that after any case statement breaks, the program just exits out. I understand why, but I don't know how to fix it. I want the program to repeat itself each time even after the case statements. Would I just say:
main(argc, argv);
before every break statement?
wrap it in a while(1) { }
eg
while(1)
{
//switch... etc down to the close of the switch
}
After you reach a break statement control resumes at the end of the switch statement, so wrapping the entire switch in a while loop will make it repeat, but I would make it a separate function called from a loop in main:
void GetUserInput() {
// switch
}
int main()
{
while (1)
GetUserInput();
return 0;
}