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