This code isn't working properly

Hello everyone, can someone please help me with my code I am trying to run. I am trying to find a solution on why this file is freezes when I run it.

According to the assignment the program is to

-initialize patientCount to 0
-read first ID and howMany from file
-while not end-of-file
*increment patientCount
*display ID
*use a count-controlled loop to read and sumup this patient’s howMany BP’s
*calculate and display average for patient
*read next ID and howMany from file
-display patientCount




Here is my code so far
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
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;

int main(){

    //Initialize Required Variables For Program
    int patientCount = 0;
    string id;
    string rHowMany; //String To Read From File
    int howMany = 0;
    int howManyCount = 0;
    int avg = 0;
    int avg2;
    string line;
    int number_lines = 0;
    ifstream reader ("data.txt"); //Open The Data File To Be Read From

    do
    {
        reader >> id;
        reader >> howMany;
        cout << "The Patient ID Is: " << id << endl;
        cout << "The Number Of Blood Pressure Record This Patient Has Is: " << howMany << endl;
        do
        {
            reader >> avg;
            avg = avg + avg;
        }
        while (reader != "\n");
        cin.clear();
        avg = avg / howMany;
        cout << "The Patient Average BP Reading Is: " << avg;
    }
    while (!EOF);
    return 0;
}




The data.txt file looks exactly like this
1
2
4567 4 180 140 170 150
5643 2 125 150


1st column is Patient ID
2nd column is How Many Tests the patient had
3rd column and beyond is all the blood pressure readings.

If anyone can help me out I greatly appericate that.
1
2
3
4
5
6
        do
        {
            reader >> avg;
            avg = avg + avg; //avg = 2*avg
        }
        while (reader != "\n");

`reader' is a file, ¿what do you think you are doing in that condition?
Also, you'll end with just the last reading on `avg'

1
2
3
4
5
6
7
while( reader>>in and reader>>howMany ){ //do not loop on eof
   for(int K=0; K<howMany; ++K)
      reader >> number;
      sum += number;
   }
   //...
}



If you want to read an unknown of numbers in a line
1
2
3
4
5
6
std::string line;
std::getline( std::cin, line );
std::istringstream input(line);
while( input>>n ){
   //...
}
Last edited on
I tried it out and for some reason it is still not working. This is what I have editted so far.



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
int main()
{
	//Initialize Required Variables For Program
    int patientCount = 0;
    string id;
    string rHowMany; //String To Read From File
    int howMany = 0;
    int howManyCount = 0;
    int avg = 0;
    int avg2;
    string line;
    int number_lines = 0;
    ifstream reader ("data.txt"); //Open The Data File To Be Read From

    do{
        reader >> id;
        reader >> howMany;
        cout << "The Patient ID Is: " << id << endl;
        cout << "The Number Of Blood Pressure Record This Patient Has Is: " << howMany << endl;
        do{
            reader >> avg;
            avg = avg + avg; //avg = 2*avg
		}
	} 
	while( reader >> id && reader >> howMany ){
		for(int K=0; K<howMany; ++K)
		reader >> number;
		sum += number;
   } 
    system("pause");
    return 0;
}




I'm still not understanding what I am doing wrong?? My teacher never explained how to do this at all.
I guess this is the unsolvable code. Thanks everyone for trying to help, I understand this seems a little more intermidate but I guess I just have to keep reading about it.
The idea was to replace the do-while loops that you where using, with the while and for.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
	//Initialize Required Variables For Program
	int patientCount = 0;
	string id;
	int howMany = 0;
	ifstream reader ("data.txt"); //Open The Data File To Be Read From

	while( reader>>in and reader>>howMany ){ //as long as you can read another pacient data
		int sum=0; //sum accumulates the pressure readings per pacient
		cout << "The Patient ID Is: " << id << endl;
		cout << "The Number Of Blood Pressure Record This Patient Has Is: " << howMany << endl;
		for(int K=0; K<howMany; ++K){
			int number;
			reader >> number;
			sum += number;
		}
		//compute the average here
	}
	return 0;
}
Topic archived. No new replies allowed.