Need Help Two File Input and Output C Programmings Please He
Need Help Two File Input and Output C++ Programmings, Please Help me and teach me how to do those Programming.
Can you explain to me each step, so I understand how to do it. ( NO hand writing please)
Program 1
Create a file with notepad, called firstLastAge.txt.
Fred Smith 21
Tuyet Nguyen 18
Joseph Anderson 23
Annie Nelson 19
Julie Wong 17
Write program that
1) Open the firstLastAge.txt file
2) Read the FirstName, LastName and Age.
3) Write a loop to read each line, until eof()
4) Print out the read data
5) Close the file.
Program 2
Use the file firstLastAge.txt from program 1.
Read in the data and print out the name and age of the youngest person, and also the oldest person.
Solution
// C++ code read file and determine old and younger person
#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
using namespace std;
int main () {
string firstName;
string lastName;
int age;
string youngestFirstName;
string youngestLastName;
int youngestage = INT_MAX;
string oldestFirstName;
string oldestLastName;
int oldestage = INT_MIN;
// prgram 1
ifstream inputfile (\"firstLastAge.txt\");
if (inputfile.is_open())
{
while ( true )
{
inputfile >> firstName;
inputfile >> lastName;
inputfile >> age;
if(age < youngestage)
{
youngestage = age;
youngestLastName = lastName;
youngestFirstName = firstName;
}
if(age > oldestage)
{
oldestage = age;
oldestLastName = lastName;
oldestFirstName = firstName;
}
cout << \"Name: \" << firstName << \" \" << lastName << \", Age: \" << age << endl;
if(inputfile.eof())
break;
}
inputfile.close();
// program 2
cout << \"\ Youngest Person\ \";
cout << \"Name: \" << youngestFirstName << \" \" << youngestLastName << \", Age: \" << youngestage << endl;
cout << \"\ Oldest Person\ \";
cout << \"Name: \" << oldestFirstName << \" \" << oldestLastName << \", Age: \" << oldestage << endl;
}
else cout << \"Unable to open file\";
return 0;
}
/*
output:
Name: Fred Smith, Age: 21
Name: Tuyet Nguyen, Age: 18
Name: Joseph Anderson, Age: 23
Name: Annie Nelson, Age: 19
Name: Julie Wong, Age: 17
Youngest Person
Name: Julie Wong, Age: 17
Oldest Person
Name: Joseph Anderson, Age: 23
*/

