I am working on a program. This is what I am attempting to achieve;
The program should present a menu of pay rates from which to choose. Use a switch to select the pay rate. The beginning of a run should look something like this:
If choices 1 through 4 are selected, the program should request the hours worked. The program should recycle until 5 is entered. If something other than choices 1 through 5 is entered, the program should remind the user what the proper choices are and then recycle. Use #defined constants for the various earning rates and tax rates.
This is the code I have so far, what am I missing?;
#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice, hour;
float taxe, total;
printf("****************************************************************\n");
printf("\nEnter the number corresponding to the desired pay rate or action");
printf("\n1)$8.75/hr");
printf("\n2)$9.33/hr");
printf("\n3)$10.00/hr");
printf("\n4)$11.20hr");
printf("\n5)Quit");
printf("**********************************************************\n");
scanf("%d", &choice);
printf("Please enter number of hours: ");
scanf("%d", &hour);
switch(choice){
case 1:
total = 8.75* hour;
break;
case 2:
total = 9.33*hour;
break;
case 3:
total = 10.00*hour;
break;
case 4:
total = 11.20*hour;
break;
case 5:
break;
return 0;
}
}
what am I missing?
What you are missing is the actual iteration.
The program should recycle until 5 is entered.
This means that you have to do all your work inside a loop, which will check each time if the value read is 5, which you need in order to stop iterating. You can use one of the following ways :
while loop:
scanf("%d", &choice);
while (choice != 5)
{
....
switch(choice){
....
}
....
scanf("%d", &choice);
}
for loop:
for (scanf("%d", &choice); choice != 5; scanf("%d", &choice);)
{
....
switch(choice){
....
}
....
}
do-while loop:
do{
scanf("%d", &choice);
....
switch(choice){
....
}
....
}while(choice != 5);
You did'nt used a loop. It can't recycle until 5 entered.
After taking the choice, insert a while loop :
while(choice != 5){
and process normally your switch, ignoring the 5 value, adding a "default" case if another value is entered.
If you want the total program to recycles, use a do{} while(); starting on your printf, ending afgter the switch.
you have yo use do while loop here like below
do
{
clrscr();
printf("****************************************************************\n");
printf("\nEnter the number corresponding to the desired pay rate or action");
printf("\n1)$8.75/hr");
printf("\n2)$9.33/hr");
printf("\n3)$10.00/hr");
printf("\n4)$11.20hr");
printf("\n5)Quit");
printf("**********************************************************\n");
scanf("%d", &choice);
printf("Please enter number of hours: ");
scanf("%d", &hour);
switch(choice){
case 1:
total = 8.75* hour;
break;
case 2:
total = 9.33*hour;
break;
case 3:
total = 10.00*hour;
break;
case 4:
total = 11.20*hour;
break;
case 5:
break;
default :
printf("\n please enter proper choice");
getch();
}
}while(choice !=5);
Related
My array inside case 4 in looping Switch Menu doesn't print/display the value of the last array when the user input goes beyond array[4].
I tried to take the case 4 out and make it a single program to check if it doesn't really work on its own but it works fine, but when I put it back into the Switch, same issue again. I thought that maybe the initialization part is the problem. Help
`
#include <stdio.h>
int main ()
{
char first[20],last[20];
float math,eng,sci,avg;
int a,b,c,d,diff,array[diff],e,i,input;
do{
printf("\nMAIN MENU\n");
printf("[1] Basic Input Output\n[2] Conditional Statement\n[3] Looping Construct\n[4] Array\n[5] About\n[6] Exit");
printf("\n\nChoose: ");
scanf("%d",&input);
printf("\n");
switch (input)
{
case 1:
printf("\nEnter your given name:");
scanf("%s",first);
printf("Enter your surname:");
scanf("%s",last);
printf("\nHello %s %s!\n",first,last);
break;
case 2:
printf("\nEnter your grade in Math:");
scanf("%f",&math);
printf("\nEnter your grade in Science:");
scanf("%f",&sci);
printf("\nEnter your grade in English:");
scanf("%f",&eng);
avg=(math+eng+sci)/3;
if(math>eng&&sci)
{
printf("\nHighest grade is in: Math");
}
if(eng>math&&sci)
{
printf("\nHighest grade is in: English");
}
if(sci>eng&&math)
{
printf("\nHighest grade is in: Science");
}
if(math==eng)
{
printf("\n--Math and English tied grades--");
}
if(math==sci)
{
printf("\n--Math and Science tied grades--");
}
if(eng==sci)
{
printf("\n--Science and English tied grades--");
}
printf("\nYour average in 3 subjects:%f\n",avg);
break;
case 3:
printf("Enter beginning number: ");
scanf("%d",&b);
printf("Enter ending number: ");
scanf("%d",&c);
printf("\nCounting from %d to %d\n",b,c);
while(b<=c)
{
printf("%d ",b);
b++;
}
printf("\n");
break;
case 4:
printf("Enter Starting Series of Numbers: ");
scanf("%d",&a);
printf("Enter Ending Series of Numbers: ");
scanf("%d",&d);
diff=(d-a);
array[diff]=d;
printf("Select Array Value to Display: 0 to %d: ",diff);
scanf("%d",&e);
for(i=0;i<=diff;i++)
{
array[i]=a;
if(i==e)
{
printf("%d\n",array[i]);
}
a++;
}
break;
case 5:
printf("Abouts\n");
break;
case 6:
printf("Thank you!");
break;
}
}while(input != 6);
return 0;
}
`
This should be a comment, but I donĀ“t have enough reputation yet.
As Gerhardh said, the problem is that you use diff before being initialized (which causes undefined behaviour)
As you said, if you move the initialization of array inside the case 4: after diff being set the program should work as intended. If you want to keep it before the switch, try using int* and malloc/free inside the case 4: (and add #include <stdlib.h>) Anyway, you should check diff as positive value before using it to set the size of array
About optimizing the code, you can skip the line array[diff]=d; because you are setting the same value inside the for loop. And you can remove the line a++; if you changes array[i]=a; -> array[i]=a+i;
I'm very new to C programming, and I need a help!
I have made a program of calculator with the help of Switch Statement and I want this program to be in a loop, So that it ask the menu (i.e Enter your Choice: Divide,Multiplication,Addition etc) again and again with the user.
Also I want, an End option with the Cases in Menu, which will close the program. I don't know how to code that End option which will make the Program Close.
Please Help!
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,choice;
float sum,mul,div,sub,quo;
printf("\n\t\t\t\t CALCULATOR");
printf("\nEnter the First Number: ");
scanf("%d",&a);
printf("Enter the Second Number: ");
scanf("%d",&b);
printf("\n Enter Your Choice");
printf("\n\n1.Sum");
printf("\n2.Multiplication");
printf("\n3.Division");
printf("\n4.Subtraction");
printf("\n5.Quotient");
printf("\nYOUR CHOICE: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
sum=a+b;
printf("Sum= %f\n",sum);
break;
case 2:
mul=a*b;
printf("Multiplication= %f\n",mul);
break;
case 3:
div=a/b;
printf("Division= %lf\n",div);
break;
case 4:
sub=a-b;
printf("Subtraction= %f\n",sub);
break;
case 5:
quo=a%b;
printf("Quotient= %f\n",quo);
break;
default:
printf("\n Unavailable Choice");
}
return 0;
getch();
}
Assuming you add option number 6 to close the calculator, you can add the following case to quit the program -
case 6:
// print bye message or whatever
exit(0);
break;
You can read about the exit function and what the arguments passed to it mean. Usually a 0 indicates successful exit from the program.
You also need to include stdlib.h if you want to use exit.
I would suggest introducing a loop condition variable, eg. CarryOn, initialise it to 1, and use that in a while loop. Let the loop continue as long as the variable is 1. When the user selects "End", set the variable to 0 in the case. That will end the loop.
Let me clarify it with some example code outline:
int CarryOn = 1;
while (CarryOn == 1)
{
// ...your original code
// ...
// add to switch
case 6: // 6 will be the "End" / "Exit" option
CarryOn = 0;
break;
}
You will need to have a while loop.
while(true){
printf("\n\t\t\t\t CALCULATOR");
printf("\nEnter the First Number: ");
scanf("%d",&a);
....
//return 0; remove the return statement
getch();
}
and also add the menu item 6) Exit to your printf and handle that option as described in the one of the answers above.
case 6:
// print bye message or whatever
exit(0);
break;
I am trying this code with exit case, the exit case doesn't work it still asks me for the two numbers and then it exits. I have provided the output. What is the alternative to the switch case that I can use for menu driven programs?
Sample input/output:
::::::Menu:::::
1. Addition:
2. Subtraction
3. Multiplication
4. Division
5. Mod
6. Exit
Enter your choice:6
Enter the values of a and b:3 4
#include<stdio.h>
#include<stdlib.h>
int main(){
int a,b,c;
int no;
do{
printf("\n::::::Menu:::::\n");
printf(" 1. Addition:\n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Mod \n 6. Exit");
printf("\nEnter your choice:");
scanf("%d",&no);
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
switch(no){
case 1:
c = a + b;
printf("\nAddition is:%d",c);
break;
case 2:
c = a - b;
printf("\nSubtraction is:%d",c);
break;
case 3:
c = a * b;
printf("\nMultiplication is:%d",c);
break;
case 4:
c = a / b;
printf("\nDivision is:%f",c);
break;
case 5:
c = a % b;
printf("\nMod is:%d",c);
break;
case 6:
exit(1);
default:
printf("\nInvalid choice\n");
break;
}
}while(no!=6);
return 0;
}
Your program asks for two numbers because you are checking for the exit code after the second scanf statement. If you wish the program to exit when 6 is entered, then you have to add an if statement in between the first and second scanf. You should additionally removed the exit case from your switch statement. Below is an example:
printf("\nEnter your choice:");
scanf("%d",&no);
if (no == 6)
exit(0);
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
You're reading the user's menu option, then asking for the next two numbers before you get to the switch statement, so of course it will always ask for those two numbers.
You need to either check specifically for 6 right after reading the menu input, or you need to move the second prompts to only where they're needed, i.e. inside each case.
It keeps asking for the two numbers because the switch statement is positioned right AFTER you ask for the user input. I restructured your code, this should work:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a,b,c;
int no;
while(1){
printf("\n::::::Menu:::::\n");
printf(" 1. Addition:\n 2. Subtraction \n 3. Multiplication \n 4. Division \n 5. Mod \n 6. Exit");
printf("\nEnter your choice:");
scanf("%d",&no);
if (no == 6)
break;
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
switch(no){
case 1:
c = a + b;
printf("\nAddition is:%d",c);
break;
case 2:
c = a - b;
printf("\nSubtraction is:%d",c);
break;
case 3:
c = a * b;
printf("\nMultiplication is:%d",c);
break;
case 4:
c = a / b;
printf("\nDivision is:%f",c);
break;
case 5:
c = a % b;
printf("\nMod is:%d",c);
break;
default:
printf("\nInvalid choice\n");
break;
}
}
return 0;
}
Note how I changed the do-while to a while-true and explicitly check for no == 6 PRIOR to asking for user input.
The statements in C are meant to be executed sequentially unless you disrupt the normal flow of action using jumps. In your case
printf("\nEnter your choice:");
scanf("%d",&no);
printf("\nEnter the values of a and b:");
scanf("%d%d",&a,&b);
// Only now does the switch start.
You ask for the choice first and then for the two numbers. That is why you always end up in entering the two values. One way is to take the exit out of the switch-case which perhaps is the simplest solution I guess. Something like
printf("\nEnter your choice:");
scanf("%d",&no);
if ( 6 == no )
exit(1);
What is the alternative to the switch case that I can use for menu
driven programs
Well, switch-case is exactly made for that. Why think of an alternative?
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
int main()
{
char ch;
while(1)
{
printf("l.print l c.print c q. exit \n");
printf("enter choice ");
scanf("%c",&ch);
switch(ch)
{
case 'l':
printf("You have typed l \n");
break;
case 'c':
printf("yoh have typed c \n");
break;
case 'q':
exit(0);
}
}
return 0;
}
void menu(){
printf("\n");
printf("1. Convert integers in decimal number system to binary numbers \n");
printf("2. Compute a consecutive square root expression \n");
printf("3. Solve a quadratic equation \n");
printf("4. Print something fun \n");
printf("q. Quit\n \n");
printf(" Enter your choice: ");
}
main () {
char choice;
do {
menu();
scanf("%c", &choice);
switch (choice){
case '1':
...
case '2':
....
case '3':
...
case '4':
....
default:
printf("Wrong choice. Please enter again: ");
break;
}
}
while (choice != 'q');
}
Here is my general idea, but I can't get it to prompt the wrong choice and repeat the menu. When I enter a wrong choice, the output is as follows:
For example, I entered 5:
Enter your choice: 5
Wrong choice, please enter again:
1. Convert integers in decimal number system to binary numbers
2. Compute a consecutive square root expression
3. Solve a quadratic equation
4. Print something fun
q. Quit
Enter your choice: (this is where I get to input)
Take a look at the below changes:
Change your scanf() as
scanf(" %c",&choice);
A space before the %c will make sure all special characters including newline is ignored.Without this everytime there is a newline in the buffer to and scanf reads from it and you will see that your look doesn't work as expected.
After this please make sure once the default case is hit you need to break from the while() loop.
do {
menu();
scanf(" %c", &choice);
switch (choice){
case '1':
break;
case '2':
break;
case '3':
break;
case '4':
break;
default:
{
printf("Wrong choice. Please enter again: ");
break;
}
}
}
while (choice != 'q');
First thing, Put break; in each case so only the case you choose will apply. To solve the printing 2 times issue, just Change the %c in scanf("%c", &choice); to %s >> scanf("%s", &choice);
This is the code:
#include <stdio.h>
void menu(){
printf("\n");
printf("1. Convert integers in decimal number system to binary numbers \n");
printf("2. Compute a consecutive square root expression \n");
printf("3. Solve a quadratic equation \n");
printf("4. Print something fun \n");
printf("q. Quit\n \n");
printf(" Enter your choice: ");
}
main () {
char choice;
do {
menu();
scanf("%s", &choice);
switch (choice){
case '1':
printf("1 \n");
break;
case '2':
printf("2 \n");
break;
case '3':
printf("3 \n");
break;
case '4':
printf("4 \n");
break;
case 'q':
break;
default:
printf("Wrong choice. Please enter again: ");
break;
}
}
while (choice != 'q');
}
Hi i am trying to calculate total price for 6 different items and 3 of them must be selected. by using C i tried use switch statement but unfortunately i stuck after 2 selection.
Thank you
#include<stdio.h>
int main()
{
char ch;
int total=0;
printf("Which cou will you choose:\n");
printf("a) cpu 1 \n");
printf("b) cpu 2 \n");
scanf("%c", &ch);
switch (ch)
{
case 'a':
printf("The price is 110 \n");
total+=110;
break;
case 'b':
printf("The price is 140\n");
total+=140;
break;
default:
printf("Invalid choice.Switching to next question\n");
}
printf("Which ram will you choose:\n");
printf("q) ram 1 \n");
printf("w) ram 2 \n");
scanf("%c", &ch);
switch (ch)
{
case 'q':
printf("The price is 10 \n");
break;
case 'w':
printf("The price is 14\n");
printf("Please Wait\n");
break;
default:
printf("Invalid choice\n");
break;
}
printf("Which hdd will you choose:\n");
printf("x) hdd 1 \n");
printf("y) hdd 2 \n");
scanf("%c", &ch);
switch (ch)
{
case 'x':
printf("The price is 65 \n");
break;
case 'y':
printf("The price is 75\n");
printf("Please Wait\n");
break;
default:
printf("Invalid choice\n");
break;
}
printf("That'll cost %d",total);
return 0;
}
after i add everything once i start program it works only for first switch statement
You need a variable total to add up the total cost. Your code will look like this:
#include<stdio.h>
int main()
{
char ch;
int total=0;
printf("Which option will you choose:\n");
printf("a) cpu 1 \n");
printf("b) cpu 2 \n");
scanf("%c", &ch);
switch (cpu)
{
case 'a':
printf("The price is 110 \n");
total+=110;
break;
case 'b':
printf("The price is 140\n");
total+=140;
break;
default:
printf("Invalid choice.Switching to next question\n");
}
//ask next question.
//get input
//use a switch like the above one
//add total
//repeat this as many times you want
printf("That'll cost %d",total);
return 0;
}