I/O reading the next line in input file?

Hello, Im a newb so please go easy on me. Im trying to get my program to display a persons name (lastname, FirstName) whether they are high blood pressure or normal. Anyways I have all that figured out. What Im having problem with is the fact that I have no clue how to get it to read the next input line in my file.

My input file reads:
Smith Eleanore 110/73
Jones Tom 200/78

any help is appreciated.

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

	// declare variables
	string junk;
	string lastName;
	string firstName;
	ifstream myFile;
	int sys;
	int dia;

	// open files
	myFile.open ("c:/Users/Jonald/Desktop/new file.dat");
		if ( !myFile )
		{	cout << "Unable to find file. Please exit and try again."; }

	// read files
		myFile >> lastName >> firstName >> sys;

		myFile.ignore (1, '/');

		myFile >> dia;
		
		
	while(myFile)
			{if (sys <= 119 && dia <= 79)
		cout << lastName << ", " << firstName << " has normal blood pressure at " << sys << "/" << dia << endl;
		
		else if (sys >= 120 && sys <= 139 || dia >= 80 && dia <= 89)
		cout << lastName << ", " << firstName << " has prehypertension at " << sys << "/" << dia << endl;

		else if (sys >= 140 && sys <= 159 || dia >= 90 && dia <= 99)
		cout << lastName << ", " << firstName << " has Stage 1 high blood pressure at " << sys <<"/"<< dia << endl;
			
		else 
		cout << lastName << ", "<< firstName << " has Stage 2 high blood pressure at " << sys <<"/"<< dia << endl;
		
		myFile.close();}
		
closed account (j3Rz8vqX)
Repeat the process (Line 27-31 provided from the posted source code).

The real problem is how are you going to store the next three data?

3 more variables? or array of 2?
Last edited on
I have no idea. I feel like im approaching this problem all wrong lol
1
2
3
4
5
6
7
8
9
10
11
12
// read files
while( myFile >> lastName >> firstName >> sys &&
       myFile.ignore(N, '/') &&
       myFile >> dia )
{
    if( sys <= 119 && dia <= 79 )
        cout << ...
    else if ...
    // .. etc
}

myFile.close();
closed account (j3Rz8vqX)
The above code embedded in your if condition as an else:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    myFile.open ("c:/Users/Jonald/Desktop/new file.dat");
    if ( !myFile )
    {	
        cout << "Unable to find file. Please exit and try again."; 
    }
    else
    {
        // read files
        while( myFile >> lastName >> firstName >> sys && myFile.ignore(N, '/') && myFile >> dia )
        {
            if( sys <= 119 && dia <= 79 )
            cout << ...
            else if ...
            // .. etc
        }

        myFile.close();
    }
that helped it from repeating the same person but how to i get it to read the next person on the next line?

the output i got was
Smith, Eleanore has normal blood pressure at 110/73

i need it to output all the names and blood pressures in the file like that.

Thanks for that though I was not sure how to get the ignore into the while statement
Topic archived. No new replies allowed.