Question

Rewrite the following highlighted if...else section using a switch. Only rewrite the code that you need...

Rewrite the following highlighted if...else section

using a switch. Only rewrite the code that you

need to change, not the entire code.

#include

#include

#define

TRUE

1

#define

FALSE

0

int main(void

)

{

enum Pops{ Coke = 1, Pepsi, Root_Beer, Grape, Seven_Up, Mountain_Dew, Dr_Pepper,

Orange };

int PopSelection, DietStyle;

float

Price;

printf(

"Please enter your favourite pop:

\n");

printf(

"1. Coke\

n");

printf(

"2. Pepsi

\n" );

printf(

"3. Root Beer\

n");

printf(

"4. Grape

\n" );

printf(

"5. 7 Up\

n");

printf(

"6. Mountain Dew\

n");

printf(

"7. Dr. Pepper

\n");

printf(

"8. Orange\

n");

fflush(

stdin);

scanf("%d"

, &PopSelection);

printf(

"\n\nDo you prefer:\

n.1Diet

\n2.Regular");

fflush(

stdin);

scanf("%d"

, &DietStyle);

if (DietStyle == 1) DietStyle = TRUE;

else DietStyle = FALSE

;

if (( PopSelection == Coke) || (PopSelection == Pepsi)) Price = 1.90;

else

{

if ((PopSelection == Root_Beer) && (DietStyle == TRUE)) Price = 1.50;

else

{

if ((PopSelection == Root_Beer) && (DietStyle == FALSE

)) Price = 1.60;

else

{

if (PopSelection == Grape) Price = 1.10;

else

{

if ((PopSelection == Seven_Up) || (PopSelection == Mountain_Dew))

Price = 2.00;

else

{

if (PopSelection == Dr_Pepper) Price = 1.55;

else

{

if (PopSelection

== Orange) Price = 1.75;

else Price = 0.00;

}

}

}

}

}

}

printf(

"The price of the pop is: %.2f", Price);

return

0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the switch statement for if else block. As requested only rewritten code is posted and not the entire code.

switch(PopSelection)

{

case 'Coke':

price=1.90;

break;

case 'Pepsi':

price=1.90;

break;

case 'Root_Beer':

switch(DietStyle)

{

case 'TRUE':

Price = 1.50;

case 'FALSE':

Price = 1.60;

// operator is doesn't match any case constant

default:

printf("Error! operator is not correct");

}

break;

case 'Grape':

price=1.10;

break;

case 'Seven_Up':

price=2.00;

break;

case 'Mountain_Dew':

price=2.00;

break;

case 'Dr_Pepper':

price=1.55;

break;

case 'Orange':

price=1.75;

break;

// operator is doesn't match any case constant

default:

Price = 0.00;

}

Since only partial code is requested so it is not possible to provide sample input and output.

Add a comment
Know the answer?
Add Answer to:
Rewrite the following highlighted if...else section using a switch. Only rewrite the code that you need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure...

    C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure you test your code. Supplied code #include <stdio.h> int main(void) { char ch; int countA = 0; int countE = 0; int countI = 0; printf("Enter in a letter A, E, or I.\n"); scanf(" %c", &ch); //Replace the following block with your switch if(ch == 'E' || ch == 'e') countE++; else if(ch == 'A' || ch == 'a') countA++; else if(ch == 'I'...

  • Could you do that in C language? Here is the code which we got #include <stdio.h>...

    Could you do that in C language? Here is the code which we got #include <stdio.h> #define MAX_SIZE 20 // function definitions void displaySpiral(int matrix[][MAX_SIZE], int size); void displayMatrix(int matrix[][MAX_SIZE], int size); int takeInput(int inputMatrix[][MAX_SIZE]); int main() { int matrix[MAX_SIZE][MAX_SIZE]; int matrixSize = takeInput(matrix); printf("Displaying the whole matrix:\n"); fflush(stdout); displayMatrix(matrix, matrixSize); printf("Now, displaying the matrix in a spiral way:\n"); fflush(stdout); displaySpiral(matrix, matrixSize); return 0; } // already implemented for you int takeInput(int inputMatrix[][MAX_SIZE]) { int size; printf("What is the size...

  • I'm writing code for a poker game and I'm not sure where I'm going wrong with...

    I'm writing code for a poker game and I'm not sure where I'm going wrong with it. My compiler is sending back errors for lines (typedef int bool)(the two char* declarations) and the two functions before my if else statement (approx lines 109). #include <stdio.h> #include <stdlib.h> #include <time.h> #define SUITS 4 #define FACES 13 #define AVAILABLE 0 #define TAKEN 1 #define SIZE 5 #define TRUE 1 #define FALSE 0 void dealACard(char *suits[], char *faces[], int deck[][FACES]); void dealAHand(char *suits[],...

  • Create a functional ATM using the code base given in the example. You must use the...

    Create a functional ATM using the code base given in the example. You must use the routines provided in the code base example. Failure to follow the directions will result in an 'F' the example is down below! /* * @desc: Simple ATM machine * @duedate: 23-April-2019 * * * */ #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> char menu(); char transaction_menu(); /* Verifies the Pin Entered match the PIN on record */ int validatePin(int); /* Verifies the funds...

  • #include <stdio.h> #include string.h     #define C17_PRICE 12.80 #define F25_PRICE 4.29 #define DN3_PRICE 10.00 #define GG7_PRICE...

    #include <stdio.h> #include string.h     #define C17_PRICE 12.80 #define F25_PRICE 4.29 #define DN3_PRICE 10.00 #define GG7_PRICE 35.00 #define MV4_PRICE 18.49 #define DISCOUNT        0.05 #define DISC_THRES    10 #define SAME_DAY_DELIVERY =   35.0 #define NEXT_DAY_DELIVERY =   20.0    typedef enum{ WRONG, C17, F25, DN3, GG7, MV4} part     /*--- Function Prototypes ------------*/ part getPartType ( void ); int getquantity ( void ); int getDeliveryOption ( void ); float calcPriceOfParts( part orderedPart, int quantity ); float calcTotalCarges ( float orderPrice, char...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • Given the following C code. Develop a evaluate post fix function that calculates the converted post...

    Given the following C code. Develop a evaluate post fix function that calculates the converted post fix. #include <stdio.h> #include <ctype.h> #define max 10 char s[100]; int top=-1; void push(char); char pop(); int precede(char); main(){    char infix[100],postfix[100],ch;    int i=0,j=0;    // Read infix expression from user    printf("Enter infix expression:");    scanf("%s",infix);    // evaluate each char in infix expression    while((ch=infix[i++])!='\0'){        // if it is number, add it to postfix expression        if(isalnum(ch)){   ...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

  • Need help answering this java programming question. Thank you in advance! g. Re-code the following as...

    Need help answering this java programming question. Thank you in advance! g. Re-code the following as a do-while loop with a switch statement for the if-elses, Assume goingToMovies is declared and has been set to true, so it can enter the loop. Assume choice, movie, Cont and input are already declared. while(goingToMavies) Sustem.aut printf("%nChoose a number from 1 through 5 for the movie" + "you want to watch: ") choice inputnextint) iffchoice 1) movie "Shazam!" else iffchoice 2) movie "A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT