Array month and day of the month program

I think this is mostly right but the string month portion of the code is coming up undefined.
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
#include <iostream>
using namespace std;

int main ()
{
	char answ, y, n;
	int num;
	int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	// vv not sure what if I have this correct or not.vv
	string month[] = {January, Febuary, March, April, May, June, July, August, September, October, November, December};
   

	while(answ = y)
	{
		cout << "Enter a number between 1-12  ";
        cin >> num;
		
		if(num > 0 && num <= 12)
		{
		--num;
		cout << month[num] << " has " << days[num] << endl;
		cout << "would you like to continue?" << endl;
		cout << "y or n" << endl;
		cin >> answ; 
		}

		else
		{
			cout << "please enter a number between 1-12\n\n" << endl;
			continue;
		}
	}

  
	

  system("pause");
  return 0;
}
@majorursa

You need to enclose the months with quotes. string month[] = {"January","February","March", ... etc};

while(answ = y)
should be
while(answ == y)

or possibly

while(answ == 'y' || answ == 'Y')
thanks with that, but i still can figure out what else is wrong with it. can you help me?
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
#include <iostream>
using namespace std;

int main ()
{
	char answ, y, n;
	int num;
	int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	// vv not sure what if I have this correct or not.vv
	string month[] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   

	while(answ == y)
	{
		cout << "Enter a number between 1-12  ";
        cin >> num;
		
		if(num > 0 && num <= 12)
		{
		--num;
		cout << month[num] << " has " << days[num] << endl;
		cout << "would you like to continue?" << endl;
		cout << "y or n" << endl;
		cin >> answ; 
		}

		else
		{
			cout << "please enter a number between 1-12\n\n" << endl;
			continue;
		}
	}

  
	

  system("pause");
  return 0;
}
Last edited on
Well, for one thing, you don't have
#include <string>

For another, neither answ nor y are initialised, so comparing the two may give any result, but most likely they will be unequal.
closed account (DEUX92yv)
It looks like you might want a do-while loop instead of a standard while loop, allowing you to initialize answ the first time the loop iterates. Also, y and n don't need to be variables; you can simply compare answ with the ASCII characters.
Last edited on
Topic archived. No new replies allowed.