8 Replies - 732 Views - Last Post: 18 November 2013 - 09:59 AM Rate Topic: -----

#1 moneymike47  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 05-April 12

Overloading Stream Operators Help

Posted 13 November 2013 - 01:13 PM

Hello all,

I really need help in my Advanced Object Oriented Programming class! We are coding in C++ and we are to create a program that creates a weather station. if you press '1' you input the temperature, wind speed and direction; if you press '2' if will give you the current weather reading; if you press '3' it will give you the weather history; and if you press '4' you exit. Also, it reads the outputs from a text file and outputs them to the console...I have that all working correctly. The problem i am having trouble with is we are to overload the stream (<< and >>) operators so we no longer have to call the print() function. Previously, i called the print function to print the current weather reading as well as the temperature history.

Temp.H

#ifndef TEMP
#define TEMP
#include <fstream>
#include <string>

using namespace std;

//int index=0;

class Temperature 
{
	int value;
	//char unit;

public:
string tempLine1;

int getTemp();
int getTemp2();

Temperature()
{
value=0;
//unit='F';
}

Temperature(int v/*, char u*/)
{
	value=v;
	//unit=u;
}
friend ostream &operator<<(ostream &out, const Temperature &p); // needs 'friend' i think
friend istream &operator>>(istream &in, Temperature &p);
//friend ostream operator<<(ostream out, const Temperature p);
//friend istream operator>>(istream in, Temperature &p);

//void printTemp(int temper);
//void printTemp2(int temper);
};

#endif 


Temp.cpp file

 #include "temp.h"
#include "Language.h"

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//        Lang La;
        fstream myEngFile5;
		string tempLine5;
		string tempLine6;
		//string tempLine1; // from language file


		/**** Keep the getTemp() method, but get rid of the print method 
		and overload the operators there*******/

ostream &operator<<( ostream &out, const Temperature &p) //&f
{
	//int far=0;
	fstream myEngFile1;
	string tempLine1;
	myEngFile1.open ("English.txt");
	myEngFile1.seekg(194);
	getline(myEngFile1, tempLine1);
	//La.getTempLang();
	out<<p.tempLine1<<endl; // What is the Temperature?
//	cin>>p; //f
	return out;
	//myEngFile1.close();
	}



/*int Temperature::getTemp()
{
	int value=0;
	fstream myEngFile1;
	string tempLine1;
	myEngFile1.open ("English.txt");
	myEngFile1.seekg(194);
	getline(myEngFile1, tempLine1);
	cout<<tempLine1<<"\n"; // What is the Temperature?
	cin>>value;
	return value;
	myEngFile1.close();
	}*/

int Temperature::getTemp2()
{
	int value=0;
	fstream myPigFile1;
	string tempLine1;
	myPigFile1.open ("PigLatin.txt");
	myPigFile1.seekg(254);
	getline(myPigFile1, tempLine1);
	cout<<tempLine1<<"\n"; //What is the Temperature? in piglatin
	cin>>value;
	return value;
	myPigFile1.close();
	}

/*void Tem::printTemp(int temper)
	{
		fstream myEngFile5;
		string tempLine5;
		string tempLine6;
		myEngFile5.open("English.txt");
		myEngFile5.seekg(335);
		getline(myEngFile5, tempLine5);
		cout<<tempLine5<<temper; //The Temperature is
		myEngFile5.seekg(356);
		getline(myEngFile5, tempLine6);
		cout<<tempLine6<<"\n"; //F
	}*////////////////////////////////////////////////////Old print function

ostream &operator<<( ostream &out, const Temperature p)
{    
		out<<tempLine5<<p.value; //The Temperature is __
		out<<tempLine6<<"\n"; //F
		return out;
}

istream &operator>>( istream &in, Temperature &p)
{
	    //in>>p.value;
		myEngFile5.open("English.txt");
		myEngFile5.seekg(335);
		getline(myEngFile5, tempLine5);
		in>>tempLine5;
		myEngFile5.seekg(356);
		getline(myEngFile5, tempLine6);
		in>>tempLine6;
		return in;
} 


Source.cpp (Main)

 #include <iostream>
#include <string>
#include "temp.h"
#include "Wind.h"
#include "Language.h"
#include <fstream>
#define SIZE 10
using namespace std;

