HELP Simplifying this...

closed account (L8RkoG1T)
HELP Simplifying this, have been told i need to use an array although im new to C++ so am a little confused :(

#include <iostream>
#include <fstream>
using namespace std;


int main ()
{
int January;
int Febuary;
int March;
int April;
int May;
int June;
int July;
int August;
int September;
int October;
int November;
int December;

cout << "Enter January Temperature: ";
cin >> January;
cout << endl;

cout << "Enter Febuary Temperature: ";
cin >> Febuary;
cout << endl;

cout << "Enter March Temperature: ";
cin >> March;
cout << endl;

cout << "Enter April Temperature: ";
cin >> April;
cout << endl;

cout << "Enter May Temperature: ";
cin >> May;
cout << endl;

cout << "Enter June Temperature: ";
cin >> June;
cout << endl;

cout << "Enter July Temperature: ";
cin >> July;
cout << endl;

cout << "Enter August Temperature: ";
cin >> August;
cout << endl;

cout << "Enter September Temperature: ";
cin >> September;
cout << endl;

cout << "Enter October Temperature: ";
cin >> October;
cout << endl;

cout << "Enter November Temperature: ";
cin >> November;
cout << endl;

cout << "Enter December Temperature: ";
cin >> December;
cout << endl;

ofstream myfile;
myfile.open ("stats.txt");
myfile << January << endl;
myfile << Febuary << endl;
myfile << March << endl;
myfile << April << endl;
myfile << May << endl;
myfile << June << endl;
myfile << July << endl;
myfile << August << endl;
myfile << September << endl;
myfile << October << endl;
myfile << November << endl;
myfile << December << endl;
myfile.close();
cout << "Writen to stats.txt file" << endl;

system("pause");
return 0;
}
closed account (L8RkoG1T)
Please Help!
You can use an array in a couple of places here. I suggest something like:

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
#include <iostream> 
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ofstream;

int main () 
{ 

string month[12]={"January", "Febuary", "March", "April", "May",  "June", "July", "August", "September", "October", "November","December"};
int temperature[12];

ofstream myfile;
myfile.open ("stats.txt");

for (int i=0; i<12; ++i)
{
  cout << "Enter " << month[i] <<" Temperature: ";
cin >> temperature[i];
cout << endl;
 myfile << temperature[i] << " ";
}

myfile.close();
cout << "Writen to stats.txt file" << endl;

return 0;
}





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;

void seperateMonths (ofstream&);

int main(int argc, char *argv[])
{
    ofstream MonthFile ("Stats.txt" , ios::app);
    int Months [12] = {0};
    string MonthName [12] = {
	"January" , "February" , "March" , "April" , "May" , "June" ,
	"July" , "August" , "September" , "October" , "November" , "December"
    };
    for (int i = 0 ; i < 12 ; i++)
    {
	cout << "Please enter the temperature for " << MonthName [i] << "." << endl;
	cin >> Months [i];
    }
    seperateMonths (MonthFile);
    for (int j = 0 ; j < 12 ; j++)
    {
	MonthFile << MonthName [j] << " : Had a temperature of " << Months [j] << "." << endl;
    }
    MonthFile.close ();
    return 0;
}

void seperateMonths (ofstream& File)
{
    File << "\n---------------------------------------------------------" << endl;
    File << "---------------------------------------------------------" << endl;
    File << "-------------------Months 1- 12--------------------------\n" << endl;
}


Hope this helps.

You may want to learn more about arrays.
http://www.cplusplus.com/doc/tutorial/arrays/

Woops Moschops beat me again -_-
Last edited on
closed account (L8RkoG1T)
Thanks alot, Moschops reply has an error for me but Chipmunks works fine. Thanks alot guys :)
That's interesting; what compiler are you using? (Please, please don't say Dev-C++)
Last edited on
wow i use dev c++. whats wrong with it.it works fine for me?
This is what's wrong with it: http://cplusplus.com/articles/36vU7k9E/

There is a more recent version that I'm told fixes many issues. At the very least, it uses a recent compiler: http://orwellengine.blogspot.com/
@Moschops You use the string class but do not include it.
Cry me a river. Cut n' paste fiasco. :)
Last edited on
What happened to the "don't give straight answers," policy xD
Topic archived. No new replies allowed.