Adding an 'if' statement into a 'switch' statement.

Ok, I'm trying to use a 'switch' statement, but with two possible scenarios for one of the switches. If the time is greater than 1min, I want it to output the time in mins. If it is 1min or less, I want it to output the time in secs. Everything else in my program is going the way I want it to. Can someone help me out here? Oh, bear with me on the words. I like to entertain myself while I learn this.

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
  int main ()
{
    cout<<"Enter a dollar amount to convert into equivalent currency.\n";
    int dollars;
    cin>>dollars;
    cout<<"Now choose a conversion type by entering the appropriate number for your selection.\n";
    cout<<"1.  Dirt (in kgs.)\n";
    cout<<"2.  Punches to face.\n";
    cout<<"3.  Time in ring with starving grizzly bear.\n";
    char convert;
    cin>>convert;
    switch (convert) {
case '1':
    cout<<"$"<<dollars<<" = "<<dollars*1.5<<"kgs.";
    break;
case '2':
    cout<<"$"<<dollars<<" = "<<dollars*2.5<<" punches.";
    break;
case '3':
    if (dollars>1)
        cout<<"$"<<dollars<<" = "<<dollars*0.2<<"mins. of fun.";
    else (dollars<=1)
        cout<<"$"<<dollars<<" = "<<dollars*0.2*60<<"secs. of fun.";
default:
    cout<<"Wow, you must be as dumb as a stump.";
    cout<<"Try entering one of the given numbers next time.";
    break;
    }
}
Last edited on
1
2
    else (dollars<=1)
        cout<<"$"<<dollars<<" = "<<dollars*0.2*60<<"secs. of fun.";


This should be else if What you are doing is

else evaluate dollars <= 1

then output..


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
#include <iostream>
using std::cout;
using std::cin;

int main ()
{
    cout<<"Enter a dollar amount to convert into equivalent currency.\n";
    int dollars;
    cin>>dollars;
    cout<<"Now choose a conversion type by entering the appropriate number for your selection.\n";
    cout<<"1.  Dirt (in kgs.)\n";
    cout<<"2.  Punches to face.\n";
    cout<<"3.  Time in ring with starving grizzly bear.\n";
    char convert;
    cin>>convert;
    switch (convert) {
case '1':
    cout<<"$"<<dollars<<" = "<<dollars*1.5<<"kgs.";
    break;
case '2':
    cout<<"$"<<dollars<<" = "<<dollars*2.5<<" punches.";
    break;
case '3':
    if (dollars>1)
        cout<<"$"<<dollars<<" = "<<dollars*0.2<<"mins. of fun.";
    else if (dollars<=1) //or just           else
        cout<<"$"<<dollars<<" = "<<dollars*0.2*60<<"secs. of fun.";
    break;
default:
    cout<<"Wow, you must be as dumb as a stump.";
    cout<<"Try entering one of the given numbers next time.";
    break;
    }
}


Though you could also simply use an else instead of else if. Because if it is not greater than 1 it must be less than or equal to 1.

You are also missing a break in case 3. So default will always be called.
Last edited on
else by itself does not have a condition. It should be either
1
2
else //(dollars<=1)
        cout<<"$"<<dollars<<" = "<<dollars*0.2*60<<"secs. of fun.";

or
1
2
else if(dollars<=1)
        cout<<"$"<<dollars<<" = "<<dollars*0.2*60<<"secs. of fun.";

You also forgot the break at the end of the case so the default will run also.
Ok, got it working now. Thanks for the help. Can't believe I overlooked such a minor detail.
Topic archived. No new replies allowed.