int main()
{
	Temperature temp1;
	win wind1;
	//Temperature p;
	string windDir[SIZE];
    int windSpeed[SIZE];
	int temp[SIZE];
	//Tem p1(int temp);
	int index=0;
	int reply;
	int reply2;
	int lang;
	int count=0;
	string STRING1, STRING2, STRING3, STRING4, STRING5, STRING6;
	fstream myEngFile;
	fstream myPigFile;
	ofstream out;

	//Language Menu
	cout<<"**** Welcome to The Michael Weather Station! \n";
	cout<<"**** Elcomeway otay Ethay Ichaelmay Eatherway Ationstay! \n";
	cout<<"Please enter 1 for english ";
	cout<<"orway 2 orfay iglatinpay \n";
	cin>>lang;
	cin.clear();

	switch(lang){

		//English Main Menu
	case 1:
	//for(int count=0; count<21; count++)
	for(int index=0; index<SIZE; index++){
	bool menu = true;
    while (menu != false){
	myEngFile.open ("English.txt");
	getline(myEngFile, STRING1);
	cout<<STRING1<<"\n";
	getline(myEngFile, STRING2);
	cout<<STRING2<<"\n";
	getline(myEngFile, STRING3);
	cout<<STRING3<<"\n";
	getline(myEngFile, STRING4);
	cout<<STRING4<<"\n";
	getline(myEngFile, STRING5);
	cout<<STRING5<<"\n";
	cin>>reply;
	cin.clear();
	myEngFile.close();

	switch(reply){

		//Gets data in English
	case 1:
	temp[count]=temp1.getTemp();
	windSpeed[count] =wind1.getSpeed();
	windDir[count] = wind1.getDirection();
	cout<<" \n";
	count++;
	break;
	
	//Prints data in English
	case 2:
		cout<<"--------------------------------\n";
	//temp1.printTemp(temp[count-1]);
		Temperature p = temp1.getTemp();
	//cout<<temp[count-1];
		cout<<p;
	wind1.printWindMeasurement(windSpeed[count-1], windDir[count-1]);
	cout<<" \n";
	cout<<" \n";
	break;

	//Prints History in English 
	case 3:
		cout<<"---------------------------------- \n";
	//for(int index=SIZE%count; index<SIZE; index++){
	for(int i=0; i<count; i++){
	//temp1.printTemp(temp[i]);
	cout<<temp[i];
	wind1.printWindMeasurement(windSpeed[i], windDir[i]);
	cout<<"\n";
	}
		break;

		//Exits...In English?
	case 4:
		cout<<"Exiting............. \n";
		menu= false;
		break;
	
		//Cheacks to see that only 1,2,3, or 4 is entered
	default:
		cout<<"Please enter 1,2,3, or 4 only!!! \n";
		cout<<" \n";

	} //Switch(reply) in english

	} //While loop in english

	return 0;

} //For loop in english

	break;

	//PigLatin Main Menu
	case 2:
	for(int index=0; index<SIZE; index++){
	bool menu = true;
    while (menu != false){
	myPigFile.open ("PigLatin.txt");
	getline(myPigFile, STRING1);
	cout<<STRING1<<"\n";
	getline(myPigFile, STRING2);
	cout<<STRING2<<"\n";
	getline(myPigFile, STRING3);
	cout<<STRING3<<"\n";
	getline(myPigFile, STRING4);
	cout<<STRING4<<"\n";
	getline(myPigFile, STRING5);
	cout<<STRING5<<"\n";
	getline(myPigFile, STRING6);
	cout<<STRING6<<"\n";
	cin>>reply2;
	cin.clear();
	myPigFile.close();


	switch(reply2){

		//Gets data in PigLatin
	case 1:
	temp[count]=temp1.getTemp2();
	windSpeed[count] =wind1.getSpeed2();
	windDir[count] = wind1.getDirection2();
	cout<<" \n";
	count++;
	break;
	
	//Prints data in PigLatin
	case 2:
		cout<<"----------------------------------\n";
	//temp1.printTemp2(temp[count-1]);
	cout<<temp[count-1];
	wind1.printWindMeasurement2(windSpeed[count-1], windDir[count-1]);
	cout<<" \n";
	cout<<" \n";
	break;

	//Prints History in PigLatin
	case 3:
		cout<<"------------------------------------ \n";
	//for(int index=SIZE%count; index<SIZE; index++){
	for(int i=0; i<count; i++){
	//temp1.printTemp2(temp[i]);
	cout<<temp[i];
	wind1.printWindMeasurement2(windSpeed[i], windDir[i]);
	cout<<"\n";
	}
		break;

		//Exits...in PigLatin?
	case 4:
		cout<<"exitingway............. \n";
		menu= false;
		break;
	
		//Checks to see only 1,2,3, or 4 is entered
	default:
		cout<<"Easeplay enterway 1,2,3, orway 4 onlyway!!! \n";
		cout<<" \n";

	} //Switch (Reply2) in piglatin

	} //While loop for PigLatin Main Menu

	return 0;

} //For loop for PigLatin Main Menu

break;

} //switch(Lang)

} //Main 



