problem with switch statement ..using switch statement.


can someone please help me why this wont work.
I'm a newbie on c++
Im creating a program similar to a fast food chain counter,that calculates
type of meal and multiplies number of orders.
A=45
B=50
c=55
D=60
E=75



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <conio.h>
#include <iostream.h>

float getPrice(char valueMeal);

 {
      float price;
      {
            switch(orderType1,orderType2,orderType3,orderType4);
      
      case 'A': price=45;
      break;
      case 'B': price=50;   
      break;
      case 'C': price=55;
      break;
      case 'D': price=60;
      break;
      case 'E':  price=75;   
      break;  
      default: price=0;
     
   
      }
         
         return price;
           }      
 int main()
           {
            float numOfOrder1,numOfOrder2,numOfOrder3,numOfOrder4,subTotal1,subTotal2,subTotal3,subTotal4,subTotal5;
            char orderType1,orderType2,orderType3,orderType4,totalAmount;  
                
                scanf( "%c %f\n\n", &orderType1,&numOfOrder1);
                
                scanf( "%c %f\n\n", &orderType2,&numOfOrder2);
                
                scanf( "%c %f\n\n", &orderType3,&numOfOrder3);
                
                scanf( "%c %f\n\n" , &orderType4,&numOfOrder4);
                
                scanf( "%c %f\n\n" , &orderType5,&numOfOrder6);
             
             orderType1=getPrice( valueMeal);
              subTotal1= price*numOforder1;
              printf( "Subtotal Is :" subTotal1);
              
              orderType2=getPrice( valueMeal);
              subTotal2= price*numOforder2; 
              printf( "Subtotal Is :" subTotal2); 
             
               
              orderType3=getPrice( valueMeal);
             
              subTotal3= price*numOforder3;
              printf( "Subtotal Is :" subTotal3); 
             
             orderType4=getPrice( valueMeal);
             
              subTotal4= price*numOforder4;
              printf( "Subtotal Is :" subTotal4);  
              
              totalAmount=subTotal1+subTotal2+subTotal3+subTotal4;
              
              
              printf("Total Amount Is:");
              printf("%0.f ",totalAmount);
              
              return 0;
              system("pause");
              
              
              }
              
            
                
                
              
Last edited on
closed account (z05DSL3A)
Line 27 return price;
switch(orderType1,orderType2,orderType3,orderType4);

Should read

switch (valueMeal)

You have a semicolon after the switch statement which effectively ends the switch test. Also your test parameter was wrong.

Also, while its really not an issue you are assigning an integer value to a float. i.e. price=45;
Would be better for readability and not type converting to be written as:
price = 45.0;
Last edited on
closed account (z05DSL3A)
This just fixes the errors to get it to compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <conio.h>
#include <iostream.h>

float getPrice(char valueMeal)
{
    float price = 0;
    switch(valueMeal)
    {
        case 'a':
        case 'A': 
            price = 45;
            break;
        case 'b':
        case 'B': 
            price = 50;   
            break;
        case 'c':
        case 'C': 
            price = 55;
            break;
        case 'd':
        case 'D': 
            price = 60;
            break;
        case 'e':
        case 'E': 
            price = 75;   
            break;  
        default: price=0;
    }
       
    return price;
}      

int main()
{
    float numOfOrder1,
          numOfOrder2,
          numOfOrder3,
          numOfOrder4,
          subTotal1,
          subTotal2,
          subTotal3,
          subTotal4,
          subTotal5,
          valueMeal,
          totalAmount;
    
    char orderType1,
         orderType2,
         orderType3,
         orderType4,
         orderType5;  

    scanf( "%c %f\n\n", &orderType1, &numOfOrder1);

    scanf( "%c %f\n\n", &orderType2, &numOfOrder2);

    scanf( "%c %f\n\n", &orderType3, &numOfOrder3);

    scanf( "%c %f\n\n", &orderType4, &numOfOrder4);

    valueMeal = getPrice(orderType1);
    subTotal1 = valueMeal * numOfOrder1;
    printf("Subtotal Is : %f \n", subTotal1);

    valueMeal = getPrice(orderType2);
    subTotal2 = valueMeal * numOfOrder2; 
    printf("Subtotal Is : %f \n", subTotal2); 

    valueMeal = getPrice(orderType3);
    subTotal3 = valueMeal * numOfOrder3;
    printf("Subtotal Is : %f \n", subTotal3); 

    valueMeal = getPrice(orderType4);
    subTotal4 = valueMeal * numOfOrder4;
    printf("Subtotal Is : %f \n", subTotal4);  

    totalAmount = subTotal1 + subTotal2 + subTotal3 + subTotal4;


    printf("Total Amount Is:");
    printf("%0.f ",totalAmount);
    
    system("pause");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.