Pass by Reference: Need help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 8
Reputation: techgenie is an unknown quantity at this point 
Solved Threads: 0
techgenie techgenie is offline Offline
Newbie Poster

Pass by Reference: Need help!

 
0
  #1
Nov 8th, 2008
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);

}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Pass by Reference: Need help!

 
0
  #2
Nov 8th, 2008
The function's type is void and you are trying to display it :p

Call the function and then send the variable to cout.

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: techgenie is an unknown quantity at this point 
Solved Threads: 0
techgenie techgenie is offline Offline
Newbie Poster

Re: Pass by Reference: Need help!

 
0
  #3
Nov 9th, 2008
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?

Originally Posted by minas1 View Post
The function's type is void and you are trying to display it :p

Call the function and then send the variable to cout.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Pass by Reference: Need help!

 
0
  #4
Nov 9th, 2008
Trying doing 9.0/5.0 rather than just 9/5

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: techgenie is an unknown quantity at this point 
Solved Threads: 0
techgenie techgenie is offline Offline
Newbie Poster

Re: Pass by Reference: Need help!

 
0
  #5
Nov 9th, 2008
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!

Originally Posted by Freaky_Chris View Post
Trying doing 9.0/5.0 rather than just 9/5

Chris
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: techgenie is an unknown quantity at this point 
Solved Threads: 0
techgenie techgenie is offline Offline
Newbie Poster

Re: Pass by Reference: Need help!

 
0
  #6
Nov 9th, 2008
[/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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Pass by Reference: Need help!

 
0
  #7
Nov 9th, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC