If & Else if issue.

I have an incomplete program to solve different variations of the Bernoulli Equation. However with my if & else if statements It seems like it isn't computing the Boolean correctly or is skipping to the final else and executing that everytime.

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
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>


using namespace std;

//This program is to solve the Bernoulli equation
int main ()
{
	double va, vb, pa, pb, j, g, h, eqnt, c, aa, ab;
	
	cout<<"WELCOME TO BERNOULLIS DOME \n";
	cout<<"Enter 1 for unknown value and 0 for unused value \n";
	cout<<"Enter value for va-int. flowspeed (m/s)\n";
		cin>>va;
	cout<<"Enter value for vb-final flowspeed (m/s)\n";
		cin>>vb;
	cout<<"Enter value for pa-pressure(N/m^2)\n";
		cin>>pa;
	cout<<"Enter value for pb-pressure(N/m^2)\n";
		cin>>pb;
	cout<<"Enter value for j-density(kg/m^3)\n";
		cin>>j;
	cout<<"Enter value for h-height (m)\n";
		cin>>h;
	cout<<"Enter value for c-coefficient of friction\n";
		cin>>c;
	cout<<"Enter value for initial area\n";
		cin>>aa;
	cout<<"Enter value for final area\n";
		cin>>ab;
		g=9.18;
	//cout<<"If Pressurized tank then type 1\n";
	//cout<<"If Vented tank then type 2\n";
	//cout<<"If Pressurized tank then type 3\n";
	//cin>>eqnt;
	//if (eqnt==1)
	{
		if (va == 1)
		{
			va=sqrt((2/(1-(aa*aa/ab*ab)))*(((pa-pb)/j)+g*h));
			cout<<va<< "(m/s) \n";
		}
		else if (vb == 1)
		{
			vb=sqrt((2/(1-(ab*ab/aa*aa)))*(((pb-pa)/j)+g*h));
			cout<<vb<<"(m/s) \n";
		}
		else if (pa == 1)
		{
			pa=j*g*h+pb+vb*vb*(1-(ab*ab/aa*aa));
			cout<<pa<<"(N/m^2) \n";
		}
		else if (pb == 1)
		{
			pb=j*g*h+pa-vb*vb*(1-(ab*ab/aa*aa));
			cout<<pb<<"(N/m^2) \n";
		}
		else;
		{
			cout<<"you suck start over \n";
			// NEED terminate command
		}
	}
	/*else if (eqnt==2)
	{
		if (va==1)
		{
		vb=sqrt(2*(pa-pb)/j);
		cout<<va<<"(m/s) \n";
		}

		else if (vb==1)
		{
		vb=sqrt(2*(pb-pa)/j);
		cout<<vb<<"(m/s) \n";
		}

	}
	*/
	return 0;
}	


Last edited on
You have a rogue ';' after the else statement. Remove that and your problem will go away.

By the way, when you post code, please use the code tags. They show up as "<>" in the Format box to the right.
Im not sure what is wrong now to be honest... It just terminates the program after compiling.
Last edited on
It just terminates the program after compiling


Do you mean when you try and run it? After you've entered your values?

Also, consider what happens when aa == ab or ab == 0, etc.
after I enter the values for the variables it automatically decides to execute the else statement then terminates the program



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
74
75
76
77
78
79
80
81
82
83
84
85
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>


using namespace std;

//This program is to solve the bernoulli equation
int main ()
{
	double va, vb, pa, pb, j, g, h, eqnt, c, aa, ab;
	
	cout<<"WELCOME TO BERNOULLIS DOME \n";
	cout<<"Enter 1 for unknown value and 0 for unused value \n";
	cout<<"Enter value for va-int. flowspeed (m/s)\n";
		cin>>va;
	cout<<"Enter value for vb-final flowspeed (m/s)\n";
		cin>>vb;
	cout<<"Enter value for pa-pressure(N/m^2)\n";
		cin>>pa;
	cout<<"Enter value for pb-pressure(N/m^2)\n";
		cin>>pb;
	cout<<"Enter value for j-density(kg/m^3)\n";
		cin>>j;
	cout<<"Enter value for h-height (m)\n";
		cin>>h;
	cout<<"Enter value for c-coefficient of friction\n";
		cin>>c;
	cout<<"Enter value for initial area\n";
		cin>>aa;
	cout<<"Enter value for final area\n";
		cin>>ab;
		g=9.18;
	//cout<<"If Pressurized tank then type 1\n";
	//cout<<"If Vented tank then type 2\n";
	//cout<<"If Pressurized tank then type 3\n";
	//cin>>eqnt;
	//if (eqnt==1)
	{
		if (va == 1)
		{
			va=sqrt((2/(1-(aa*aa/ab*ab)))*(((pa-pb)/j)+g*h));
			cout<<va<< "(m/s) \n";
		}
		else if (vb == 1)
		{
			vb=sqrt((2/(1-(ab*ab/aa*aa)))*(((pb-pa)/j)+g*h));
			cout<<vb<<"(m/s) \n";
		}
		else if (pa == 1)
		{
			pa=j*g*h+pb+vb*vb*(1-(ab*ab/aa*aa));
			cout<<pa<<"(N/m^2) \n";
		}
		else if (pb == 1)
		{
			pb=j*g*h+pa-vb*vb*(1-(ab*ab/aa*aa));
			cout<<pb<<"(N/m^2) \n";
		}
		else
		{
			cout<<"you suck start over \n";
			// NEED terminate command
		}
	}
	/*else if (eqnt==2)
	{
		if (va==1)
		{
		vb=sqrt(2*(pa-pb)/j);
		cout<<va<<"(m/s) \n";
		}

		else if (vb==1)
		{
		vb=sqrt(2*(pb-pa)/j);
		cout<<vb<<"(m/s) \n";
		}

	}
	*/
	return 0;
}	

Last edited on
Please use code tags when posting code. Click the little button that looks like "<>" and paste your code between the tags.

What values are you inputting into your program? If none of the values va, vb, pa or pb are exactly equal to 1, then the final else will execute.
1 // va ... this is the value I am looking for
10 //vb
.1 //pa
.2 // pb
1000 // j
10 // h
0 // c
100 // aa
10 //ab

with the output of

-1.#IND(m/s)

I am going to double check my equations... I don't know where else the error could come from

Edit*****

From trial and error I think my equation is off or the numbers im entering are off, I am going to redo my math by hand. ill post results shortly... thanks for your help
Last edited on
Topic archived. No new replies allowed.