Sign in to follow this  
Blckknight118

Trouble with a program

Recommended Posts

Hi everyone, I was wondering if someone could tell me why this won't compile. All it does is give me build errors and then doesn't build. Forgive me for how long the code is.

#include<iostream>
using namespace std;

int main(){

int day = 0;
int month = 0;
bool loop = true;
char ans;



do {
cout << "Welcome to the Horoscope program \n";
cout << "Please enter in the month you were born \n";
cin >> day;
cout << '\n';
cout << "Now enter the number of the month you were born in \n";
cin >> month;

switch (month){
case 1:
if(day >= 20 && day <= 31){

cout << "You are an Aquarius \n";
cout << "Tomorrow is going to be a great day!";
}

else if(day >= 1 && day <= 19)
cout << "You are a Capricorn \n";
cout << "Fun things will happen to you!";

break;
case 2:
if(day >= 1 && day <= 18){
cout << "You are an Aquarius \n";
cout << "Tomorrow is going to be a great day!";

}

else if(day >= 19 && day <= 28)
cout << "You are a Pisces \n";
cout << "You are going to have a great surprise soon!";

break;
case 3:
if(day >= 1 && day <= 20){
cout << "You are a Pisces \n";
cout << "You are going to have a great surprise soon!";

}

else if(day >= 21 && day <= 31)
cout << "You are an Aries \n";
cout << "You will overcome a great obstacle!";

break;
case 4:
if(day >= 1 && day <= 19){
cout << "You are an Aries \n";
cout << "You will overcome a great obstacle!";

}

else if( day >= 20 && day <= 30)
cout << "You are a Taurus \n";
cout << "You will discover an awesome treasure!";

break;
case 5:
if(day >= 1 && day <= 20){
cout << "You are a Taurus \n";
cout << "You will discover an awesome treasure!";

}

else if(day >= 21 && day <= 31)
cout << "You are a Gemini \n";
cout << "You will find your true love soon!";

break;
case 6:
if(day >= 1 && day <= 21){
cout << "You are a Gemini \n";
cout << "You will find your true love soon!";

}

else if(day >= 22 && day <= 30)
cout << "You are a Cancer \n";
cout << "One day, you will be a great leader!";

break;
case 7:
if(day >= 1 && day <= 22){
cout << "You are a Cancer \n";
cout << "One day, you will be a great leader!";

}

else if(day >= 23 && day <= 31)
cout << "You are a Leo \n";
cout << "You will attract great people!";

break;
case 8:
if(day >= 1 && day <= 22){
cout << "You are a Leo \n";
cout << "You will attract great people!";

}

else if(day >= 23 && day <= 31)
cout << "You are a Virgo \n";
cout << "You will become a world class athlete!";

break;
case 9:
if(day >= 1 && day <= 22){
cout << "You are a Virgo \n";
cout << "You will become a world class athlete!";

}

else if(day >= 23 && day <= 30)
cout << "You are a Libra \n";
cout << "You will stop a terrible tragedy!";

break;
case 10:
if(day >= 1 && day <= 22){
cout << "You are a Libra \n";
cout << "You will stop a terrible tragedy!";

}

else if(day >= 23 && day <= 31)
cout << "You are a Scorpio \n";
cout << "Don't take the bus today!";

break;
case 11:
if(day >= 1 && day <= 21){
cout << "You are a Scorpio \n";
cout << "Don't take the bus today!";

}

else if(day >= 22 && day <= 30)
cout << "You are a Sagittarius \n";
cout << "You are going to create the greatest invention ever!";

break;
case 12:
if(day >= 1 && day <= 21){
cout << "You are a Sagittarius \n";
cout << "You are going to create the greatest invention ever!";

}

else if(day >= 22 && day <= 31)
cout << "You are a Capricorn \n";
cout << "Fun things will happen to you!";

break;

default: cout << "I don't understand";

}

cout << '\n';
cout << "Do you want to do another Horoscope?";
cout << '\n';
cout << "(y/n)";
cin >> ans;



}

while(loop == true && ans != 'n' && ans != 'N');
cout << '\n';
cout << "Goodbye!";

return 0;

}

Share this post


Link to post
Share on other sites
Might be the compiler you are using, using VS2010 Pro it compiled and ran fine for me. Which IDE are you using to write your programs?

EDIT** Also source tags help for posting longer code.

Share this post


Link to post
Share on other sites
Quote:
Original post by wicked357
Might be the compiler you are using, using VS2010 Pro it compiled and ran fine for me. Which IDE are you using to write your programs?

EDIT** Also source tags help for posting longer code.


I am using Visual Studio 2010, I don't know why it's not compiling for me.

Share this post


Link to post
Share on other sites
Quote:
Original post by BjsAust
Quote:
Original post by Blckknight118
All it does is give me build errors and then doesn't build.


Perhaps post the first few build errors?


Never mind I got it to work, but I have another issue. I enter my birthday as May 23 and I get no fortune. Also, Some of theme are getting double fortunes which I also don't understand.

Share this post


Link to post
Share on other sites
Quote:
Original post by BjsAust
Why not put a bunch of cout's in there to validate your values so you can see what the program is seeing?


I don't understand what you mean.....
Also, I'm still getting double prophecies and I don't know why.

Example: Enter in June 8 I get
You will find your true love!One day you will be a great leader!

Share this post


Link to post
Share on other sites
I'm years past doing C but you'll get what I mean. For instance if you put:


do {
cout << "Welcome to the Horoscope program \n";
cout << "Please enter in the month you were born \n";
cin >> day;
cout << '\n';
cout << "Now enter the number of the month you were born in \n";
cin >> month;
cout << "Entered day: " + day + " and month: " + month + " \n";
switch (month){



Then you know exactly what is being evaluated by your switch statement, rather than just assuming you know. Do the same kind of stuff on the way through the code to work out the logic of what its actually doing, instead of what you think it should be doing.

You can wrap them in #debug type stuff, or just delete/comment them out once its working.

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
Also, I'm still getting double prophecies and I don't know why.

Example: Enter in June 8 I get
You will find your true love!One day you will be a great leader!


You dont break inside your if statement, try using braces around your else logic to help see what the programs doing.

Share this post


Link to post
Share on other sites
Quote:
Original post by BjsAust
Quote:
Original post by Blckknight118
Also, I'm still getting double prophecies and I don't know why.

Example: Enter in June 8 I get
You will find your true love!One day you will be a great leader!


You dont break inside your if statement, try using braces around your else logic to help see what the programs doing.


Thank you very much

Share this post


Link to post
Share on other sites
The else if clause doesn't have enclosing braces, so only one of the two cout statements is conditional; the second cout is not in the else if, and is applied every time. Here's the code with the spaces adjusted to make it more visible

      case 7:
if(day >= 1 && day <= 22){
cout << "You are a Cancer \n";
cout << "One day, you will be a great leader!";

}
else if(day >= 23 && day <= 31) // no braces, so only the next line counts
cout << "You are a Leo \n"; // end of else if statement
cout << "You will attract great people!"; // executed every time
break;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this