hi,
could someone help me fix the code for the following question:
I need to create a program that determines the animal corresponding to an input year of birth.
Assume that the input year is 1900 or later.
The 12-year
animal cycle is rat, ox, tiger, rabbit, dragon, snake, horse, ram (or goat), monkey, rooster,
dog, and boar

#include <iostream>// requires for keyboard and screen I/O
#include <conio.h>

using namespace std;

void main ()

{
int birthYear = 0;

 // used to determine the animal year
cout << " The 12-year animal cycle is rat, ox, tiger, rabbit, dragon, snake, horse, ram (or goat), monkey, rooster, dog, and boar." << endl;
cout << "Enter your year of birth and I will tell you your corresponding animal from the Chinese calendar " << endl;
cin >> birthYear;

while ((birthYear == 1900)|| (birthYear += 1900))
	{cout << "The corresponding animal from the Chinese calendar is: Rat" << endl;
	cin >> birthYear;}
while ((birthYear == 1901)|| (birthYear += 1901)) 
	{cout << "The corresponding animal from the Chinese calendar is: Ox" << endl;
	cin >> birthYear;}
while ((birthYear == 1902)|| (birthYear += 1902)) 
	{cout << "The corresponding animal from the Chinese calendar is: Tiger" << endl;
	cin >> birthYear;}
while ((birthYear == 1903)|| (birthYear += 1903))
	{cout << "The corresponding animal from the Chinese calendar is: Rabbit" << endl;
	cin >> birthYear;}
while ((birthYear == 1904)|| (birthYear += 1904))  
	{cout << "The corresponding animal from the Chinese calendar is: Dragon" << endl;
	cin >> birthYear;}
while ((birthYear == 1905)|| (birthYear += 1905))  
	{cout << "The corresponding animal from the Chinese calendar is: Snake" << endl;
	cin >> birthYear;}
while ((birthYear == 1906)|| (birthYear += 1906))
	{cout << "The corresponding animal from the Chinese calendar is: Horse" << endl;
	cin >> birthYear;}
while ((birthYear == 1907)|| (birthYear += 1907))  
	{cout << "The corresponding animal from the Chinese calendar is: Ram" << endl;
	cin >> birthYear;}
while ((birthYear == 1908)|| (birthYear += 1908))  
	{cout << "The corresponding animal from the Chinese calendar is: Monkey" << endl;
	cin >> birthYear;}
while ((birthYear == 1909)|| (birthYear += 1909))
	{cout << "The corresponding animal from the Chinese calendar is: Roster" << endl;
	cin >> birthYear;}
while ((birthYear == 1910)|| (birthYear += 1910))  
	{cout << "The corresponding animal from the Chinese calendar is: Dog" << endl;
	cin >> birthYear;}
while ((birthYear == 1911)|| (birthYear += 1911))  
	{cout << "The corresponding animal from the Chinese calendar is: Boar" << endl;
	cin >> birthYear;} 

// output results to screen
cout << "The corresponding animal from the Chinese calendar is: " << endl;
getch(); // holding the screen
} // end main

To do this you can use the following code:

#include <iostream>

char* animals[12] = {"rat", "ox", "tiger", "rabbit", "dragon", "snake",
		     "horse", "goat", "monkey", "rooster", "dog", "boar"};

int main() {
  int year = 0;
  std::cout << "Enter your birth year: ";
  std::cin >> year;
  std::cout << "Your animal is " << animals[(year-4) % 12] << std::endl;
  return 0;
}

Since 1900 % 12 = 4 and we want 1900 to be rat we have to subtract 4 from the year.. or change the animal array. This is a typical case where modulus should be used.

Comments
Please don't give away a solution (especially not one that the OP can hand in verbatim)

I got this point but how can I make the input range valid from 1900 or later
(i.e. no less than 1900)

You can add a simple check

if (year < 1900) {
  cout << "Invalid input" << endl;
}

Is this what you mean?

It still no showing the right output

#include <iostream>// requires for keyboard and screen I/O
#include <conio.h>

using namespace std;

void main ()

{
int birthYear = 0;
int year;


 // used to determine the animal year
cout << " The 12-year animal cycle is rat\n ox\n tiger\n rabbit\n dragon\n snake\n horse\n ram (or goat)\n monkey\n rooster\n dog\n and boar." << endl;
cout << "What is your year of birth? " << endl;
cin >> birthYear;

while ((birthYear >= 1900) || (birthYear <= 9999))
{
	year = birthYear % 12;
	
// output results to screen
	switch (year)
	{
case 4:
	cout << "The corresponding animal year is Rat" << endl;
	cin >> birthYear;
break;
case 5: 
	cout << "The corresponding animal year is Ox" << endl;
	cin >> birthYear;
break;
case 6:
	cout << "The corresponding animal year is Tiger" << endl;
	cin >> birthYear;
break;
case 7:
	cout << "The corresponding animal year is Rabbit" << endl;
	cin >> birthYear;
break;
case 8:
	cout << "The corresponding animal year is Dragon" << endl;
	cin >> birthYear;
break;
case 9:
	cout << "The corresponding animal year is Snake" << endl;
	cin >> birthYear;
break;
case 10:
	cout << "The corresponding animal year is Horse" << endl;
	cin >> birthYear;
break;
case 11:
	cout << "The corresponding animal year is Ram" << endl;
	cin >> birthYear;
break;
case 12:
	cout << "The corresponding animal year is Monkey" << endl;
	cin >> birthYear;
break;
case 1:
	cout << "The corresponding animal year is Roaster" << endl;
	cin >> birthYear;
break;
case 2:
	cout << "The corresponding animal year is Dog" << endl;
	cin >> birthYear;
break;
case 3:
	cout << "The corresponding animal year is Boar" << endl;
	cin >> birthYear;
break;
	}


	if (year < 1900) {
  cout << "Invalid input" << endl;
}

}

getch(); // holding the screen
} // end main

The modulus operator calculates the remainder of a division, in your case with 12. This will result in a value between 0 to 11 (not 1 to 12). Your while condition will always be true, I'm guessing this is not what you want. Why are you using a while loop? I do not understand what you want your application to do? The 'input' check (year < 1900) will not work since year is not the actual year.. (year = birthYear % 12) and as I have said, this will be a value between 0 to 11.

This article has been dead for over six months. Start a new discussion instead.