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

In a (too) simplified response, you use an ifstream, inStream in your program, to read from the file into the program and ab ofstream, outStream in your program, to write material from the program to a file. Use inStream instead of cin anytime you want to read from the file and outStream instead of cout anytime to want to write to the file instead of the monitor.

Only associate one file per stream at a time.

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

}

Edited 6 Years Ago by doggie123: n/a

This article has been dead for over six months. Start a new discussion instead.