multiple items price calculation in C - c

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

Related

Do while loop in a switch statement

Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%d", &oper)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;}
}while(select == 1);
} ```
I think you want something like bellow code.
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
do{
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
printf("Press 0 to stop and 1 to continue: ");
scanf("%d", &select);
} while(select == 1);
printf("Finish");
}
change the following code bellow:
while(select != 0);
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
}while(select != 0);
}

no output for while loop with nested switch

#include <stdio.h>
int main()
{
char cha;
int m=0;
int f=0;
int tot;
while(cha=='m'||cha=='M'||cha=='F'||cha=='f')
{
printf("What is your gender?(m or f):\n");
scanf(" %c",&cha);
switch (cha)
{
case 'm':
case 'M':
printf("\nYou are a male");
++m;
printf("\nPress a to total up");
break;
case 'f':
case 'F':
printf("\nYou are a female");
++f;
printf("\nPress a to total up");
break;
case 'a':
tot=m+f;
printf("\nThe number of male is %d and the number of female is %d",m,f);
printf("\nThe total of male and female is:%d",tot);
break;
}
}
return 0;
}
So i just meddle around with my code trying to put up a program that can sum up the amount of m and f inputted in the program and end up with no output at all.The program just terminated without showing any output.I try to put semicolon at the end of while expression but the while loop end up doesnt even functioning.What's wrong with my code?
you don't give cha a value.
so it doesn't enter the while.
i think this edition is better. and also error handling is so important.
#include <stdio.h>
int main()
{
char cha;
int m=0;
int f=0;
int tot;
bool end = false;
while(end==false)
{
printf("What is your gender?(m or f):\n");
scanf(" %c",&cha);
switch (cha)
{
case 'm':
case 'M':
printf("\nYou are a male");
++m;
printf("\nPress a to total up");
break;
case 'f':
case 'F':
printf("\nYou are a female");
++f;
printf("\nPress a to total up");
break;
case 'a':
tot=m+f;
end = true;
printf("\nThe number of male is %d and the number of female is %d",m,f);
printf("\nThe total of male and female is:%d",tot);
break;
default:
printf("\nEnter valid sex.");
}
}
return 0;
This loop expression:
while(cha=='m'||cha=='M'||cha=='F'||cha=='f')
is evaluated before cha is given a value by the call to scanf(). So basically you get random behavior since cha is not initialized when that line is first reached. There's a very low chance that it holds any of the valid characters, to the loop exits immediately.
Also, please note that scanf() can fail; you should always check its return value.
while condition never gets true
never gets true you should modify this to work according to you although i have modified your code you can have a look of it.
`#include <stdio.h>
int main()
{
char cha;
int m=0;
int f=0;
int tot;
while(cha!='a')
{
printf("What is your gender?(m or f):\n");
scanf(" %c",&cha);
switch (cha)
{
case 'm':
case 'M':
printf("\nYou are a male");
++m;
printf("\nPress a to total up");
break;
case 'f':
case 'F':
printf("\nYou are a female");
++f;
printf("\nPress a to total up");
break;
case 'a':
tot=m+f;
printf("\nThe number of male is %d and the number of female is %d",m,f);
printf("\nThe total of male and female is:%d",tot);
break;
}
}
return 0;
`}
Hi your while loop is never satisfied as cha was never initialized before the while loop. As per your requirement it should be do while loop instead of a while loop. Modify your code like below:-
int main()
{
char cha;
int m=0;
int f=0;
int tot;
do
{
printf("What is your gender?(m or f):\n");
scanf(" %c",&cha);
switch (cha)
{
case 'm':
case 'M':
printf("\nYou are a male");
++m;
printf("\nPress a to total up");
break;
case 'f':
case 'F':
printf("\nYou are a female");
++f;
printf("\nPress a to total up");
break;
case 'a':
tot=m+f;
printf("\nThe number of male is %d and the number of female is %d",m,f);
printf("\nThe total of male and female is:%d",tot);
break;
}
} while(cha=='m'||cha=='M'||cha=='F'||cha=='f');
return 0;
}

How do I stop my C program from flooding the console with the same response

It was challenging to word the title for this question, but I hope you can forgive me as I will elaborate here.
The problem i'm having is my C program floods the console with "Enter the student's grade: F" if I enter anything other than an integer. I'm new to C, so I don't understand how to check if the input is of the valid type.
int main() {
int grade; //number 0-10 associated with the letter grade
while (1) {
printf("Enter the student's grade: ");
scanf("%i", &grade);
switch (grade) {
case 10: printf("A \n"); break;
case 9: printf("A \n"); break;
case 8: printf("B \n"); break;
case 7: printf("C \n"); break;
case 6: printf("D \n"); break;
case 5: printf("F \n"); break;
case 4: printf("F \n"); break;
case 3: printf("F \n"); break;
case 2: printf("F \n"); break;
case 1: printf("F \n"); break;
case 0: printf("F \n"); break;
default: printf("Please enter a valid test score \n"); break;
}
}
return 0;
}
Thanks for the help!
Check if scanf succeeded.
int nread = scanf("%i", &grade);
if (nread != 1) // scanf failed, start cleanup
{
scanf("%*[^\n]%*c");
}
Look for any book and you'll know that scanf returns number of elements that were successfully read, so if you type a letter, it will fail to read an integer and return 0 (nothing read), then you can know there's something wrong and discard wrong stuffs.
Since scanf don't read anything if it encounters an error, the wrong stuff will remain in the input buffer, and badly it'll break the next scanf, leading to an infinite output flushing.
P.S. You don't need to repeat the statements after case 5 4 3..., just merge them into one:
case 5: // Remove these and leave the last one there
case 4:
case 3:
case 2:
case 1:
case 0: printf("F \n"); break;
Check the return of scanf. 1 means an integer was successfully scanned. 0 means the input was not an integer. Clean the input stream and try again.
#include <stdio.h>
int main ( void) {
int grade; //number 0-10 associated with the letter grade
int valid = 0;
while (1) {
do {
printf("Enter the student's grade: ");
if ( 1 != ( valid = scanf("%i", &grade))) {// 1 is success
if ( EOF == valid) {
printf ( "found EOF\n");
return 0;
}
while ( '\n' != getchar ( )) {//clear input stream
}
}
} while ( !valid);
switch (grade) {
case 10: printf("A \n"); break;
case 9: printf("A \n"); break;
case 8: printf("B \n"); break;
case 7: printf("C \n"); break;
case 6: printf("D \n"); break;
case 5: printf("F \n"); break;
case 4: printf("F \n"); break;
case 3: printf("F \n"); break;
case 2: printf("F \n"); break;
case 1: printf("F \n"); break;
case 0: printf("F \n"); break;
default: printf("Please enter a valid test score \n"); break;
}
}
return 0;
}

Why will my program not flag characters?

I'm making this program using switch statements that will assign letter grades based on if the user enters numbers 0 - 10. If the user enters a number that is not 0-10, the program outputs an error message and has the user re-enter. However, if the user enters a character the program will loop at the default case. I want it to output the error message from the default case once, and have them re-enter if they enter a character. I'm not sure as to why it loops the default case when a character is entered though.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
}
}
return 0;
}
Try something like:
#include <stdio.h>
int main()
{
int grade;
int r=0;
while(r != 1)
{
scanf("%i", &grade);
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
r=1
break;
case 8:
printf("Your grade is a B\n");
r=1
break;
case 7:
printf("Your grade is a C\n");
r=1
break;
case 6:
printf("Your grade is a D\n");
r=1
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
r=1
break;
default:
printf("Invalid score, please re-enter\n");
break;
}
}
return 0;
}
This will clear the input buffer on an invalid input and allow a retry.
#include <stdio.h>
int main()
{
int grade;
int r;
while((r = scanf("%i", &grade)) != EOF)
{
if ( r != 1) {//r == 1 is successful input of integer
grade = -1;//reset grade on invalid input
}
switch(grade)
{
case 10:
case 9:
printf("Your grade is an A\n");
break;
case 8:
printf("Your grade is a B\n");
break;
case 7:
printf("Your grade is a C\n");
break;
case 6:
printf("Your grade is a D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("Your grade is an F\n");
break;
default:
printf("Invalid score, please re-enter\n");
while ( getchar() != '\n');//clear input buffer
}
}
return 0;
}
The reason your code always loops is because there is no way to exit out of your while other than to kill the program. Remember that break only breaks out of the inner-most switch or loop.
The cleanest way to break out of multiple levels is to use a flag. One way to do what you want is like this:
bool valid_grade = false;
while(!valid_grade && (r = scanf("%i", &grade)) != EOF)
{
valid_grade = true;
switch(grade)
{
case 10:
// unchanged from your code
default:
valid_grade = false;
printf("Invalid score, please re-enter\n");
}
}

Repeat a menu and prompt if the user entered invalid choice in C

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

Resources