1 Replies - 296 Views - Last Post: 26 October 2013 - 04:17 PM Rate Topic: -----

#1 soupysizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-October 13

converting my current program to using arrays.

Posted 26 October 2013 - 01:32 PM

so this ended up being the program i wrote, now my teacher asked us to convert are current project to use arrays now. Not quite understanding arrays. Also she added that to get the data from our file its going to look like 2000(year) 1(month) 10(rain in inches) so in the new program when im getting my data getInput function could i still use the similar set up by passing the data in the array by int &month[], something similar to that with each function. With that being said. I could get rid of the convert number to month function and be able to create a for loop to display the months on the bar graph. apologize for the newb question but i have no clue what im doing. just looking for general help on how to get started. I appreciate the time.


#include <iostream> // for count
#include <fstream> // file rainInput.txt
#include <cstdlib> // for exit
#include <iomanip>
#include <string>
using namespace std;
// functions 
void openInputFile(string fileName); // opens file
void openOutput(string fileName);	// creates the output file
void getInput(int &month, int &rainFallAmount);	// get input from file
string writeMonth( int month); //convert number to month
string drawBar ( int rainFallAmount); // create bar line
string drawScale(); //create scale 
string statistics( int cumulativeRainFall , int rainFallAmount); //show statistics
string statistics2( int maxMonth );

ifstream inStream;
ofstream outStream;
const string INPUT_FILE = "rainInput.txt";    // declaring the innput and output file as a constant
const string OUTPUT_FILE = "rainOutput.txt";


int main (){  //main function
	int month;							//declare month
	int rainFallAmount;					//deckare Rain fall amount
	int cumulativeRainFall = 0;			//declare the cummulative rain fall amount
	int maxMonth;						//declare the month with the max rain fall
	int maxRainFall = 0;				// declard the maximum rain fall 


	openInputFile(INPUT_FILE);
	openOutput(OUTPUT_FILE);
	
	while(!inStream.eof())// read from the file
	{
		getInput(month, rainFallAmount);	//get the input 
		outStream << setw(10) << setiosflags(ios::left) << writeMonth(month); // stting the month to left 10 spaces 
		outStream << " |"; // drawing the line
		outStream << drawBar(rainFallAmount) << endl;
		cumulativeRainFall =  cumulativeRainFall + rainFallAmount; // total rain fall
		if( rainFallAmount > maxRainFall){  //max rainfall 
			maxMonth = month;
			maxRainFall = rainFallAmount;
		}

	}

	outStream << drawScale();
	outStream << endl;
	outStream << statistics(cumulativeRainFall, rainFallAmount);
	outStream << endl;
	outStream << statistics2 (maxMonth);
}

void openInputFile(string fileName){  //open file
	inStream.open(fileName);
	if (inStream.fail())
	{
		cout << "Input file opening failed. \n";
		exit(1);
	}
	
}
void openOutput(string fileName){ //open output file
	outStream.open(fileName);
	if (outStream.fail())
	{
		cout << "Output file opening failed. \n";
		exit(1);
	}
}
void getInput(int &month, int &rainFallAmount) { //month and rain fall amount passed to the getinput fuction 
	if(!inStream.eof())// read from the file
	{
		inStream >> month >> rainFallAmount; // dispace the month and rain fall amount
	}
}
string writeMonth( int month){ // convert the numbers into months
	string convert_to_m ;

	switch (month) 
	{
		case 1:
				convert_to_m="January" ;
				break ;
		case 2:
				convert_to_m="Febuary" ;
				break ;
		case 3:
				convert_to_m="March" ;
				break ;
		case 4:
				convert_to_m="April" ;
				break ;
		case 5:
				convert_to_m="May" ;
				break ;
		case 6:
				convert_to_m="June" ;
				break ;
		case 7:
				convert_to_m="July" ;
				break ;
		case 8:
				convert_to_m="August" ;
				break ;
		case 9:
				convert_to_m="September" ;
				break ;
		case 10:
				convert_to_m="October" ;
				break ;
		case 11:
				convert_to_m="November" ;
				break ;
		case 12:
				convert_to_m="December" ;
				break ;
	}
	return convert_to_m ;
}
string drawBar ( int rainFallAmount){ // creating the ***** as a bar graph
	string bar = "";

	for( int x=0; x < rainFallAmount; x++){
		bar = bar + "*****";
	}
	return bar;
}
string drawScale(){	//crate the scale
	string scale =  "           |___________________________________________________";
	scale = scale +	"\n            ****1****2****3****4****5****6****7****8****9****10";
    return scale;
}

string statistics( int cummulativeRainFall , int rainFallAmount){
	string stat1;
	stat1 = " Cummulative rain fall:  " + cummulativeRainFall ; 
	return stat1;
}
 
string statistics2(  int maxMonth){
	string stat2;

	stat2 = " Month with the max rain: " + maxMonth;
	return stat2;
}

This post has been edited by jimblumberg: 26 October 2013 - 01:39 PM
Reason for edit:: Added missing code tags, please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: converting my current program to using arrays.

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: converting my current program to using arrays.

Posted 26 October 2013 - 04:17 PM

What's the question?
How do you use an array?

General tutorial on C++:
http://www.cplusplus.com/doc/tutorial/

Tutorial on arrays:
http://www.cplusplus...utorial/arrays/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1