Here is my code. HOw can I stop it from printing two birthday\'s when I don\'t w
ID: 3531413 • Letter: H
Question
Here is my code. HOw can I stop it from printing two birthday's when I don't want it to? Is there too many ifs? SHould one of them be something else? What is wrong with my do while loop and why is it not looping correctly?
#include <iostream>
using namespace std;
int main()
{
char ans;
cout << " Wanna know your horoscope sign? Enter y for yes: ";
cin>> ans;
do
{
int month, day;
cout << "Enter your birth month: ";
cin >> month;
cout << "Enter your birthday:";
cin >> day;
switch (month)
{
case 1:
if (day<=20)
cout << " Your sign is Capricorn: The future is Knar! ";
if (day>=17 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs! Your sign is also Aquarius: Looks like you need some coffee!";
else if ((day<=31))
cout << " Your sign is Aquarius: Looks like you need some coffee!";
else
cout << "This is not a valid birthday";
break;
case 2:
if (day<=18)
cout << "Your sign is Aquarius: Looks like you need some coffee!";
if (day>=16 && day<=21)
cout<< " Your birthday is on a cusp! You have 2 signs! Your sign is also Pices: Your plans never fall through.";
else if ((day<=29))
cout << "Your sign is Pices: Your plans never fall through. ";
else
cout << "This is not a valid birthday";
break;
case 3:
if (day<=20)
cout << "Your sign is Pices: Your plans never fall through. ";
if (day>=17 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Aries: Like Nike said, Just do it! ";
else if (day<=31)
cout << "Your sign is Aries: Like Nike said, Just do it!";
else
cout << "This is not a valid birthday";
break;
case 4:
if (day<=19)
cout << "Your sign is Aries: Like Nike said, Just do it!";
if (day>=17 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Taurus: You are feeling a little lazy. ";
else if (day<=31)
cout << "Your sign is Taurus: You are feeling a little lazy. ";
else
cout << "This is not a valid birthday";
break;
case 5:
if (day<=20)
cout << "Your sign is Taurus: You are feeling a little lazy. ";
if (day>=18 && day<=23)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Gemini: You can't get what you want if you don't make some changes today.";
else if (day<=31)
cout << "Your sign is Gemini: You can't get what you want if you don't make some changes today.";
else
cout << "This is not a valid birthday";
break;
case 6:
if (day<=21)
cout << "Your sign is Gemini: You can't get what you want if you don't make some changes today.";
if (day>=19 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Cancer: Get some fresh air!";
else if (day<=31)
cout << "Your sign is Cancer: Get some fresh air!";
else
cout << "This is not a valid birthday";
break;
case 7:
if ((day<=22))
cout << "Your sign is Cancer: Get some fresh air!";
if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Leo: You are an amazing and attractive person.";
else if (day<=31)
cout << "Your sign is Leo: You are an amazing and attractive person.";
else
cout << "This is not a valid birthday";
break;
case 8:
if (day<=22)
cout << "Your sign is Leo: You are an amazing and attractive person";
if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Virgo: If you try hard today, you will get what you need.";
else if (day<=31)
cout << "Your sign is Virgo: If you try hard today, you will get what you need.";
else
cout << "This is not a valid birthday";
break;
case 9:
if (day<=22)
cout << "Your sign is Virgo: If you try hard today, you will get what you need.";
if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Libra: What a great week for you! Why don't you go buy a lottery ticket!!";
else if (day<=31)
cout << "Your sign is Libra: What a great week for you! Why don't you go buy a lottery ticket!!!";
else
cout << "This is not a valid birthday";
break;
case 10:
if (day<=22)
cout << "Your sign is Libra: What a great week for you! Why don't you go buy a lottery ticket!!!";
if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Scorpio: UH OH! Be careful today. Bad luck is in the air!";
else if (day<=31)
cout << "Your sign is Scorpio: UH OH! Be careful today. Bad luck is in the air!";
else
cout << "This is not a valid birthday";
break;
case 11:
if (day<=21)
cout << "Your sign is Scorpio: UH OH! Be careful today. Bad luck is in the air!";
if (day>=19 && day<=23)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Sagittarius: Take some ristks today!";
else if (day<=31)
cout << "Your sign is Sagittarius: Take some ristks today!";
else
{cout << "This is not a valid birthday";
break;
case 12:
if (day<=21)
cout << "Your sign is Sagittarius: Take some ristks today!";
if (day>=18 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Capricorn: The future is Knar";
else if (day<=31)
cout << "Your sign is Capricorn: The future is Knar";
else
cout << "This is not a valid birthday";
break;
default:
cout << "This is not a valid birth month!";
}
}
while ((ans=='y') || (ans=='Y'));
cout<< "Okay, bye!";
return 0; }
Explanation / Answer
The use of only 'if' in place of 'else if' in the nested if else statements inside the cases was the problem. The following is the corrected code:
#include<iostream.h>
#include<stdio.h>
using namespace std;
int main()
{
char ans;
cout << " Wanna know your horoscope sign? Enter y for yes: ";
cin>> ans;
do
{
int month, day;
cout<<" Enter your birth month: ";
cin>>month;
cout<<"Enter your birthday:";
cin>>day;
switch(month)
{
case 1:
if(day<=20)
cout<<" Your sign is Capricorn: The future is Knar! ";
else if(day>=17 && day<=22)
cout<<" Your birthday is on a cusp! You have 2 signs! Your sign is also Aquarius: Looks like you need some coffee!";
else if((day<=31))
cout<<" Your sign is Aquarius: Looks like you need some coffee!";
else
cout<<"This is not a valid birthday";
break;
case 2:
if(day<=18)
cout<<"Your sign is Aquarius: Looks like you need some coffee!";
else if(day>=16 && day<=21)
cout<<" Your birthday is on a cusp! You have 2 signs! Your sign is also Pices: Your plans never fall through.";
else if((day<=29))
cout<<"Your sign is Pices: Your plans never fall through. ";
else
cout << "This is not a valid birthday";
break;
case 3:
if(day<=20)
cout << "Your sign is Pices: Your plans never fall through. ";
else if(day>=17 && day<=22)
cout<<" Your birthday is on a cusp! You have 2 signs!Your sign is also Aries: Like Nike said, Just do it! ";
else if(day<=31)
cout << "Your sign is Aries: Like Nike said, Just do it!";
else
cout << "This is not a valid birthday";
break;
case 4:
if(day<=19)
cout<<"Your sign is Aries: Like Nike said, Just do it!";
if(day>=17 && day<=22)
cout<<" Your birthday is on a cusp! You have 2 signs!Your sign is also Taurus: You are feeling a little lazy. ";
else if(day<=31)
cout<< "Your sign is Taurus: You are feeling a little lazy. ";
else
cout << "This is not a valid birthday";
break;
case 5:
if (day<=20)
cout << "Your sign is Taurus: You are feeling a little lazy. ";
else if (day>=18 && day<=23)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Gemini: You can't get what you want if you don't make some changes today.";
else if (day<=31)
cout << "Your sign is Gemini: You can't get what you want if you don't make some changes today.";
else
cout << "This is not a valid birthday";
break;
case 6:
if (day<=21)
cout << "Your sign is Gemini: You can't get what you want if you don't make some changes today.";
else if (day>=19 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Cancer: Get some fresh air!";
else if (day<=31)
cout << "Your sign is Cancer: Get some fresh air!";
else
cout << "This is not a valid birthday";
break;
case 7:
if ((day<=22))
cout << "Your sign is Cancer: Get some fresh air!";
else if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Leo: You are an amazing and attractive person.";
else if (day<=31)
cout << "Your sign is Leo: You are an amazing and attractive person.";
else
cout << "This is not a valid birthday";
break;
case 8:
if (day<=22)
cout << "Your sign is Leo: You are an amazing and attractive person";
else if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Virgo: If you try hard today, you will get what you need.";
else if (day<=31)
cout << "Your sign is Virgo: If you try hard today, you will get what you need.";
else
cout << "This is not a valid birthday";
break;
case 9:
if (day<=22)
cout << "Your sign is Virgo: If you try hard today, you will get what you need.";
else if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Libra: What a great week for you! Why don't you go buy a lottery ticket!!";
else if (day<=31)
cout << "Your sign is Libra: What a great week for you! Why don't you go buy a lottery ticket!!!";
else
cout << "This is not a valid birthday";
break;
case 10:
if (day<=22)
cout << "Your sign is Libra: What a great week for you! Why don't you go buy a lottery ticket!!!";
else if (day>=20 && day<=24)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Scorpio: UH OH! Be careful today. Bad luck is in the air!";
else if (day<=31)
cout << "Your sign is Scorpio: UH OH! Be careful today. Bad luck is in the air!";
else
cout << "This is not a valid birthday";
break;
case 11:
if (day<=21)
cout << "Your sign is Scorpio: UH OH! Be careful today. Bad luck is in the air!";
else if (day>=19 && day<=23)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Sagittarius: Take some ristks today!";
else if (day<=31)
cout << "Your sign is Sagittarius: Take some ristks today!";
else
cout << "This is not a valid birthday";
break;
case 12:
if (day<=21)
cout << "Your sign is Sagittarius: Take some ristks today!";
else if (day>=18 && day<=22)
cout<< " Your birthday is on a cusp! You have 2 signs!Your sign is also Capricorn: The future is Knar";
else if (day<=31)
cout << "Your sign is Capricorn: The future is Knar";
else
cout << "This is not a valid birthday";
break;
default:
cout << "This is not a valid birth month!";
}
}while ((ans=='y') || (ans=='Y'));
cout<< "Okay, bye!";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.