Answers for "paint by numbers"

1

PntIDNumbers

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#define MAX 200
using namespace std;
// Function receive address of the first element of an array
// Reads file contents and stores ID in the pointer
// Returns number of ID's read from the file using pass by reference
void readFromFile(string *idnumbers, int &counter)
{
// Opens the file for reading
ifstream readF;
readF.open("idnumbers.txt");
// Checks if the file unable to open for reading display's error message and stop
if(!readF)
{
cout<<"n ERROR: Unable to open the file idnumbers.txt for reading.";
exit(0);
}// End of if condition
// Loops till end of the file
while(!readF.eof())
{
// Reads a ID from the file and stores in the pointer
readF>>*idnumbers;
// Increase the pointer by one to point to next address
idnumbers++;
// Increase the ID counter by one
counter++;
}// End of while loop
// Closer the file
readF.close();
}// End of function
// Function to display all the ID
void displayIDnumbers(string *idnumbers, int len)
{
cout<<"n List of ID numbers: n";
// Loops till number of ID
for(int c = 0; c < len; c++)
// Displays current ID
cout<<" "<<idnumbers[c]<<endl;
}// End of function
// Function to extract year of birth from ID and displays it
void displayYearsBorn(string *idnumbers, int len)
{
// To store integer version of year
int numYear;
cout<<"n List of years born: ";
// Loops till number of ID
for(int c = 0; c < len; c++)
{
// To store current date of birth
string dob = "";
// Extracts first two characters from the ID as year
string year = idnumbers[c].substr(1, 2);
// Converts the string year to integer
stringstream convert(year);
convert >> numYear;
// Checks if year is greater than 20
if(numYear > 20)
// Concatenates "19" before the year
dob = "19" + year;
// Otherwise year is greater then 20
else
// Concatenates "20" before the year
dob = "20" + year;
// Displays the date of birth
cout<<"n Year born: "<<dob;
}// End of for loop
}// End of function
// Function to extract month of birth from ID and displays it
void displayMonthsBorn(string *idnumbers, int len)
{
cout<<"nn List of months born: ";
// Loops till number of ID
for(int c = 0; c < len; c++)
{
// Extracts two characters from third index position from the ID as month
string month = idnumbers[c].substr(3, 2);
// Checks if first character is '0'
if(month.at(0) == '0')
// Stores the first character of the month
month = month[1];
// Displays the date of birth
cout<<"n Month born: "<<month;
}// End of for loop
}// End of function
// main function definition
int main()
{
// Creates an array of type string of size MAX to store ID
string arrIDs[MAX];
// To store number of records
int len = 0;
// Calls the function to read file contents
readFromFile(&arrIDs[0], len);
// Calls the function to display all ID
displayIDnumbers(&arrIDs[0], len);
// Calls the function to extract year of birth from ID and displays it
displayYearsBorn(&arrIDs[0], len);
// Calls the function to extract month of birth from ID and displays it
displayMonthsBorn(&arrIDs[0], len);
return 0;
}// End of main function
Posted by: Guest on November-03-2020

Browse Popular Code Answers by Language