Arrays, blank output, and crashing.

Hello, this is my first post here of what I hope to be many. I'm getting into c++ and am trying to output an array element. When the cout executes the array 'variable' is blank and I get a system error which has to force close the console.

here is my 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace std;

const int SIZE = 7;
string dayNameArray[SIZE] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

class DayOfTheWeek
{
private:

	int dayChoice; 

public:

	int setDayNum();
	
	

};

int main()
{
	int dayChoice = 999;

	DayOfTheWeek day;
	day.setDayNum();

	if(dayChoice == -1)//check for quit
	{
		cout << endl << "Have a nice day! " << endl;
		system("pause");
		return 0;
	}
	else
	{
	cout << "You entered " << dayNameArray[dayChoice] << ". " << endl;
	
	system("pause");
	return 0;
	}
}
	
int DayOfTheWeek::setDayNum()
{
	do
	{
		cout << endl << "Enter a number for the day in a week [1-7 (Enter 0 to quit)]: ";
		cin >> dayChoice;
		dayChoice = dayChoice - 1; //work-around to avoid user putting in 0 for Sunday

		if(dayChoice == -1) //-1 actually takes the place of 0 from week 1
			return dayChoice;
		else if(dayChoice > 6)
		{
			cout << endl << "Invalid day. ";
		}
		else
		{
			return dayChoice;
		}


	}while(true);

-----

The eventual goal is to take the element and add a day to it, or remove a day from it, or add X amount of days, but I would like this to work first. Once you enter a number (2 for example, which should output Monday) it just comes up "You entered " and then I get an error box and I have to close the console. Any help is appreciated.
Last edited on
the dayChoice in main and dayChoice in setDayNum are unrelated, so the one in main equals 999 and your array doesn't have 999 elements.
possible solutions:
dayChoice = day.setDayNum();

or

remove the local variable dayChoice from the main and use day.dayChoice instead (you'd have to make it public)

or

change setDayNum so that it takes an int& dayChoice.

note that only in the 2'nd solution it makes sense to have a DayOfTheWeek class.
Ok, thank you. I think solution two is what I have to go with, I have to use classes (and this program will get bigger).

When I take dayChoice out of the main() the dayChoice in my if statement errors as unidentified, even though setDayNum should be returning it?

Could you help me out with what day.dayChoice; actually does? I'm uncertain because I've only ever used that to use methods on objects. And should it be before or after day.setDayNum()?

Sorry, I'm quite new to this.
ok, I needed if(day.dayChoice == -1), I think I'm starting to get classes can objects a little more.

I ask about what day.dayChoice; does because it still works when I take that out.
Last edited on
day.dayChoice; does nothing. You misunderstood me. When I said "use day.dayChoice instead" I meant that your main should look like this:
1
2
3
4
5
6
7
8
9
10
11
int main(){
   DayOfTheWeek day;
   day.setDayNum();

   if(day.dayChoice == -1) cout << "\nHave a nice day!\n ";
   else cout << "You entered " << dayNameArray[day.dayChoice] << ". \n";

   cout << "Press Enter to quit."
   cin.get();
   return 0;
}
yes, thank you. it is working now, I will mark this solved.

thanks
thanks
thanks
I removed my solved because I've run into another problem and I'd rather not waste board space with a new thread.

Now that I have the array working, I need to pass it into two functions, one that will decrease the day (yesterday) and one to increase the day (tomorrow). I can very easily do do:

dayNameArray[day.dayChoice - 1]

but that's not acceptable, as it doesn't use a function.

I'm trying it this way:

cout << "Yesterday was " << day.nextDay(dayNameArray[day.dayChoice]) << ". " << endl;

but it gives me an error under dayNameArray that says no acceptable conversion from str to str, which is weird.

The nextDay function isn't complete yet. I'm not really sure how to do it. When I use day.dayChoice in my class identifier it gives me errors that day is unidentified.

1
2
3
4
5
string DayOfTheWeek::nextDay(string dayNameArray[])/*can't put day.dayChoice in the index cus I can't do it in the class identifier*/
{
	dayNameArray[day.dayChoice] = dayNameArray[day.dayChoice + 1]; //day also errors as undefined here
	return day.dayChoice;
}




haha obviously I'm lost......
Last edited on
You're over-complicating it.
You know that what you need is dayNameArray[day.dayChoice - 1].
dayNameArray is a global variable so you don't have to pass it anywhere. (Note: global variables are often disliked. You may want to make dayNameArray a static member of DayOfTheWeek to make it a little neater, once you've solved your other problems.)
nextDay is a member function of DayOfTheWeek so just like in setDayNum you don't need to type that "day.". It is implied.
So that leaves us with return dayNameArray[dayChoice-1];
I owe you a beer. This is great help, I'll implement these and see how it goes. Thanks again.
Topic archived. No new replies allowed.