Chinese Zodiac Program

So obviously this seems like such a simple program, but I am absolutely blanking on how to set up the default value so that if a year is entered outside of the range (1936-1955) it gives an error message to the user. Here is my program, right now it still assigns a zodiac sign to any number entered.

//HG
//COSC 1436
//Test 1
//Create an application that will determine a user’s animal sign and personality traits according to the Chinese
//zodiac. Animal signs are assigned based on a person’s year of birth. First ask the user for their year of birth.
//Do not allow year of births lower than 1936or greater than 2055
//.Based on the year provided, calculatethe remainder by first subtracting the birth year by 1936 and then,
//mod the difference by 12.
//Example:(birthyear–1936) % 12 ....Using the remainder, display the appropriate animal sign and personality trait(s).
//Hint: use a switch decision structure

#include <iostream>

using namespace std;

int main()

{
//Declare Variables
//Ask user for their birth year

cout << "Please enter your birth year: ";
int year;
cin >> year;

//Calculate user's zodiac and write program to display the proper sign

switch ((year - 1936) % 12)
{
default:(year > 1936 || year < 2055);
cout << "you entered an invalid year, please try again!" << endl;
break;

case 0:
cout << "Your chinese zodiac sign is the rat! " << endl;
cout << "Rats are very popular!" << endl;
break;

case 1:
cout << "Your chinese zodiac sign is the ox! " << endl;
cout << "Oxes are dependable and calm!" << endl;
break;

case 2:
cout << "Your chinese zodiac sign is the tiger! " << endl;
cout << "Tigers are brave and respected!" << endl;
break;

case 3:
cout << "Your chinese zodiac sign is the rabbit! " << endl;
cout << "Rabits are nice to be around!" << endl;
break;

case 4:
cout << "Your chinese zodiac sign is the dragon! " << endl;
cout << "Dragons are known for their good health and having lots of energy!" << endl;
break;

case 5:
cout << "Your chinese zodiac sign is the snake! " << endl;
cout << "Snakes are good with money!" << endl;
break;

case 6:
cout << "Your chinese zodiac sign is the horse! " << endl;
cout << "Horses are popular, cheerful, and quick to compliment others!" << endl;
break;

case 7:
cout << "Your chinese zodiac sign is the goat! " << endl;
cout << "Goats are known for being great artists!" << endl;
break;

case 8:
cout << "Your chinese zodiac sign is the monkey! " << endl;
cout << "Monkeys are very funny and good problem solvers!" << endl;
break;

case 9:
cout << "Your chinese zodiac sign is the rooster! " << endl;
cout << "Roosters are talented and hard working!" << endl;
break;

case 10:
cout << "Your chinese zodiac sign is the dog! " << endl;
cout << "Dogs are very loyal and can keep a secret!" << endl;
break;

case 11:
cout << "Your chinese zodiac sign is the pig! " << endl;
cout << "Pigs are good students, honest and brave!" << endl;
break;


}

system("pause");
return 0;
}
Last edited on
The default case of a switch can't take any sort of condition (like an if). But you shouldn't use the default case of the switch anyway. Instead, put a loop around the part of your code that reads in the year:
1
2
3
4
5
6
7
8
int year;
while (true) {
    cout << "Please enter your birth year: ";
    cin >> year;
    if (year >= 1936 && year <= 2055)
        break;
    cout << "The year must be from 1936 to 2055.\n";
}

Last edited on
@tpb
So I added that in and now it sends the error message when the value is out of range, but if I put a value in that is in range, it outputs no message at all now...


int main()

{
//Declare Variables
//Ask user for their birth year


int year;


while (true) {
cout << "Please enter your birth year: ";
cin >> year;
if (year >= 1936 && year <= 2055)
break;
cout << "The year must be from 1936 to 2055, please try again!" << endl;

cin >> year;


//Calculate user's zodiac and write program to display the proper sign

switch ((year - 1936) % 12)
{

case 0:
cout << "Your chinese zodiac sign is the rat! " << endl;
cout << "Rats are very popular!" << endl;
break;

You missed a bit. You didn't copy all of tpb's suggested code. You missed a small but important piece.
You didn't do it right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    int year;
    while (true) {
        cout << "Please enter your birth year: ";
        cin >> year;
        if (year >= 1936 && year <= 2055)
            break;
        cout << "The year must be from 1936 to 2055, please try again!\n";
    }

    //Calculate user's zodiac and write program to display the proper sign
    switch ((year - 1936) % 12)
    {
    case 0:
        cout << "Your chinese zodiac sign is the rat! " << endl;
        cout << "Rats are very popular!" << endl;
        break;


And in the future remember to use code tags when posting code to preserve indentation:
[code]
your code here
[/code]
Last edited on
I added \n thinking I was flushing out the rest of the options with endl.... but I am still having the same issue. Do I need to use \n throughout the whole program to have to run smoothly?

And thank you! I didn't know that was a thing. Adding more to my tool box!
I figured it out! Thanks ya'll!
Topic archived. No new replies allowed.