zodiac program (array practice)

hi guys!

so i've been trying to practice with arrays.

this is for a programming class. instead of doing pseudocode, i've been doing all of my assignments in c++ to test if the program works and to see what the output will be. so i'm learning this language on my own without the help of a textbook or teacher (although he does know many programming languages)

so the assignment has 4 parts, and i'm on the last part

"prompt a user to enter a birth month and day, and continue to prompt until the day entered is in range for the month. compute the day's numeric position in the year." for example, april 13th is 103rd day of the year. "then using parallel arrays, find and display the zodiac sign for the date."

(this is using the month number instead of month name)

what i have so far is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main() {
  int m;
  int d;
  int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  string monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  string monthZodiac[] = {"Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"};
 
  cout << "Enter birth month: ";
  cin >> m;
  cout << "Enter birth day: ";
  cin >> d;

  cout << monthNames[m-1] << " has " << monthDays[m-1] << " days.";
  
}


the reason for the "month has ___ days" part is because part a, b, and c required that i include that, though with part d i'm sure i'm going to remove it because we're asking for the zodiac signs

what i thought i was going to was add an if statement under birth month, such as
1
2
if(m>1 || m<12)
  cout << "Invalid month.";


however, i'm not sure how to make it loop back and ask for the month until a valid number is entered.

because it's asking for another parallel array i assume i'm going to add another string or int but i'm a little bit confused as how to compute the day's numeric position within the year...
If you initialize your variables when you define them you could use a simple while() loop, by the way you should declare your variables close to first use, not in one big glob at the beginning of a scope and consider using meaningful variable and function names.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
    int month = 0;
    while(month < 1 || month > 12)
    {
        std::cout << "Enter birth month: ";
        std::cin >> month;
        if(!std::cin)
        {
            std::cin.clear();
            std::cin.ignore(1000,'\n');
        }
    }    


I tried to use the code you provided, and I played around with it.

I also renamed my variables, bmonth as in birthday month and bday as in birthday... day lol
I also initialized my other variables.

1
2
3
4
5
6
7
8
9
while(bmonth < 1 || bmonth > 12) {
    cout << "Enter birth month: ";
    cin >> bmonth;
    if(!cin) {
      cin.clear();
      cin.ignore(1000,'\n');
      cout << "Invalid." << endl; }

  }


This code as it is will not accept anything that is less than 1 or greater than 12, which is awesome

But it doesn't display the invalid.

I played with other while statements, if statements, using various variables.

In class I had noticed my classmate had other variables, such as

1
2
int i=0
bool verify=0


he had used verify=0 to validate cin in place of

if(!cin)

while using a while loop, with an if loop nested within.

as well as other #includes at the top of the code.

I played around with that as well, but it only led to "Invalid" being repeated indefinitely. At one point I got it to display once, but when I typed in another invalid month (such as 44) it'd display three or four times. It varied depending on what I put down.

I'm sorry if it doesn't seem like I can follow along...

I know I can use if else to display the zodiacs signs.

However I'm not sure how to get the cout to display within the while loop, as well as how to align dates with months. For example,

if I put my birth month as 2 (Feb) and I put in my birth date as 30, I don't know how I would code it to where it tells the user that it's invalid.
Last edited on
But it doesn't display the invalid.

The only time it should display "invalid" is when the stream fails because you either entered EOF or you entered a non-numeric value. Otherwise it will just loop to the beginning and ask you to enter the value again. As supplied the only way out of that loop is when you enter a valid number or a month. You will still need to validate your day as well.

if I put my birth month as 2 (Feb) and I put in my birth date as 30, I don't know how I would code it to where it tells the user that it's invalid.

You need to consider functions your friend. If the supplied while loop was placed in a function that had parameters for the high and low values you could call that function whenever you need an integer between two values.





Topic archived. No new replies allowed.