943,801 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1375
  • C++ RSS
Nov 8th, 2008
0

Pass by Reference: Need help!

Expand Post »
Hey guys
Need help with the following program. I keep getting compilation error

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

Apparently I am not able to pass by reference however it works well when I pass by value.
The assignment requires a pass by reference function or I would just do pass by value.
My dilemma is how do I call the pass by reference function properly.
I am desperate. Please help anyone!

#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;
using std::setprecision;
using std::fixed;

float cent(float); // function prototype (pass by value)
void fahren(float &); // function prototype (pass by reference)

// function main begins program execution
int main()
{
float centi = 0;
float fah = centi;

// displays table information
cout << setw(25) << "Centigrade to Fahrenheit" << setw(30)
<< setw(35) << "Fahrenheit to Centigrade" << endl << endl;

// two tables are created
cout << setw(8) << "Centigrade" << setw (16) << "Fahrenheit" // table for centigrade to fahrenheit
<< setw(18) << "Fahrenheit" << setw(16) << "Centigrade"; //table for fahrenheit to centigrade
cout << endl;

for (float i = 0; i <= 100; i+= 5)
{
centi = i;
for (float j = 0; j <= 5; j+= 25)

cout << setw(6) << setprecision(0) << i + j << setw(18)
<< setprecision(2) << fixed << fahren(centi) << ' ' << setw(15)
<< setprecision(2) << i + j << setw(18) << setprecision(2) << fixed << cent(i + j) << ' ' << endl;
}

return 0;
}

float cent(float f)
{
f = ((f * 9/5) + 32);
return f;
}
void fahren( float &c)
{
c = ((c - 32) * 5/9);

}
Similar Threads
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 8th, 2008
0

Re: Pass by Reference: Need help!

The function's type is void and you are trying to display it :p

Call the function and then send the variable to cout.

C++ Syntax (Toggle Plain Text)
  1. fahren(centi);
  2. cout << setw(6) << setprecision(0) << i + j << setw(18)
  3. << setprecision(2) << fixed << centi<< ' ' << setw(15)
  4. << setprecision(2) << i + j << setw(18) << setprecision(2) << fixed << cent(i + j) << ' ' << endl;
  5. }
Last edited by minas1; Nov 8th, 2008 at 5:54 pm.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 9th, 2008
0

Re: Pass by Reference: Need help!

Thank you minas1, it worked. However my calculations from centigrade to fahrenheit does not seem accurate. I will continue work on it.

Does the calculations appear accurate? If not, any suggestions?

Click to Expand / Collapse  Quote originally posted by minas1 ...
The function's type is void and you are trying to display it :p

Call the function and then send the variable to cout.

C++ Syntax (Toggle Plain Text)
  1. fahren(centi);
  2. cout << setw(6) << setprecision(0) << i + j << setw(18)
  3. << setprecision(2) << fixed << centi<< ' ' << setw(15)
  4. << setprecision(2) << i + j << setw(18) << setprecision(2) << fixed << cent(i + j) << ' ' << endl;
  5. }
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 9th, 2008
0

Re: Pass by Reference: Need help!

Trying doing 9.0/5.0 rather than just 9/5

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 9th, 2008
0

Re: Pass by Reference: Need help!

Chris,
Appreciate your suggestion. I tried that but got this error message:
conversion from 'double' to 'float', possible loss of data
I even converted all floats to doubles and got the same calculations as when they were floats.
Maybe the error is in the for loop!

Trying doing 9.0/5.0 rather than just 9/5

Chris
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 9th, 2008
0

Re: Pass by Reference: Need help!

[/QUOTE]

You are right Chris, the calculations do work. Apparently I overlooked the - (negative) I got for my previous calculations(when it was 9/5 and designated float).
The - (negative) is now gone after changing all floats to doubles and 9/5 to 9.0/5.0.

Thanks a lot.
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 9th, 2008
0

Re: Pass by Reference: Need help!

Good sorry i should of paid attention to the fact you were using floats, i would then of suggested 9.0f/5.0f

Glad its sorts, if there are no more problems then mark thread as solved

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to deserialize binary stream
Next Thread in C++ Forum Timeline: runtime error while incresing the couter inside a loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC