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


}

Dani AI

Generated

Two separate issues caused the trouble: the C2679 stream error and the numeric inaccuracy/warning. Both are easy to fix.

The stream error happens because you tried to do cout << fahr(centi) while fahr is declared void. Streaming needs a value; a void function returns nothing. Either change the function to return the converted number and stream that return value, or keep the reference (out-parameter) style and call the function first, then stream the variable. This is exactly what recommended: call the void function to modify centi, then cout the centi value.

The incorrect conversion came from integer division and mixed types. When you write 9/5 the compiler does integer division (result 1). Use floating-point division instead (use a decimal or the f suffix), or make your variables double. Also, if you use 9.0 (double) but keep variables float, MSVC will warn about converting double to float — either use 9.0f/5.0f or switch the variables to double. was right to point out the literals.

One more practical point: your nested for looks suspicious (incrementing j by 25 while testing j <= 5 makes the inner loop run once). For two side-by-side tables either run two independent loops and print corresponding rows, or use a single loop with two counters (e.g., for (float c = 0, f = 0; c <= 100; c += 5, f += 25)) so each line prints a Celsius value and a Fahrenheit value. Finally, apply fixed/setprecision consistently and test a few known points (0C -> 32F, 100C -> 212F) to verify correctness.

Recommended Answers

All 6 Replies

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

Call the function and then send the variable to cout.

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

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?

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

Call the function and then send the variable to cout.

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

Trying doing 9.0/5.0 rather than just 9/5

Chris

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

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.

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

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

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.