I keep getting an error message AFTER running my program

So the purpose of my program is for it to load two arrays called Names and Days and to then display 10 random questions of the days of the month and check if the answer is correct or not. If correct display Correct and add 10 points to the total score or display Incorrect otherwise. Basically here's an example:
RUN
How many days are there in the month of March? 28
No March has 31 days.

How many days are there in the month of November? 30
Correct!

.. and so on

When I run my code, this is what I always get, what can I do to fix it?:

How many days are there in the month of June ?30
No
--------------------------------
Process exited with return value 255
Press any key to continue . . .

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
//Program to display months of the year and the number of days in each month
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
	int CorrectAns,score=0;
	const int Num_Month=12;
        string name[Num_Month]={"January","February","March",
                                 "April", "May",   "June",
			         "July", "August", "September",
			         "October", "November","December"};
  
    int days[Num_Month]={31,28,31,30,31,30,31,31,30,31,30,31};
    
	 
    
	for(int Question=1;Question<=10;Question++)
	   {
            for(int month=0;month<=Num_Month;month++)
            {
             int choice=1+rand()%12;
        	cout<<"How many days are there in the month of  "<<name[choice-1]<<" ?";
            cin>>choice;
            CorrectAns=days[choice-1];
        
     
	     if(CorrectAns==choice)
             {
               cout<<"Correct!"<<endl;
               score+=10; //Add 10 pts to score
              }
           else
           {
             cout<<"No "<<name[choice-1]<<" has "<<CorrectAns<<"days."<<endl;
           }
	         cout<<"Total score is: "<<score<<endl;
}           }
}
There might be more mistakes, but the first one I see is that you are using "choice" as an array index.
Your days[] array only has 12 indices, indexed from [0 ... 11].
If the user types 31, it tries to get the value at days[30], which is definitely not a valid array index!

Right after you do int choice=1+rand()%12;, you quickly overwrite it with the cin >> choice.
I would suggest having two variables, one to keep track of the users choice, and one to keep track of the month index.


Also, put this at the top if your main function:
srand(time(NULL));
That seeds rand()'s random number generator based on the current time so that you get different random results each time.
Last edited on
so something like this?:

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
//Program to display months of the year and the number of days in each month
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
	srand((unsigned)time(NULL));
	
	int Month,CorrectAns,score=0;
	const int Num_Month=12;
    string name[Num_Month]={"January","February","March",
                       "April", "May",   "June",
					   "July", "August", "September",
					   "October", "November","December"};
  
    int days[Num_Month]={31,28,31,30,31,30,31,31,30,31,30,31};
    
	 
    
	for(int Question=1;Question<=10;Question++)
	   {
        for(int month=0;month<=Num_Month;month++)
          {
           int choice=1+rand()%12;
        	cout<<"How many days are there in the month of  "<<name[choice-1]<<" ?";
            cin>>Month;
            CorrectAns=days[choice-1];
        
     
	     if(CorrectAns==Month)
           {
         	cout<<"Correct!"<<endl;
        	score+=10; //Add 10 pts to score
           }
           else
           {
             cout<<"No "<<name[choice-1]<<" has "<<CorrectAns<<"days."<<endl;
           }
	         cout<<"Total score is: "<<score<<endl;
}           }
}
The inner part seems to work okay now, it shouldn't crash now at least.
Although I'm assuming you don't want a 130-question long test? If so, your inner for loop (month goes in range [0, 12]) is useless, and you probably only need the outer "Question=1 -> 10" loop.
Last edited on
ok i took that out, thanks, but now i get this error:

[Error] 'time' was not declared in this scope
Topic archived. No new replies allowed.