A problem

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
#include <iostream>
#include <string>
using namespace 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?)

Thank you.
Line 10 - str is private member of class Dates.
Line 17 - str is local array for Dates sconstructor.

Do you understand the difference?
Oh. I got it. But how should I rewrite so that the string array in Line 17 using the private data member, instead of a local array?

Thank you.
1
2
private:
    string str[7];


and

1
2
3
4
5
6
7
8
9
10
Dates::Dates()
{
    str[0] = "Sun";
    str[1] = "Mon";
    str[2] = "Tue";
    str[3] = "Wed";
    str[4] = "Thur";
    str[5] = "Fri";
    str[6] = "Say";
}
Topic archived. No new replies allowed.