I'll keep it simple and hopefully whatever simple error I keep overlooking can be used as a learning experience for others. This program uses a user created file called

patient.dat that contains the following:


Smith Eleanore 110/73
Jones Tom 200/78

The error I am having is that Mr Tom Jones should be giving my output a message saying has stage 2 high blood pressure. But it is saying that his blood pressure is normal.
And the second issue is that it is doing this twice. So when the file has been read, it is restarting the loop again using the last line instead of just stopping.

Why is my code not reading the int "200" correctly ?

And how can I make the loop stop without repeating the last line twice in output?

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;


void pause()
{   
	cout << "\n\n Press enter to continue..."; //pause
	char junk;
	cin.ignore();
	cin.get(junk);
}


int main()
{

	//declare variables
	ifstream myInfile1; //declare the in file

	string firstName, lastName, bacon; // declarations
	int syst, dias;
	char junk;


	//get input

	myInfile1.open("c:\\c++\\patient.dat"); //openfiles  MAKE SURE FILE NAMES AND DIRECTORIES MATCH THROUGHOUT CODE!!!
	
	
    while (myInfile1) // while loop continues while myInfile1 is still being read
    {

	myInfile1 >> lastName >> firstName >> syst >> junk >> dias; // since .txt if formatted with whitespaces this grabs the values in order stopping at each whitespace (junk) stands for the slash

if ((syst < 120) || (dias < 80)) //if syst is equal to or less than 120// dias is less than or eqaul to 80
{
cout << lastName << ", " << firstName << " has normal blood pressure " << syst << junk << dias; // lastname, firstname has normal blood pressure syst/dias
}


else
if(((syst >= 120) && (syst <= 139)) || ((dias >= 80) && (dias <= 89)))//if syst is equal to or greater than 120 & less than 139// dias is greater than or eqaul to 80 & less than 89
{
cout<< lastName << ", " << firstName << " has prehypertension " << syst << junk << dias;// lastname, firstname has prehypertension syst/dias
}


else
if(((syst >= 140) && (syst <= 159)) || ((dias >= 90) && (dias <= 99)))//if syst is equal to or greater than 140 & less tahn 159// dias is less than or eqaul to 90 & less tahn 99
{
cout<< lastName << ", " << firstName << " has stage 1 high blood pressure " << syst << junk << dias;// lastname, firstname has stage 1 high blood pressure syst/dias
}


else
if((syst >= 160 ) || (dias >= 100))//if syst is equal to or greater than 160// dias is equal to or greater than 100
{
cout << lastName << ", " << firstName << " has stage 2 high blood pressure " << syst << junk << dias;// lastname, firstname has stage 2 high blood pressure syst/dias
}


cout << "\n\n";

}
	
	cout << "\n\nEnd of report\n";

	pause();
	return 0;
}

Thanks for any help in advance

Hi,
Answer to your first question:-
1) Why is my code not reading the int "200" correctly ?
- Your code is reading correctly and out put is also according to your code condition. AS for "Mr Tom Jones" syst = 200 and dias = 78 so your first condition is true "if ((syst < 120) || (dias < 80))" because dias is less then 80 i.e. 78

commented: Thank you +1

Use while (!myInfile1.eof()) in place of line 35 and also close file handler at line 70) myInfile1.close();

Use while (!myInfile1.eof()) in place of line 35 and also close file handler at line 70) myInfile1.close();

No, don't. Here's why ( feof() is the same as .eof()

commented: Great resource with thorough explanation +1

Hi,
Answer to your first question:-
1) Why is my code not reading the int "200" correctly ?
- Your code is reading correctly and out put is also according to your code condition. AS for "Mr Tom Jones" syst = 200 and dias = 78 so your first condition is true "if ((syst < 120) || (dias < 80))" because dias is less then 80 i.e. 78

Ok so I have a logical error then. Where am I confused on this then? I thought that from my specific code it mean syst < 120 OR dias < 80. Did i screw that up?

No, don't. Here's why ( feof() is the same as .eof()

Thank you, as you stated the code provided above pretty much duplicated the error i was already having. Your fix solved that issue. Thank you Walt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.