help with switch!mine is not work it...

#include <iostream>
#include<cmath>
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";
cin>>"time";
cout<<"pls enter your time exit";
cin>>time;

switch(hours_charge)
{
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";
cin>>"x";
cout<<"the charge is"<<"RM";
cin>>"x*case">>"="




return 0;
}
I am a newbie here

But I know that you cannot cin a string. You can cin variables...

can you just do this instead

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
#include <iostream>
#include <cmath>

using namespace std;

int main(){
    int time_enter;
    int time_exit;

    cout << "Time entry: ";
    cin >> time_enter;
    cout << "Time Exit: ";
    cin >> time_exit;

    int duration = time_exit - time_enter;
    int charge = 0;

    switch( duration ){
        case 1: charge = 2; break;
        case 2: charge = 3; break;
        case 3: charge = 4; break;
        case 4: charge = 5; break;
        case 5: charge = 8; break;
        default : charge = -1; break;
    }

    if( charge == -1 ){
        cout << "Invalid hours of charge" << endl;
    } else {
        cout << "The charge is RM" << charge << endl;
    }
    return 0;
}



RM seems familiar. is it ringgit by any chance ?

I don't really get what you want but I hope this helps you
u can cin to a string class object
Topic archived. No new replies allowed.