parallel array

hello, this is my first time here. I needed to write a program to display the "flower of the month" depending on your birth date. So far it works as intended accept for a run-time error i keep getting. It needs to display a error message "invalid number" when a user inputs a number <0 or >13. but when a number out side of the range is entered it puts out about 30 random lines and then displays"Unhandled exception at 0x0FE16D26 (msvcp110d.dll) in projectnamehere.exe: 0xC0000005: Access violation reading location 0x0000000F." I would really appreciate some help with this, it is due tonight. thank you

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int idx=0;
char ans='y';
string month [13] = {"","january","february","march","april","may","june","july","august","september","october","november","december"};
string flower [13] = {"","carnation","iris","daffodil","daisy","lily of the valley","rose","sunflower","gladiolus","aster","snapdragon","chrysanthemum","orchid"};
while (ans=='y' || ans=='Y')
{
system("cls");
system("color f0");
cout<<"\t\t\t******************************"<<endl;
cout<<"\t\t\t* First Last *"<<endl;
cout<<"\t\t\t* computer *"<<endl;
cout<<"\t\t\t* Flower *"<<endl;
cout<<"\t\t\t******************************"<<endl<<endl;

cout<<"Please enter the month you were born <1-12>: ";
cin>>idx;
if (idx<0||idx>13)
{
cout<<"invalid number entered: ";
}
//else
cout<<"you were born on "<<month[idx]<<" and your flower is "<<flower[idx]<<endl;
cout<<"\nwould you like to continue <Y or N>? ";
cin>>ans;
}
cout<<endl<<" THANK YOU"<<endl<<endl;
system("pause");
return 0;
}
Hey and welcome to the forum :) Please edit your post, and if you decide to post again in the future, use code tags <> http://www.cplusplus.com/articles/jEywvCM9/

The problem is the if statement. If the user enters say 22, which is out of the range 0-12. Then you will try to access the out of range element in the array in the cout statement right after the if statement. What you want is a while-loop that will keep running until the user enters something between 0-12. Here, I'll post the entire code -

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
int idx = 0;
	char ans = 'y';
	string month[13] = { "", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
	string flower[13] = { "", "carnation", "iris", "daffodil", "daisy", "lily of the valley", "rose", "sunflower", "gladiolus", "aster", "snapdragon", "chrysanthemum", "orchid" };
	while (ans == 'y' || ans == 'Y')
	{
		system("cls");
		system("color f0");
		cout << "\t\t\t******************************" << endl;
		cout << "\t\t\t* First Last *" << endl;
		cout << "\t\t\t* computer *" << endl;
		cout << "\t\t\t* Flower *" << endl;
		cout << "\t\t\t******************************" << endl << endl;

		cout << "Please enter the month you were born <1-12>: ";
		cin >> idx;
		while (idx<0 || idx>13)
		{
			cout << "invalid number entered: " << endl;
			cout << "Please enter the month you were born <1-12>: ";
			cin >> idx;
		}
		
		cout << "you were born on " << month[idx] << " and your flower is " << flower[idx] << endl;
		cout << "\nwould you like to continue <Y or N>? ";
		cin >> ans;
	}
	cout << endl << " THANK YOU" << endl << endl;
	system("pause");
	return 0;


So basically. If user enters a month that does not exist, they will be asked to asked again and again until they enter a month that does exist.
Last edited on
I recommend using the '&&' logical operator instead of the '||' for your if() statement
@Fasion. That makes no sense... There does not exist a number that is less than 0 AND bigger than 13. If you can think of one, please tell me :p

Stick to the || operator its correct here.
I think the condition should be while (idx<1 || idx>12).
Yep. What @fg109 said.
thank you very much @TarikNeaj, and everyone else! I have only started learning code a couple months ago and i'm still working out the kinks. thank again!
You're very welcome man :) Feel free to come back if you get any more problems!

And dont forget to use code tags like I if you decide to post again - http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.