In this problem,You are given a file containing an ordered list of all students
ID: 3539528 • Letter: I
Question
In this problem,You are given a file 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 to 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 itto the user.
Hints:
-Use c-strings to store text. You can use as many as you feel necessary.
- You will probably want to use c-string functions that were introduced in
class, e.g., strcpy(), strcmp(), strcat(). You can find documentation and
examples of these in the book and online (do not copy them, just reference
them!).
-You need to format the string of the input name so that it will match the
format of names in the file.
- Another function that might be useful is getline() since the >> operators read
only up to any whitespace.
- Use good file input/output practice - make sure the file opens correctly and
be sure to close it.
Please help me out as this is urgent and need help!
Thank you so much
Explanation / Answer
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fstream>
using namespace std;
int main()
{
char name[100];
cout<<"Enter the name to be searched : ";
cin.getline(name, 100);
ifstream inFile("course_roaster.csv");
char first[50], last [50];
int rank = 1;
int flag = 0;
while(!inFile.eof())
{
inFile >> last;
inFile >> first;
char tName[100];
strcpy(tName, first);
strcat(tName, " ");
strcat(tName, last);
if(strcmp(name, tName)==0)
{
flag = 1;
break;
}
else
rank++;
}
if(flag==1)
cout << "Rank of "<<name <<" is "<<rank<<endl<<endl;
else
cout<<name <<" not found in file."<<endl<<endl;
inFile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.