if statements dictated by char

Hello,

I must be missing something pretty fundamental with if statements, but I cant put my finger on it.

The way this code sits, my first if statements:

if (dInput1=='m'||'M'||'t'||'T'||'w'||'W'||'f'||'F')

seems like its always coming up as true, because my code goes into that branch
regardless of what char you enter.

When I change this to:

if (dInput1==('m'||'M'||'t'||'T'||'w'||'W'||'f'||'F'))

it fixes that problem. But then it just always comes out as false, even if you enter the appropriate character.

Also, you may notice I originally set this up to receive two inputs. I am trying to avoid using the 2nd input if I can. Taking this in and out of the code doesnt seem to make a difference with my errors.

Much appreciated in advance! :)

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

using namespace std;
//Global Constants
//Function prototypes
//Execution 
int main(int argc, char** argv) {
//Declare variables
    float dWeek=.40,nWeek=.25,wkend=.15;
    int length, time;
    char dInput1, dInput2;
    cout<<"Please enter the time of the day your call started"<<endl;
    cout<<"in a 4 digit, 24H format. EX: 1:30PM = 1330"<<endl;
    cin>>time;
    cout<<"Enter the length of your call in whole minutes"<<endl;
    cin>>length;
    cout<<"Please enter the day of your call using"<<endl;
    cout<<"two character format. Ex. (Mo, Tu, We, Th, Fr, Sa, Su)"<<endl;
    cin>>dInput1>>dInput2;
    if (dInput1=='m'||'M'||'t'||'T'||'w'||'W'||'f'||'F')  {
        if (time>759&&time<1800) {
            cout<<"Your "<<length<<" minute call cost $";
            cout<<length*dWeek<<" at a rate of "<<dWeek<<" per minute"<<endl;
            return 0;
        }
        else {
            cout<<"Your "<<length<<" minute call cost $";
            cout<<length*nWeek<<" at a rate of "<<nWeek<<" per minute"<<endl;
            return 0;
        }
    }
    else if (dInput1==('s'||'S')) {
        cout<<"Your "<<length<<" minute call cost $";
        cout<<length*wkend<<" at a rate of "<<wkend<<" per minute"<<endl; 
        return 0;
    }
    else { 
        cout<<"Invalid input";
    }
    return 0;
}
I figured it out. I realize you need to make every possible outcome for the variable its own comparison. So instead of

(input1=='r'||'R')

it must be:

(input1=='r')||(input1=='R')


I am curious, though, is there a better way to end the program other than return 0;??? Say I just want to break out of that block to the next outer block?

Thanks!

For reference:

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
using namespace std;
//Global Constants
//Function prototypes
//Execution 
int main(int argc, char** argv) {
//Declare variables
    float dWeek=.40,nWeek=.25,wkend=.15;
    int length, time;
    char dInput1, dInput2;
    cout<<"Please enter the time of the day your call started"<<endl;
    cout<<"in a 4 digit, 24H format. EX: 1:30PM = 1330"<<endl;
    cin>>time;
    cout<<"Enter the length of your call in whole minutes"<<endl;
    cin>>length;
    cout<<"Please enter the day of your call using"<<endl;
    cout<<"two character format. Ex. (Mo, Tu, We, Th, Fr, Sa, Su)"<<endl;
    cin>>dInput1>>dInput2;
    if ((dInput1=='m')||(dInput1=='M')||(dInput1=='t')||(dInput1=='T')||
            (dInput1=='w')||(dInput1=='W')||(dInput1=='f')||(dInput1=='F'))  {
        if (time>759&&time<1800) {
            cout<<"Your "<<length<<" minute call cost $";
            cout<<length*dWeek<<" at a rate of "<<dWeek<<" per minute"<<endl;
            return 0;
        }
        else {
            cout<<"Your "<<length<<" minute call cost $";
            cout<<length*nWeek<<" at a rate of "<<nWeek<<" per minute"<<endl;
            return 0;
        }
    }
    else if ((dInput1=='s')||(dInput1=='S')) {
        cout<<"Your "<<length<<" minute call cost $";
        cout<<length*wkend<<" at a rate of "<<wkend<<" per minute"<<endl; 
        return 0;
    }
    else { 
        cout<<"Invalid input";
    }
    return 0;
}


Last edited on
Topic archived. No new replies allowed.