Hi, new here, I'm in about week 3 of my C++ class and I and we are on the decisions chapter (if, else, switch, etc.)
I was given a psuedocode algorithm to base a script from that tells you what season a given date is in.
1 2 3 4 5 6 7 8 9
|
If month is 1, 2, or 3, season = "Winter"
Else if month is 4, 5, or 6, season = "Spring"
Else if month is 7, 8, or 9, season = "Summer"
Else if month is 10, 11, or 12, season = "Fall"
If month is divisible by 3 and day >= 21
If season is "Winter", season = "Spring"
Else if season is "Spring", season = "Summer"
Else if season is "Summer", season = "Fall"
Else season = "Winter"
|
I initially had no problem figuring this out, and this is the code I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <string>
using namespace std;
int main ()
{
//declare variables
int month, day;
string season, ft_month;
//prompt for input
cout << "Month: ";
cin >> ft_month;
cout << "Day: ";
cin >> day;
//determine integer value for month based on user input
if(ft_month == "January" || ft_month == "january"){
month = 1;
} else if(ft_month == "February" || ft_month == "february"){
month = 2;
} else if(ft_month == "March" || ft_month == "March"){
month = 3;
} else if(ft_month == "April" || ft_month == "april"){
month = 4;
} else if(ft_month == "May" || ft_month == "may"){
month = 5;
} else if(ft_month == "June" || ft_month == "june"){
month = 6;
} else if(ft_month == "July" || ft_month == "july"){
month = 7;
} else if(ft_month == "August" || ft_month == "august"){
month = 8;
} else if(ft_month == "September" || ft_month == "september"){
month = 9;
} else if(ft_month == "October" || ft_month == "october"){
month = 10;
} else if(ft_month == "November" || ft_month == "november"){
month = 11;
} else if(ft_month == "December" || ft_month == "december"){
month = 12;
}
//determine season
if(month <= 3)
{
season = "Winter";
}
else if(month >=4 && month <= 6)
{
season = "Spring";
}
else if(month >=7 && month <=9)
{
season = "Summer";
}
else if(month >=10 && month <= 12)
{
season = "Fall";
}
//make correction for solstices/equinoxes
if(month % 3 == 0 && day >=21) {
if(season == "Winter")
{
season = "Spring";
}
else if(season == "Spring")
{
season == "Summer";
}
else if(season == "Summer")
{
season == "Fall";
}
else {
season = "Winter";
}
}
cout << ft_month << " " << day << " is in " << season << "!" << endl;
return 0;
}
|
Now, it works flawlessly for the most part, but for some reason, when I went to test certain date values just to test each individual elseif, I ran into problems. It gets the first set of conditionals right, but when I enter June 22 it still tells me it's in spring. Does anyone see anything in my code that might be causing this? When I tested other months that were divisible by 3 such as March, it worked fine. March 20? Winter. March 21? Spring. Thank you in advance for any help. Most of my experience is with PHP so there have been little silly mistakes I've been making with syntax and integer stuff.
EDIT: I just checked again, and it's also doing it with September 20/21. December is working as intended.