PROBLEM: You are given a file named \"course_roster.csv\" (on Angel) containing
ID: 3539530 • Letter: P
Question
PROBLEM:
You are given a file named "course_roster.csv" (on Angel) 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. For extra credit(5 points), have your program ask the user if they would like to search for another student and repeat the process until they answer in the negative.
HINTS:
1. Use c-strings to store texts, you can use as many as you feel necessary
2. 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!).
3. You need to format the string of the input name so that it will match the format of names in the file.
4. Another function that might be useful is getline() since the >> operators read only up to any whitespace.
NOTE:- I have pasted the file (course_roster) at the location where my project is saved !!!!!
Explanation / Answer
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fstream>
using namespace std;
int main()
{
char name[100];
char ch = 'y';
while(ch=='y'||ch=='Y')
{
cout<<"Enter the name to be searched in format (LAST, FIRST) : ";
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();
cout<<"Search another student ? (y/n) : ";
cin>>ch;
getchar();
while(ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N')
{
cout<<"Invalid choice . Enter y/n :";
cin>>ch;
getchar();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.