I have excluded my files wind.h and wind.cpp because i havent even started trying to implement the overloading in there yet; plus i think i posted enough code in one post. Also, i should mention that the program also reads output from an english.txt if the user wants english or piglation from piglatin.txt if they want piglatin

Is This A Good Question/Topic? 0
  • +

Replies To: Overloading Stream Operators Help

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,896
  • Joined: 12-December 12

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 02:25 PM

Comparing this:

ostream &operator<<( ostream &out, const Temperature p)

to the example here:
http://msdn.microsof...(v=vs.110).aspx

you are missing an ampersand (&):
ostream &operator<<( ostream &out, const Temperature& p)

I haven't checked >> and I don't know anything beyond this, so good luck ;)

See the example here at tutorialspoint as well.

This post has been edited by andrewsw: 13 November 2013 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 moneymike47  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 05-April 12

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 02:26 PM

That's another issue... depending where you look, they tell you to put an ampersand, or they dont....Also some places tell you to declare the class as a 'const' and some dont
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,896
  • Joined: 12-December 12

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 02:32 PM

You also seem to have defined this insertion operator twice. But not really my area, so good luck (again).
Was This Post Helpful? 0
  • +
  • -

#5 moneymike47  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 05-April 12

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 02:47 PM

Good catch! although that won't full solve the problem, im sure it will definitely help.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,896
  • Joined: 12-December 12

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 03:08 PM

I am over-stretching my knowledge of C++ here.. but.. when you overload these operators all it means is that you can use your object temp1 in statements like this:

cout << "Temperature is : " << temp1 << endl;

Your over-loading code simply determines which (string) property, or properties, of your object will be output by this statement. That is, your code shouldn't be attempting to open a file. For the writing to occur to a file, rather than the console, cin and cout can be redirected to a file.

From the tutorialspoint tutorial:

Quote

The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types...

This post has been edited by andrewsw: 13 November 2013 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 moneymike47  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 05-April 12

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 03:16 PM

View Postandrewsw, on 13 November 2013 - 03:08 PM, said:

cout << "Temperature is : " << temp1 << endl;

Your over-loading code simply determines which (string) property, or properties, of your object will be output by this statement. That is, your code shouldn't be attempting to open a file. For the writing to occur to a file, rather than the console, cin and cout can be redirected to a file.


I don't have to write to any files, but i am supposed to still read output from a file and output it to the console...As well as overloading the stream operators....Im thinking reading from a file shouldnt be done in the overload process
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: Overloading Stream Operators Help

Posted 13 November 2013 - 08:06 PM

Quote

.Im thinking reading from a file shouldnt be done in the overload process

Why not? Reading and writing to and from a file stream is the purpose of overloading the insertion and extraction operators. Remember cout is an output file stream and cin is an input file stream.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 moneymike47  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 05-April 12

Re: Overloading Stream Operators Help

Posted 18 November 2013 - 09:59 AM

View Postjimblumberg, on 13 November 2013 - 08:06 PM, said:

Quote

.Im thinking reading from a file shouldnt be done in the overload process

Why not? Reading and writing to and from a file stream is the purpose of overloading the insertion and extraction operators. Remember cout is an output file stream and cin is an input file stream.


Jim

Yeah you are right, I ended up being able to get it to work correctly, and the reading from a file was done within the overloading process
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1