Homework help. PLEASE!!!

You are given a file named "course_roster.txt" containing an ordered list of all students in the class. Each name is on its own line in the file and is listed in the following format:

LAST,FIRST

Your program should prompt the user and accept the first name and then the last name of a student (assume that the user enters both names in all capitals). Using this input, you must determine the rank of the student according to the list in the file. If the name is not found, you should report it to the user.


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
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;


int main()

{
	ifstream inFile;
    
	inFile.open("course_roster.txt"); 

	if(inFile.fail())
	{
		cout<< "Cannot open file" << endl;
		exit(1);
	}
	else
	{
		cout<< "File opened.\n";
	}
	
	char name[100];

	{

		cout<<"Enter the name to be searched : ";

			cin.getline(name, 100);
	
		char FIRST[50], LAST [50];

		int rank = 1;

		int flag = 0;

		bool found = false;

			while(!inFile.eof())

			{

				inFile >> LAST;

				inFile >> FIRST;

				strcpy(LAST, FIRST);
				strcat(LAST, ",");
				strcat(LAST, FIRST);


			if(strcmp(name, LAST)==0)

				{
					found = true;
					flag = 1;

						break;
				}

					else 
						rank++;

			if(flag==1)
			
			cout << "Rank of "<<name <<" is "<<rank - 1 <<endl;
			
			else
			
				cout<<name <<" not found in file."<<endl;
			}
			
			
			inFile.close();

			}
	
}


I am having trouble out putting the rank of the students

the course_roster.txt has 5 names in all caps

RAGER, SCOTT
PHILIP, PAUL
SMITH, JOHN
DOE, JACK
WILLIAMS, BILL
What is the rank of a user?
Make a int to hold the line count, read a line and check if its a match, if not add one to line count and check again.

1
2
3
4
5
6
7
8
ifstream users(""class_roster.txt");
string temp, name = (last + "," + first);
int rank = 1;
while(getline(cin,temp))
    if(temp == name)
        cout << "User rank is " << rank << endl;
    else
        ++rank; 
Last edited on
The rank is:
1. RAGER, SCOTT
2. PHILIP, PAUL
3. SMITH, JOHN
4. DOE, JACK
5. WILLIAMS, BILL
then my solution will work, do you not understand? Here is a little clearer.

1
2
3
4
5
6
7
8
9
10
11
string temp, name;
cout << "Please Enter a name as shown : LAST,FIRST" << endl;
getline(cin,name);
ifstream users("class_roster.txt");

int rank = 1; //holds rank/line
while(getline(cin,temp)) // get line check if line matches user input
    if(temp == name)
        cout << "User rank is " << rank << endl;
    else
        ++rank; // doesnt match then increment count to represent current rank/line 
Last edited on
yes, that helps a lot. Thank you so much for your help!
Topic archived. No new replies allowed.