Write a program that ask the user for a number from 1 to 5, then the number give
ID: 3765621 • Letter: W
Question
Write a program that ask the user for a number from 1 to 5, then the number given will represent the number of names that the user has to input. The names are composed of first and last name, and could also include a middle name. You might request the user to write the names beginning with the last name. Then all those names have to be written down to a file in alphabetical order based on the last names first. Besides the “main()” you have to use at least other two functions. Hint: you might use a function to swap the strings
Please be sure that the names are written in ALPHABETICAL ORDER to the file. The program must be able to read last name, firstname and middle name if necessary. PLease use c++ and basic commands, this is for an introduction to computing class. I posted this question twice but none of the previous answers works.
Explanation / Answer
#include<iostream.h>
#include<fstream.h>
void fileins(char l[], char f[], char m[])
{
ofstream out;
out.open("details.txt",ios::app); //this statement will append data in the file due to ios::app
out<<l<<" "<<f<<" "<<m<<endl;
out.close();
}
void main()
{
char last[20],first[20],mid[20];
int n,i;
cout<<"Enter number between 1 to 5";
cin>>n;
ofstream out;
for(i=0;i<n;i++)
{
cout<<"Enter Last name";
cin>>last;
cout<<"Enter First name";
cin>>first;
cout<<"Enter Middle name";
cin>>mid;
fileins(last,first,mid);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.