Nested IF ELSE troubles.

Need help getting my nested IF ELSE statement working, it works for the most part but I cannot get it to work completely, or maybe I am writing it completely wrong.

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

	//Patient 1 analysis
	if (systolicI <= 119 || diastolicI <= 79){
		cout << last << ", " << first << " has normal blood pressure " << systolic << "/" << diastolic << endl;
	} else 
		if (systolicI >= 120, systolicI <= 139 || diastolicI >= 80, diastolicI <= 89){
		cout << last << ", " << first << " has Hypertension " << systolic << "/" << diastolic << endl;
	} else 
		if (systolicI >= 140, systolicI <= 159 || diastolicI >= 90, diastolicI <= 99){
		cout << last << ". " << first << " has Stage 1 High Blood Pressure " << systolic << "/" << diastolic << endl;
	} else {
		cout << last << ". " << first << " has Stage 2 High Blood Pressure " << systolic << "/" << diastolic << endl;
	}

	//Patient 2 analysis
	if (systolic2I <= 119 || diastolic2I <= 79){
		cout << last2 << ", " << first2 << " has normal blood pressure " << systolic2 << "/" << diastolic2 << endl;
	} else 
		if (systolic2I >= 120, systolic2I <= 139 || diastolic2I >= 80, diastolic2I <= 89){
		cout << last2 << ", " << first2 << " has Hypertension " << systolic2 << "/" << diastolic2 << endl;
	} else 
		if (systolic2I >= 140, systolic2I <= 159 || diastolic2I >= 90, diastolic2I <= 99){
		cout << last2 << ". " << first2 << " has Stage 1 High Blood Pressure " << systolic2 << "/" << diastolic2 << endl;
	} else {
		cout << last2 << ". " << first2 << " has Stage 2 High Blood Pressure " << systolic2 << "/" << diastolic2 << endl;
	}



This results in getting, for example, normal blood pressure when I am supposed to be getting Stage 2 High BP.
Last edited on
Using the comma operator in an if() statement usually doesn't produce the results you desire, you probably want to be using one of the logical operators instead.

What are the values for systolicI and diastolic1 that is causing the problems?

It looks like maybe you're using the wrong logic in that first if() statement. Shouldn't the systolic be less than 120 and the diastolic be less than 80?

Perhaps you should review your blood pressure chart to be sure.

http://www.heart.org/HEARTORG/Conditions/HighBloodPressure/KnowYourNumbers/Understanding-Blood-Pressure-Readings_UCM_301764_Article.jsp#.WRJTnNXyE0Q

Also you should strive to eliminate the code duplication, functions should be your friends.
and the coupled conditions are odd. what if the patient had 190 / 70?
I don't know medical stuff but I don't know if that code covers all possible settings with correct statements.


every comparison costs you .. its more processing, its a place for a bug, and its visual clutter.

If you agree to put the code in a function anyway, so you can call it on every patient cleanly...

void doBP(double sys, double dia)
{
if(sys > 139 || dia > 89)
{
cout << high2;
return;
}

if(sys > 119 || dia > 79)
{
cout << high2;
return;
}

etc. I may not have the numbers right, but the idea is it checks the highest cutoff, if that fails, check the next highest... soon as it finds one, it prints & quits. Return replaces else, more or less. But you don't need to bound the conditions... if it got past the earlier check, the next one can absorb that information and check a simpler condition.

}
Last edited on
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void pause()
{
	system("pause");
}


