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;
}
}
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;
}
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
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;
}
}
}
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;
}
I'm trying to implement a command line menu in C so that when the user enters a character, it will instantly process the character and carry out specific functions. The problem is, whenever I try to make it so that after each input is processed, the menu displays again and is ready for new input, the program will just continually read input and never process it unless I exit the program.
This is the code that works 1 time through:
char command;
command = getchar();
switch(command){
case 'c':
//create a new hash table;
break;
case 'l':
//look up a word;
break;
case 'f':
//read a file
break;
case 'p':
//print the table;
break;
case 'r':
//Remove a word
break;
case 'q':
exit(0);
break;
}
However, if I try to place it into an infinite loop to continually run, like I said, it will never process the inputs until I exit the program.
This code should work for you — it works for me. Note the use of int for the variable command.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
int command;
while ((command = getchar()) != EOF)
{
switch(command)
{
case 'c':
printf("Create a new hash table\n");
break;
case 'l':
printf("Look up a word\n");
break;
case 'f':
printf("Read a file\n");
break;
case 'p':
printf("Print the table\n");
break;
case 'r':
printf("Remove a word\n");
break;
case 'q':
printf("Quit\n");
exit(0);
break;
default:
printf("Unexpected input %d (0x%.2X) ('%c')\n",
command, command, isgraph(command) ? command : '.');
break;
}
}
return 0;
}