unexpected output

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;

viscden();
viscDen();


cout << "Density(slug/ft^3):" ;
cout << den << endl;
cout << "Viscosity(lbf-sec/ft^2):" ;
cout << visc << endl;


return 0;
}

float viscden()
{
char a, b, c, code;
float fluid, visc = 0;
const float visA = 2.29, visB = 1.17, visC = 4.01;

if (code == a)
{
cout << "Fluid: Ethyl alcohol" << endl;
visc = visc + visA;
}

else if (code == b)
{
cout << "Fluid: Methyl alcohol" << endl;
visc = visc + visB;
}

else if (code == c)
{
cout << "Fluid: Propyl alcohol" << endl;
visc = visc + visC;
}

else
cout << "Invalid code. Please try again.";


return visc;
}

float viscDen()
{
char a, b, c, code;
float den = 0;
const float denA = 1.527, denB = 1.531, denC = 1.556;

if (code == a)
den = den + denA;

else if (code == b)
den = den + denB;

else if (code == c)
den = den + denC;
else
cout << "Invalid code. Please try again.";

return den;

}
Last edited on
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;
const float 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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
using namespace std;

double viscden(char code);
double viscDen(char code);

int main()
{
	char code;
	double den, visc;	

	cout << "Please enter the Fluid Code:";
	cin >> code;

	visc = viscden(code);
	den = viscDen(code);

	cout << "Density(slug/ft^3):";
	cout << den << endl;
	cout << "Viscosity(lbf-sec/ft^2):";
	cout << visc << endl;

	return 0;
}

double viscden(char code)
{
	double visc = 0;
	const double visA = 2.29, visB = 1.17, visC = 4.01;

	if (code == 'a')
	{
		cout << "Fluid: Ethyl alcohol" << endl;
		visc = visc + visA;
	}

	else if (code == 'b')
	{
		cout << "Fluid: Methyl alcohol" << endl;
		visc = visc + visB;
	}

	else if (code == 'c')
	{
		cout << "Fluid: Propyl alcohol" << endl;
		visc = visc + visC;
	}

	else
		cout << "Invalid code. Please try again.";


	return visc;
}

double viscDen(char code)
{
	double den = 0;
	const double denA = 1.527, denB = 1.531, denC = 1.556;

	if (code == 'a')
		den = den + denA;

	else if (code == 'b')
		den = den + denB;

	else if (code == 'c')
		den = den + denC;
	else
		cout << "Invalid code. Please try again.";

	return den;
}
Last edited on
thanks @jonnin and @AbstractionAnon...i got it now
You might be better with a database-type solution.

Your viscosities are 100000 times too large.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include <string>
using namespace std;

void viscDen( int code, string &name, double &den, double &vis );

int main()
{
   const double SIden = 515.379, SIvis = 47.8803;
   string name;
   double den, vis;   
   int code;
   
   cout << "How many carbon atoms: ";
   cin >> code;
   viscDen( code, name, den, vis );
   cout << "Name: " << name << '\n';
   cout << "Density: " << den << " slug/ft^3    (" << den * SIden << " kg/m^3 )" << '\n';
   cout << "Dynamic viscosity: " << vis << " lbf-s/ft^2    (" << vis * SIvis << " Pa s )" << '\n';   
}

void viscDen( int code, string &name, double &den, double &vis )
{
   struct Chemical
   {
      string name;
      double den;
      double vis;
   };
   const Chemical alcohol[] = { { "", 0.0, 0.0 }, { "Methanol", 1.537, 1.20e-5 }, { "Ethanol", 1.531, 2.47e-5 }, { "Propanol", 1.558, 4.09e-5 } };
   
   if ( code < 1 || code > 3 )
      {
       cout << "Invalid code\n";
       code = 0; 
   }
   name = alcohol[code].name;
   den = alcohol[code].den;
   vis = alcohol[code].vis;
}


How many carbon atoms: 2
Name: Ethanol
Density: 1.531 slug/ft^3    (789.045 kg/m^3 )
Dynamic viscosity: 2.47e-05 lbf-s/ft^2    (0.00118264 Pa s )
Last edited on
Topic archived. No new replies allowed.