If statments

Hi,

I am into my 2nd week of my computer programming class and my professor assigned us a lab. It has to do with property tax and there can be two ways to calculate it; if you are a senior citizen or if you are a regular homeowner.

I know how to do the if statements, but all the ones I have looked online, they have a numeral if statement such as if (total > 100). How do I do this if I need the layout to go if (answer = yes) (to being a senior citizen)? Or in the same situation, if (answer = no)?

Also, the calculations for property tax is different from regular homeowners than seniors. How do I get the program to do a different calculation for each?

THANK YOU! :)
For your input, what are you storing it in? The easiest would be if you are using a string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main() {
    std::string answer;
    std::cout << "Are you are senior citizen (yes/no)? "
    std::cin >> answer;

    if (answer == "yes") {
        // do your calculations for seniors here
    }
    else if (answer == "no") {
        // do your calculations for 'normal people' here
    }
    else {
        // answer wasn't yes or no, print an error
        std::cout << "That wasn't a valid response.\n";
    }

    return 0;
}


If you are using char arrays, though, it gets a bit more complicated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstring>

int main() {
    char answer[4];
    std::cout << "Are you a senior citizen (yes/no)? ";
    std::cin.getline(answer, 4);

    if (strcmp(answer, "yes") != 0) {
        // do your calculations for senior citizens here
    }
    else if (strcmp(answer, "no") != 0) {
        // do your calculations for regular homeowners here
    }
    else {
        // invalid response, print an error
        std::cout << "That wasn't a valid response.\n";
    }

     return 0;
}
Last edited on
Topic archived. No new replies allowed.