7 Replies - 1822 Views - Last Post: 18 November 2010 - 12:08 AM Rate Topic: -----

#1 Guest_jvbdhye4*


Reputation:

Console to input/output file

Posted 16 November 2010 - 03:24 PM

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::ofstream;
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; } }



Is This A Good Question/Topic? 0

Replies To: Console to input/output file

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Console to input/output file

Posted 16 November 2010 - 03:34 PM

What error/warning messages do you get when you compile?

Please post the entire error messages.

Jim

This post has been edited by jimblumberg: 16 November 2010 - 03:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Console to input/output file

Posted 16 November 2010 - 09:29 PM

I already fixed the errors so there are no more errors. Thank you but 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::ofstream;
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 {                                                             
  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])                                    
			  cout << rainfall[month] - averages[i] << endl;                    
		  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.";
  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; } 

}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 17 November 2010 - 01:33 PM

Was This Post Helpful? 0

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Console to input/output file

Posted 16 November 2010 - 11:27 PM

Welcome to DIC!

Please edit your posting (see the "EDIT" button in lower right). (If Edit is not available make a new posting on this thread and skip ( a ) below.)
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
Read here for more help with the code tags
http://www.dreaminco...-code-tags-are/
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_jvbdhye4*


Reputation:

Re: Console to input/output file

Posted 17 November 2010 - 01:31 PM

Can someone help me on this I'm desperate. Thank you if you can.
Was This Post Helpful? 0

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Console to input/output file

Posted 17 November 2010 - 01:51 PM

When I compile your code I get the following error/warning messages:

Quote

main.cpp||In function ‘int main()’:|
main.cpp|66|warning: statement has no effect|
main.cpp|69|warning: statement has no effect|
main.cpp||In function ‘void turnToMonth(int)’:|
main.cpp|80|warning: switch missing default case|
||=== Build finished: 0 errors, 3 warnings ===|


Take a look at the first warning on line 66:

for (i; averages[i] > 0; averages[i]--)


What are you trying to do on this line? I doubt that it is doing what you think.

You should also look at this whole section:

	  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 << "*"; 
           } 
        }




Both the inner for loops are incorrect. I also think the first loop might be incorrect. You might want to print out the loop variables inside the loop to see if they are doing what you expect.

Jim



Jim

This post has been edited by jimblumberg: 17 November 2010 - 01:51 PM

Was This Post Helpful? 1
  • +
  • -

#7 Guest_jvbdhye4*


Reputation:

Re: Console to input/output file

Posted 17 November 2010 - 11:04 PM

Thanks for all of the help. I finally have most of it working. My only problem is that I can't get my months to show up.

#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's my output file. To the left of the colons and near the bar graphs should be the months.

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.


Was This Post Helpful? 0

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Console to input/output file

Posted 18 November 2010 - 12:08 AM

Are you even trying?

Look at line 77.
What needs to change on that line to do part of what you want to do?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1