Grabbing More data...

Hello all,

I have a program that is near complete (thanks to users like you), but there is just one more thing I would like it to be able to do. The program is used to determine the overall score of a school based on their results in multiple competitive events. Right now, the program asks the user for a school name, and then goes line by line looking for the name, and upon finding it, gets the school's place finished in the particular event and then converts the place to points, which are summed. The user then gets the overall team score. Example below.


What the data file looks like:

Inventions and Innovations
Place School Name State
1 School X OK
2 School Z FL
3 Imanginary School KS
4 ...etc

Agriculture and Biotechnology
Place School Name State
1 Sidewalk School NY
2 American HS NJ
3 School X OK

Program does the following -
Enter a school name:
>School X

----<Award Summary for School X>-----

1st place (+10 points)
3rd place (+8 points)
etc.


Now the last thing I want it to do is to print the event name between the place and points in the award summary, so it would look like:

1st place Inventions and Innovations (+10 points)
3rd place Agriculture and Biotechnology (+8 points)
etc...

Somehow, when the program finds the school name and gets the placement, the program will have to know to go "n+2" lines back from the placement (where placement = n) to get the line with event name, since the way the txt file is formatted the event title is "placement + 2" lines back from the placement.

Thanks in advance-

Chf. Os

PS: Code below for your inspection

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
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>

using namespace std;

const int PLACE_MATH = 11;

void printHeader();						//Print Header Function

int main()
{
 
	printHeader();						//Prints Header
	

		string fileName;						//Declare string type for file name

		cout << "Please enter the conference and year... \n"
			"(enter 2012  conference as 'nationals12.txt', etc.)\n" << endl;

		getline ( cin, fileName );

	while ( true )
	{
		string school = "";					//Declare school name
		string temp;						//Declare temporary string to be read
		int points = 0;						//Declare and initialize value for points
		int totalPoints = 0;				//Delcare and initialize value for total points
		ifstream inputFile;					//Declare input file stream

		inputFile.open( fileName.c_str());		//Opens file

		if ( !inputFile )			//If not in file, display error message, and exit 
		{
			cout << "Error: Data file could not be opened.\n"
			"Check that the file name is correct.\n";

			exit (EXIT_FAILURE);
		}

		// Ask user for school name
		cout << "\nPlease enter a school name, state abbreviation (preceded "
			"by a tab),\n or enter 'exit':\n\n>";

		getline( cin, school );

		if ( school == "exit")
		{
			cout << "\n Thank you come again \n\n";

			return 0;
		}

		else
		{
			cout << "\n-----<" << school << " Award Summary>-----\n\n";


			while ( getline( inputFile, temp ) )		// Read the next line in the file.
			{
				if ( temp.find ( school ) != -1 )		// Search line for the school name.
				{
					// If school name is found, get its placement.
					int placement = stoi ( temp.substr ( 0, temp.find ( '\t' ) ) );

					// Based on the placement, determine the suffix of place, and print 
					if ( placement == 1 )
					{ 
						string var = "st";
						cout << "  " << placement << var << " place";
					}
					if ( placement == 2 )
					{
						string var = "nd";
						cout << "  " << placement << var << " place";
					}
					if (placement == 3 )
					{
						string var = "rd";
						cout << "  " << placement << var << " place";
					}
					if (placement > 3)
					{
						string var = "th";
						cout << "  " << placement << var << " place";
					}

					points = PLACE_MATH - placement;	//How many points in the event
					cout << "	+" << points << " points" << endl;

					totalPoints += points;		//Keep running total of total points
				}
			}


			//Print how many points the school had
			cout << endl << ">>>>>" << school << " had " << totalPoints;
			cout << " OVERALL TEAM POINTS!>>>>> \n"
				">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" << endl;

	
	
		inputFile.close ( );									// Close the input file 
		}
	}
	return 0;

}
You need to remember the event when you see it. Replace lines 63 & 64 with:
1
2
3
4
5
6
7
8
9
10
11
bool nextLineIsEvent = true;
while (getline(inputFile, temp)) {
    if (temp.size()) {
        if (nextLineIsEvent) {
            eventName= temp;
            nextLineIsEvent = false;
            continue;
        }
    } else {
        nextLineIsEvent = true;
    }


Note that there is a bug at line 65. It will match if the input string is a subset of the school name. For example suppose you have two schools called "London" and "Londonderry". If the user enters "London" then it will also match on Londonderry

I like to think of it as a feature, not a bug. This way you don't have to type out the full school name. ;)

You could print out the name of the school it found and ask the user if it's the right one. If they say no, keep looking.
@dhayden I realize that this is the case, I agree it is more of a feature as it can shorten the input. I only have to be careful in the rare cases like you mentioned above (I would type London High School instead of just London).

Regard to your suggestion of the solution to the problem, I am not searching for the event name. I am searching for the school name. I only need the event name after I have found the school name. This is why it has stumped me.
Last edited on
Topic archived. No new replies allowed.