#include <iostream>
#include <string>
usingnamespace std;
class Dates {
public:
Dates();
void askDate(int);
private:
string str[];
};
Dates::Dates()
{
string s1 = "Sun"; string s2 = "Mon"; string s3 = "Tue"; string s4 = "Wed";
string s5 = "Thur"; string s6 = "Fri"; string s7 = "Say";
string str[] = {s1,s2,s3,s4,s5,s6,s7};
}
void Dates::askDate(int a)
{
cout<<"The corresponding day is "<<str[a-1]<<endl;
}
int main ()
{
char answer = 'y';
while (answer == 'y')
{
cout<<"Enter a number (between 1 - 7):";
int day;
cin>>day;
if (day<1 || day >7)
cout<<"Wrong number"<<endl;
else
{
Dates A;
A.askDate(day);
}
cout<<"Continue?";
cin>>answer;
}
system("pause");
}
The problem is on line 22, it cannot display the text stored in str[] and crashed(meesge: memory cannot be read/written).
Is it legal to declare a data memeber like the one in line 10? (i.e. an array?)