In C++ Write a program that reads first names from a text file. Names do not con
ID: 3801327 • Letter: I
Question
In C++
Write a program that reads first names from a text file. Names do not contain spaces, and are separated with a newline character (one name per line). Prompt the user for the name of the text file. Once all names have been read, display on the console the name that would be first in alphabetical order, and the name that would be last in alphabetical order. USER STRING COMPARISONS < and >; DO NOT TRY TO IMPLEMENT A SORTING ROUTINE. DO NOT READ THE DATA INTO AN ARRAY. When testing your program, create a text file on your desktop, such as C:Desktop ames.txt. Remember the in bold text restrictions.
The output should be like example: First name: Anna Last name: Zach
Explanation / Answer
Code:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
string first,last,temp;
char file[20],path[200]="";
cout<<"Enter the file name"<<endl;
cin>>file;
strcat(path,"homepraveenDesktop");
strcat(path,file);
ifstream in(path);
in>>temp;
first=temp;
last=temp;
while(in>>temp)
{
if(temp<first)
first=temp;
else if(temp>last)
last=temp;
}
cout<<"Fitst name is : "<<first<<endl;
cout<<"last name is : "<<last<<endl;
}
Input(names.txt):
kumar
naz
kat
keti
kalyan
rajesh
aravind
purna
lakshmi
zaheer
ananth
anush
Output:
Fitst name is : ananth
last name is : zaheer
Note:
inorder to test this in windows change the path in the following line to path of desired input folder
ex:
strcat(path,"homepraveenDesktop");
strcat(path,"C:UserspraveenDesktop");