int main()
{
	//Declaring variables
	string patient1, patient2;
	string first, first2, last, last2;
	string systolic, systolic2, diastolic, diastolic2;
	int systolicI, systolic2I, diastolicI, diastolic2I;
	string myString, myString2;

	//Declaring file
	ifstream patient;

	//Opening file
	patient.open("F://patient.DAT");


	//Extracting from patient.DAT
	while(! patient.eof()){

		//First patient information
		getline(patient, patient1);
		myString = patient1;

		last = myString.substr(0, 5);		 //Last name
		first = myString.substr(6, 8);		 //First name
		systolic = myString.substr(15, 3);	 //Systolic BP reading
		diastolic = myString.substr(19 , 3);	 //Diastolic BP reading

//		cout << last  << ", " << first << " " << systolic << "/" << diastolic << endl;    //   ****Was testing to see if I extracted right


		//Second patient information
		getline(patient, patient2);
		myString2 = patient2;

		last2 = myString2.substr(0, 5);        //Last name
		first2 = myString2.substr(6, 3);		  //First name
		systolic2 = myString2.substr(10, 3);	  //Systolic BP reading
		diastolic2 = myString2.substr(14, 3);  //Diastolic BP reading
		
	
	//	cout << last2  << ", " << first2 << " " << systolic2 << "/" << diastolic2 << endl;   //   ****Same as above
	}

	//Converting blood readings from strings to integers
	systolicI = stoi(systolic);
	diastolicI = stoi(diastolic);
	systolic2I = stoi(systolic2);
	diastolic2I = stoi(diastolic2); 

	//Patient 1 analysis
	if (systolicI <= 119 && diastolicI <= 79){
		cout << last << ", " << first << " has normal blood pressure " << systolic << "/" << diastolic << endl;
	} else 
		if (systolicI >= 120, systolicI <= 139 && diastolicI >= 80, diastolicI <= 89){
		cout << last << ", " << first << " has Hypertension " << systolic << "/" << diastolic << endl;
	} else 
		if (systolicI >= 140, systolicI <= 159 && diastolicI >= 90, diastolicI <= 99){
		cout << last << ". " << first << " has Stage 1 High Blood Pressure " << systolic << "/" << diastolic << endl;
	} else {
		cout << last << ". " << first << " has Stage 2 High Blood Pressure " << systolic << "/" << diastolic << endl;
	}

	//Patient 2 analysis
	if (systolic2I <= 119 && diastolic2I <= 79){
		cout << last2 << ", " << first2 << " has normal blood pressure " << systolic2 << "/" << diastolic2 << endl;
	} else 
		if (systolic2I >= 120, systolic2I <= 139 && diastolic2I >= 80, diastolic2I <= 89){
		cout << last2 << ", " << first2 << " has Hypertension " << systolic2 << "/" << diastolic2 << endl;
	} else 
		if (systolic2I >= 140, systolic2I <= 159 && diastolic2I >= 90, diastolic2I <= 99){
		cout << last2 << ". " << first2 << " has Stage 1 High Blood Pressure " << systolic2 << "/" << diastolic2 << endl;
	} else {
		cout << last2 << ". " << first2 << " has Stage 2 High Blood Pressure " << systolic2 << "/" << diastolic2 << endl;
	}

	
	//Closing patients file
	patient.close();

	//Freezing
	cout << "\n";
	pause();

	return (0);
}


Here is the entire code.
The file that I am pulling the information from looks like this:
1
2
Smith Eleanore 150/78
Jones Tom 200/78


This is the output that I get. I don't have low blood pressure in this program, just anything above normal.


Smith, Eleanore has Hypertension 150/78
Jones, Tom has Hypertension 200/78

Press any key to continue . . .


It is pulling the information from the first condition that it finds and doesn't check the rest, which is what I cannot find out, I will try doing the functions and the function example that jonnin gave.
I got the program to work, thank you jonnin for the example function, I took that and rearranged it a little bit and it ended up working great.

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
string norm = " has Normal Blood pressure.\n";
string hyper = " has Hypertension.\n";
string high1 = " has Stage 1 High Blood Pressure.\n";
string high2 = " has Stage 2 High Blood Pressure.\n";

void doBP(double sys, double dia, string name1, string name2)
{

	if(sys >= 160 || dia >= 100)
{
	cout << name1 << ", " << name2 << high2;
	return;
}

if(sys >= 140 && sys <= 159 || dia >= 90 && dia <= 99)
{
	cout << name1 << ", " << name2 << high1;
	return;
}

if(sys >= 120 && sys <= 139 || dia >= 80 && dia <= 89)
{
	cout << name1 << ", " << name2 << hyper;
	return;
}

if(sys <= 119 || dia <= 79)
{
	cout << name1 << ", " << name2 << norm;
	return;
}
}

Thank you both for answering, sorry I was so late to see this.
I'm glad that you got the program to work.

However you should also strive to reduce your code duplication and have only one return path from the program.

And it looks like you're only testing one patient's blood pressure but showing the results for two patients. I'd recommend something more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string check_blood_pressure(int systolic, int diastolic)
{
    std::string ret_value;

    if(systolic > 180 || diastolic > 100)
        ret_value = "Hypertensive Crisis";
    else if(systolic > 160 || diastolic > 100)
        ret_value = "(Hypertension) Stage 2";
    else if(systolic > 139 || diastolic > 89)
        ret_value = "(Hypertension) Stage 1";
    else if(systolic > 119 || diastolic > 79)
        ret_value = "Prehypertension";
    else
        ret_value = "Normal";

    return(ret_value);
}

Then you would just call this function for any number of patients.

Last edited on
Topic archived. No new replies allowed.