Nov 25, 2011 at 12:47am UTC
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;
}
Nov 25, 2011 at 1:51am UTC
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 Nov 25, 2011 at 1:54am UTC
Nov 25, 2011 at 2:14am UTC
Thanks alot, Moschops reply has an error for me but Chipmunks works fine. Thanks alot guys :)
Nov 26, 2011 at 12:09am UTC
That's interesting; what compiler are you using? (Please, please don't say Dev-C++)
Last edited on Nov 26, 2011 at 12:10am UTC
Nov 26, 2011 at 12:15am UTC
wow i use dev c++. whats wrong with it.it works fine for me?
Nov 26, 2011 at 1:30am UTC
@Moschops You use the string class but do not include it.
Nov 26, 2011 at 1:31am UTC
Cry me a river. Cut n' paste fiasco. :)
Last edited on Nov 26, 2011 at 1:34am UTC
Nov 26, 2011 at 1:37am UTC
What happened to the "don't give straight answers," policy xD