this pgrmming need to use "switch" but mine is not work it.why?

#include <iostream>
using namespace std;
int main()
{int x;
char time, charge;
cout<<"case 1<=2hours,case 2<=3hours,case 3<=4hours,case 4<=5hours,case 5>6hours";
cout<<"pls enter your time entry";
cout<<"pls enter your time exit";

case'1':cout<< "the charge is RM2.00\n";break;
case'2':cout<< "the charge is RM3.00\n";break;
case'3':cout<< "the charge is RM4.00\n";break;
case'4':cout<< "the charge is RM5.00\n";break;
case'5':cout<< "the charge is RM8.00\n";break;
default:cout<<"invalid hours of charge";

cout<<"no of hours parked is"<<"x";
cout<<"the charge is"<<"RM";

return 0;
Last edited on
What is it cin>>"time"; ?
These are your variables:
char time, charge;

This line makes no sense:
cin >> "time"; // "time" is a string -- NOT your variable time.

You can not store the two different values in one variable.
cout << "pls enter your time entry";
cin >> time; // time entry is now stored in variable time

cout << "pls enter your time exit";
cin >> time; // time exit is now stored in variable time -- time entry is GONE.

Your switch is using a non-existant variable hours_charge. I assume you meant charge:

However, you've never given charge a value, so it will have undefined behaviour (the value will be whatever was stored in that memory before it was used for your variable).

You probably want to do something like this before your switch:
cout << "pls enter your entry time";
cin >> entry_time;

cout << "pls enter your exit time";
cin >> exit_time;

charge = (exit_time - entry_time) * hourly_fee;

Last edited on
Topic archived. No new replies allowed.