| | |
Pass by Reference: Need help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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);
}
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);
}
The function's type is void and you are trying to display it :p
Call the function and then send the variable to cout.
Call the function and then send the variable to cout.
C++ Syntax (Toggle Plain Text)
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; }
Last edited by minas1; Nov 8th, 2008 at 5:54 pm.
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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?
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.
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- Isn't java pass by reference? (Java)
- Pass Value & Pass-By-Reference (C++)
- need help understandin how to pass by reference. (Java)
Other Threads in the C++ Forum
- Previous Thread: Deprecated Or Antiquated Header
- Next Thread: runtime error while incresing the couter inside a loop
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






