Writing to output file

I need help writing to an output file. It needs to show the names of each month and number of days. Heres my code so far.

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
#include <iostream>
using namespace std;

//function prototypes

int main()
{
	//declare array and variable
	int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	int month = 0;

	cout << "Enter month number (1 for Jan - 12 for Dec | -1 to end): ";
	cin >> month;
	while (month != -1)
	{
		if (month < 1 || month > 12)
			cout << "Invaild month number, choose from 1-12." << endl;
		else
		{
			cout << "Month " << month << " has " << days[month -1] << " days." << endl;
		}

		cout << "Enter month number (1 for Jan - 12 for Dec | -1 to end): ";
		cin >> month;
	}   //end while

	system("pause");
	return 0;
}   //end of main function 
If you are outputting to a file where is your fstream object?

http://www.cplusplus.com/reference/iostream/ofstream/

Here's some reading for how to declare and use an ofstream object to open and write to a file.
i know that i need an fstream but how do i get the output window to show the names of each month. thats what i need to do
Why not use an array of string objects. Then you can simply output the correct month name using array[month]. If you do it that way the array will need 13 elements since array[month] will never access array[0]. Alternatively, could could use array[month-1] and only declare the array with 12 elements saving a bit of space but making the code a bit more complex. Here's some more reading on the string class. They are fairly easy to use, so don't worry about them being a class if you have no idea what those are. :)

http://www.cplusplus.com/reference/string/string/
Still having a little problem. I added to 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//function prototypes

int main()
{
	ofstream outputFile;
	outputFile.open("monthNames.txt", ios::app);
	//declare array and variable
	int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	int month = 0;
	char* array[12] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
	

	cout << "Enter month number (1 for Jan - 12 for Dec | -1 to end): ";
	cin >> month;
	while (month != -1)
	{
		if (month < 1 || month > 12)
			cout << "Invaild month number, choose from 1-12." << endl;
		else
		{
			cout << "Month " << month << " has " << days[month -1] << " days." << endl;
		}

		cout << "Enter month number (1 for Jan - 12 for Dec | -1 to end): ";
		cin >> month;
	}   //end while

	outputFile.close();
	system("pause");
	return 0;
}   //end of main function 
What problem?
It still won't show the month names. Is there something I can change so it will display the names and the days?
That's because you are storing the month names in array[], but only outputting month in your else statement. If you want to output the month name entered just use array[month]
Oh I got it now. Thanks!
Topic archived. No new replies allowed.