-
November 16th, 2010, 05:40 PM
#1
Console to input/output file
I need some help on what I'm working on right now I'm not asking for an answer maybe some help or a push in the right direction. Here is what I'm asked to do
"Convert your program to run from an input file instead of console input." I'm suppose to have a .txt file thats suppose to have numbers for the average rainfall and input it into what I have so far. I really don't understand Inputs/Outputs of text files and all that I tried to look up help from sites with guides that didnt really help. Can someone please help me, here is what I have so far:
#include <iostream>
#include <fstream>
#include <cstdlib>
using std::ifstream;
using std:fstream;
using std::cout;
using std::endl;
void turnToMonth(int month);
int main()
{
ifstream inStream;
ofstream outStream;
double rainfall[12];
double averages[12];
int currentMonth;
char tableOrGraph;
char yesOrNo;
inStream.open("currentrainfall.txt");
inStream.open("previousfainfall.txt");
outStream.open("outfile.txt");
cout << "Please enter average rainfall for each month" << endl;
for (int i=0; i<12; i++) {
turnToMonth(i);
cout << ": ";
cin >> averages[i]; }
cout << "What is the number of the current month? Jan=1, Feb=2, etc." << endl;
cin >> currentMonth;
cout << "Please enter the rainfall for each month in the previous year" << endl;
int count = 0;
for (int month=currentMonth-1; count < 12; month=(month+1)%12, count++) {
turnToMonth(month);
cout << ": ";
cin >> rainfall[month]; }
cout << "\nWould you like to see a table of the results\n"
<< "or a graph? Type 't' for the table or 'g' for the graph.";
cin >> tableOrGraph;
do {
if (tableOrGraph == 't') {
cout << "Rainfall table\n\n"
<< "Month\t\tRainfall (prev. 2 months)\tDeviation\n";
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++) {
turnToMonth(month);
cout << "\t\t" << rainfall[month] << "\t\t\t";
if (rainfall[month] > averages[i])
else if (rainfall[month] < averages[i])
cout << averages[i] - rainfall[month] << endl;
else
cout << "0\n"; } }
else if (tableOrGraph == 'g') {
cout << "\nRainfall graph\n\n"
<< "Month\n";
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++) {
cout << endl;
turnToMonth(month);
cout << "\nAverage rainfall: ";
for (i; averages[i] > 0; averages[i]--)
cout << "*";
cout << "\nActual rainfall: ";
for (i; rainfall[month] > 0; rainfall[month]--)
cout << "*"; } }
cout << "\n" << "Would you like to see another table or graph? 'y' for yes and 'n' for no.";
cin >> yesOrNo; }
while (yesOrNo == 'y');
return 0; }
void turnToMonth(int month) {
switch(month) {
case 0:
cout << "Jan";
break;
case 1:
cout << "Feb";
break;
case 2:
cout << "March";
break;
case 3:
cout << "April";
break;
case 4:
cout << "May";
break;
case 5:
cout << "June";
break;
case 6:
cout << "July";
break;
case 7:
cout << "Aug";
break;
case 8:
cout << "Sept";
break;
case 9:
cout << "Oct";
break;
case 10:
cout << "Nov";
break;
case 11:
cout << "Dec";
break; } }
-
November 16th, 2010, 07:14 PM
#2
Re: Console to input/output file
1) There is (basically) no difference in coding between console input and file input.
Instread of "cin >>" , you use "inStream >>"
2) Why do you have 2 open statements for the same inStream object ?
3) When reading from a file, you would not have "cout prompts"
Last edited by Philip Nicoletti; November 16th, 2010 at 07:18 PM.
-
November 16th, 2010, 11:17 PM
#3
Re: Console to input/output file
Thank you Lerner for helping me out a little it helped alot I guess since there isnt any errors anymore but it still doesnt work right. Whenever it starts all it does is show all of the couts in my code and it just tells me to press any key to continue. I need it so each month will have the number corresponding in the .txt file so basically .txt is 1,2,3,4...etc. so it'll be Jan: 1 Feb: 2 so on and so on.
Here is what I have so far with the changes. Thank you if you can help me.
#include <iostream>
#include <fstream>
#include <cstdlib>
using std::ifstream;
using std:fstream;
using std::cout;
using std::endl;
void turnToMonth(int month);
int main()
{
ifstream inStream;
ofstream outStream;
double rainfall[12];
double averages[12];
int currentMonth;
char tableOrGraph;
char yesOrNo;
inStream.open("previousrain.txt");
cout << "Please enter average rainfall for each month" << endl;
for (int i=0; i<12; i++) {
turnToMonth(i);
cout << ": ";
inStream >> averages[i]; }
cout << "What is the number of the current month? Jan=1, Feb=2, etc." << endl;
inStream >> currentMonth;
cout << "Please enter the rainfall for each month in the previous year" << endl;
int count = 0;
for (int month=currentMonth-1; count < 12; month=(month+1)%12, count++) {
turnToMonth(month);
cout << ": ";
inStream >> rainfall[month]; }
cout << "\nWould you like to see a table of the results\n"
<< "or a graph? Type 't' for the table or 'g' for the graph.";
inStream >> tableOrGraph;
do { //Asks for table or graph at least once
if (tableOrGraph == 't') {
cout << "Rainfall table\n\n" //Sets up title of the table
<< "Month\t\tRainfall (prev. 2 months)\tDeviation\n"; //Sets up column titles
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++) { //
turnToMonth(month); //
cout << "\t\t" << rainfall[month] << "\t\t\t"; //
if (rainfall[month] > averages[i]) //Sets up data under each column as well as I could.
cout << rainfall[month] - averages[i] << endl; //Note the use of /t chracters
else if (rainfall[month] < averages[i]) //
cout << averages[i] - rainfall[month] << endl; //
else //
cout << "0\n"; } } //
else if (tableOrGraph == 'g') {
cout << "\nRainfall graph\n\n" //Sets up the title of the graph
<< "Month\n"; //Sets up the column
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++) { //
cout << endl; //
turnToMonth(month); //Writes in order: Month, bar graph of
cout << "\nAverage rainfall: "; //average rainfall of that month,
for (i; averages[i] > 0; averages[i]--) //bar graph of actual rainfall of that month.
cout << "*"; //
cout << "\nActual rainfall: "; //
for (i; rainfall[month] > 0; rainfall[month]--) //
cout << "*"; } } //
cout << "\n" << "Would you like to see another table or graph? 'y' for yes and 'n' for no.";
inStream >> yesOrNo; }
while (yesOrNo == 'y');
return 0; }
void turnToMonth(int month) {
switch(month) {
case 0:
cout << "Jan";
break;
case 1:
cout << "Feb";
break;
case 2:
cout << "March";
break;
case 3:
cout << "April";
break;
case 4:
cout << "May";
break;
case 5:
cout << "June";
break;
case 6:
cout << "July";
break;
case 7:
cout << "Aug";
break;
case 8:
cout << "Sept";
break;
case 9:
cout << "Oct";
break;
case 10:
cout << "Nov";
break;
case 11:
cout << "Dec";
break; }
}
-
November 16th, 2010, 11:40 PM
#4
Re: Console to input/output file
What is the purpose of all the cout statements ? For eaxmple:
Code:
cout << "Please enter average rainfall for each month" << endl;
If you are inputting values from the console, that lets the user know
what should be entered. But you are reading from a file, so that
is not needed. What does your input file look like ? It should
have the same input as when you were entering data from the console.
So, it looks like the first 12 lines should have the average rainfall for
January thru December.
Then the output file should have the results.
Basically, run the console version of the code and write down each input.
Then put that input into the input file.
The next line would contain the current month. etc.
The entire "turnToMonth()" function is not needed.
Last edited by Philip Nicoletti; November 16th, 2010 at 11:43 PM.
-
November 18th, 2010, 12:55 AM
#5
Re: Console to input/output file
Here's what I have so far. The only problem I have left is that my months don't show up in the output file and I'm really not sure why. I'd really appreciate it if someone could point me in the right direction.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void turnToMonth(int month);
int main()
{
ifstream my_input_file;
ofstream my_output_file;
double rainfall[12];
double averages[12];
int currentMonth;
char tableOrGraph;
char yesOrNo;
my_input_file.open("input_file.txt");
my_output_file.open("output_file.txt");
my_output_file << "Please enter average rainfall for each month" << endl;
for (int i=0; i<12; i++)
{
my_input_file >> averages[i];
turnToMonth(i);
my_output_file << ": " << averages[i] << endl;
}
my_output_file << "\nWhat is the number of the current month? Jan=1, Feb=2, etc."
<< endl;
my_input_file >> currentMonth;
my_output_file << currentMonth;
my_output_file << "\n\nPlease enter the rainfall for each month in the previous year"
<< endl;
int count = 0;
for (int month=currentMonth-1; count < 12; month=(month+1)%12, count++)
{
turnToMonth(month);
my_output_file << ": ";
my_input_file >> rainfall[month];
my_output_file << rainfall[month] << endl;
}
my_output_file << "\nWould you like to see a table of the results\n"
<< "or a graph? Type 't' for the table or 'g' for the graph.";
my_input_file >> tableOrGraph;
do
{
if (tableOrGraph == 't')
{
my_output_file << "Rainfall table\n\n"
<< "Month\t\tRainfall (prev. 2 months)\tDeviation\n";
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++)
{
turnToMonth(month);
my_output_file << "\t\t" << rainfall[month] << "\t\t\t";
if (rainfall[month] > averages[i])
my_output_file << rainfall[month] - averages[i] << endl;
else if (rainfall[month] < averages[i])
my_output_file << averages[i] - rainfall[month] << endl;
else
my_output_file << "0\n";
}
}
else if (tableOrGraph == 'g')
{
my_output_file << "\nRainfall graph\n\n" << "Month\n";
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++)
{
my_output_file << endl;
turnToMonth(month);
my_output_file << "\nAverage rainfall: ";
for (i; averages[i] > 0; averages[i]--)
my_output_file << "*";
my_output_file << "\nActual rainfall: ";
for (i; rainfall[month] > 0; rainfall[month]--)
my_output_file << "*";
}
}
my_output_file << "\n" << "Would you like to see another table or graph? 'y' for yes and 'n' for no.";
yesOrNo = 'n';
}
while (yesOrNo == 'y');
my_input_file.close();
my_output_file.close();
return 0;
}
void turnToMonth(int month)
{
switch(month)
{
case 0:
cout << "Jan";
break;
case 1:
cout << "Feb";
break;
case 2:
cout << "March";
break;
case 3:
cout << "April";
break;
case 4:
cout << "May";
break;
case 5:
cout << "June";
break;
case 6:
cout << "July";
break;
case 7:
cout << "Aug";
break;
case 8:
cout << "Sept";
break;
case 9:
cout << "Oct";
break;
case 10:
cout << "Nov";
break;
case 11:
cout << "Dec";
break;
}
}
And here is my output file. To the left of each colon and also near the bar graphs should be the months.
Code:
Please enter average rainfall for each month
: 1
: 2
: 3
: 4
: 5
: 6
: 7
: 8
: 9
: 10
: 11
: 12
What is the number of the current month? Jan=1, Feb=2, etc.
11
Please enter the rainfall for each month in the previous year
: 9
: 9
: 9
: 9
: 9
: 9
: 9
: 9
: 9
: 9
: 9
: 9
Would you like to see a table of the results
or a graph? Type 't' for the table or 'g' for the graph.
Rainfall graph
Month
Average rainfall: *
Actual rainfall: *********
Average rainfall: **
Actual rainfall: *********
Average rainfall: ***
Actual rainfall: *********
Average rainfall: ****
Actual rainfall: *********
Average rainfall: *****
Actual rainfall: *********
Average rainfall: ******
Actual rainfall: *********
Average rainfall: *******
Actual rainfall: *********
Average rainfall: ********
Actual rainfall: *********
Average rainfall: *********
Actual rainfall: *********
Average rainfall: **********
Actual rainfall: *********
Average rainfall: ***********
Actual rainfall: *********
Average rainfall: ************
Actual rainfall: *********
Would you like to see another table or graph? 'y' for yes and 'n' for no.
Last edited by fdhbvk2; November 18th, 2010 at 12:57 AM.
-
November 18th, 2010, 06:40 AM
#6
Re: Console to input/output file
Do you mean why the "Jan" , "Feb", etc ? Are not being sent to the output file ?
If so, it is because you are using "cout" in turnToMonth() instead of writing
to your output file. Normally, you would put those string literals into
an array instead of your method. But to modify your method ...
pass the output stream variable to the turnToMonth() function by
reference, and use it instead of cout.
Code:
void turnToMonth(int month , ostream & out); // change prototype
//
turnToMonth(i,my_output_file); // calling routine
//
void turnToMonth(int month , ostream & out)
{
switch(month)
{
case 0:
out << "Jan"; // change cout to out
break;
// etc.
-
November 18th, 2010, 03:47 PM
#7
Re: Console to input/output file
Thank you so much! It runs perfectly now, but is there any way to have both the table and graph in the output file? As of right now, only one will show up.
-
November 18th, 2010, 04:19 PM
#8
Re: Console to input/output file
Well, you tell it to do only one or the other ...
Code:
if (tableOrGraph == 't')
{
// rest of your code
}
else if (tableOrGraph == 'g')
{
// rest of your code
}
So either ... take away the condtional if and just execute what is in
both sections of the input ... or have the data in the input file be such
that it loops a second time. The sewcond time it chooses graph
instead of table.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|