Problems with the string class

Hello, I've having problems with the string class.In the code below i want to create an array of strings , 7 in total at line 19. I then want to populate each of the seven arrays with a string for each day of the week(I've only put "Sunday" in for now). There are no compiler errors but i can't 'watch' the array in the debugger. Just isn't visible when back in main(). Is the string 'Sunday' being stored at all?


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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Days of the week

class dayType
{
public:
	string offsetday( int offset);
	dayType();        //constructor
		
private:	
	int currentDayindex;
	string days[7];     
};

dayType::dayType()
{
	currentDayindex =0;
	days[1]="Sunday";

}

int main(void)

{
    dayType d1;      //constructor
	system("PAUSE");
		
}
Remember that when you're using arrays they start from 0 and not 1. Try changing line 25 to days[0]="Sunday";
Why don't you implement string dayType::offsetday(int) and then call it in main like this:

1
2
3
4
5
6
int main()
{
    dayType d;
    cout << d.offsetday(0) << endl;
    cin.get();
}

to see for yourself?
Hello,

sorry i probably wasn't very clear, i didn't want to use the offsetday function until later. i just want to store the days of the week into an array of strings and not actually input them as part of the program from the user.

Is the declaration days[7] creating an array of 7 strings , such that i can then do the following

days[0] = "unused"
days[1] = "Sunday"
days[2] = "Monday" etc.

or does it simply create one array 7 characters long?. Or am i entering the text incorrectly?
Is the declaration days[7] creating an array of 7 strings , such that i can then do the following...

Yes.

EDIT: However, if you want to store 8 strings, because that is what it seems that you want, you'll have to declare it like this: string days[8];
Last edited on
Topic archived. No new replies allowed.