Help. Boolian with chars?

I am teaching myself here and I am working out of a textbook that asks me to create a program that asks for 2 numbers and then takes input of a s d or m to do some basic math. My compiler however says I have an error with the use of variables that are undeclared when recognizing my input for the char variable I have created.

The text book hasn't even spoken to me about using char variables in Boolean expressions and is mostly trying to teach me how to loop functions which I think I have a good grip on. Maybe I am being an over-achiever but I don't know a simpler way to take a non integer input here to translate it for my loop. and I'm trying not to learn things out of order from my materials.

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
#include<iostream.h>
#include<conio.h>

int main()
{
    double first,second;
    char math;
    
    cout<<"Please enter the first number: ";
    cin>>first;
    cout<<"Thank you! You have entered the number "<<first<<"."<<endl;
    
    cout<<"Please enter the second number: ";
    cin>>second;
    cout<<"Thank you! You have entered the number "<<second<<"."<<endl;
    
    cout<<"Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide these numbers? ";
    cin>>math;
    // my issue is here i believe...
    while(math != 'a' || math != 'd' || math != 's' || math != 'm')

    {
    cout<<"Invalid input.  Please select a,s,d, or m: ";
    cin>>math;
    }      


    if(math==a)
        cout<<first<<" + "<<second<<" = "<<first+second<<endl;
    if(math==d)
        cout<<first<<" / "<<second<<" = "<<first/second<<endl;
    if(math==s)
        cout<<first<<" - "<<second<<" = "<<first-second<<endl;
    if(math==m)
        cout<<first<<" * "<<second<<" = "<<first*second<<endl;
                 

getch();
} 
1
2
3
4
5
6
7
8
if(math==a)
        cout<<first<<" + "<<second<<" = "<<first+second<<endl;
    if(math==d)
        cout<<first<<" / "<<second<<" = "<<first/second<<endl;
    if(math==s)
        cout<<first<<" - "<<second<<" = "<<first-second<<endl;
    if(math==m)
        cout<<first<<" * "<<second<<" = "<<first*second<<endl;


I believe this is where your problem lies. You need to ' ' around each letter that you are comparing math to.
Last edited on
Hello, Kilrothy, and welcome to these forums. I, too am learning out of a textbook with no teacher other than the old pee-brain, and I know how it can be when you run into a problem. It seems that your problem is in this portion of code:
1
2
3
4
5
6
7
8
if(math==a)
        cout<<first<<" + "<<second<<" = "<<first+second<<endl;
    if(math==d)
        cout<<first<<" / "<<second<<" = "<<first/second<<endl;
    if(math==s)
        cout<<first<<" - "<<second<<" = "<<first-second<<endl;
    if(math==m)
        cout<<first<<" * "<<second<<" = "<<first*second<<endl;

The undeclared variables are d, s, and m. The compiler thinks that those letters are Variables, rather than characters (which I'm assuming is what you want.) If you want to compare a char variable to a character, use apostrophes:
1
2
3
4
5
6
7
8
if(math=='a')
        cout<<first<<" + "<<second<<" = "<<first+second<<endl;
    if(math=='d')
        cout<<first<<" / "<<second<<" = "<<first/second<<endl;
    if(math=='s')
        cout<<first<<" - "<<second<<" = "<<first-second<<endl;
    if(math=='m')
        cout<<first<<" * "<<second<<" = "<<first*second<<endl;

also, it's just a habit that I have, but I normally would put else if in place of the last 3 if statements. It makes it a bit easier to understand, in my opinion. Just my style.
Wow, I knew I wasn't 100% today, THANK YOU! I swore my compiler was saying all errors on the same line ( I don't seem to have numbering on my source code view I'm sure it's a setting I haven't bothered to find.) Total Homer Simpson moment there!

for anyone that googles their way here or is curious as to my final code for my goal here: I did have one other error that my book even warned me about during the lesson: when using a "while" statement it is easy to mix up || and &&.


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
#include<iostream.h>
#include<conio.h>

int main()
{
    double first,second;
    char math;
    
    cout<<"Please enter the first number: ";
    cin>>first;
    cout<<"Thank you! You have entered the number "<<first<<"."<<endl;
    
    cout<<"Please enter the second number: ";
    cin>>second;
    cout<<"Thank you! You have entered the number "<<second<<"."<<endl;
    
    cout<<"Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide these numbers? ";
    cin>>math;
    // my issue is here i believe...
    while(math != 'a' && math != 'd' && math != 's' && math != 'm')

    {
    cout<<"Invalid input.  Please select a,s,d, or m: ";
    cin>>math;
    }      


    if(math == 'a')
        cout<<first<<" + "<<second<<" = "<<first+second<<endl;
    if(math == 'd')
        cout<<first<<" / "<<second<<" = "<<first/second<<endl;
    if(math == 's')
        cout<<first<<" - "<<second<<" = "<<first-second<<endl;
    if(math == 'm')
        cout<<first<<" * "<<second<<" = "<<first*second<<endl;
                 

getch();
}   
    
Last edited on
Topic archived. No new replies allowed.