hi, this program executes fine but the output isn't what I want. for some reason, my if and else statement is not executing. This is the question:
"(Fluid Mechanics) The viscosity and density of three common fluids are listed in the following chart:
Using this data, write and test a C++ function named viscDen() that returns the viscosity and density of the selected fluid by using reference parameters. The type of fluid should be input to the function as a character that’s passed by value."
please help me!
#include<iostream>
using namespace std;
float viscden();
float viscDen();
int main()
{
char a, b, c, code;
float fluid, den, visc;
const float denA = 1.527, visA = 2.29, denB = 1.531, visB = 1.17, denC = 1.556, visC = 4.01;
cout << "Please enter the Fluid Code:";
cin >> code;
please use code tags, edit the post to fix.
but right off I spot one issue:
1 2 3 4 5 6 7
float viscDen()
{
char a, b, c, code;
float den = 0;
constfloat denA = 1.527, denB = 1.531, denC = 1.556;
if (code == a)
what is the value of code? what is the value of a?
both are neither initialized nor read in or anything. They have random junk in them.
the variables in a function are not tied to other variables of the same name in main. They are new variables, local to the function. If you want the ones from main, you need to pass them in.
the value for code is either a, b or c. i wanted to assign a, b and c to either of the alcohols, like a is ethyl alcohol, b is methyl alcohol and c is propyl alcohol.
Do you understand what an uninitialized variable is? Uninitialized variables contain garbage.
jonnin didn't ask what they represent, he asked what value they have when you run your program.
problem 1: a, b, c, and code in viscden() and viscDen() are local variables and are unrelated to a, b, c, and code in main.
problem 2: if you want to test the value the user entered, you have to compare code to the literals 'a', 'b' or 'c'. Note the single quotes around the letter.
problem 3: You call viscden() and viscDen(), but don't assign the returned value to anything.