Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program presentation. I have to write a detailed presentation about my code,

ID: 3779518 • Letter: C

Question

C++ program presentation.

I have to write a detailed presentation about my code, what it does, and explain each function and section of code. I am absolutely terrible with my words and explaining things in a way that other people will understand, so I'm going to post a brief description of what my program does, and all it's code. Please help me come up with a presentation that I can use, and edit as needed. I am currently working on the trial and error experience part of my presentation since obviously no one knows that part... but please help me with the rest.

I have come up with a pet adoption program that allows the user to select a cat or dog, and then it displays five options for each species depending on the users selection. After they have read the stats (pulled from a saved text file for each pet), the user then has the option to accept or decline the pet. If accepted, the program will print out the User's information gathered in the beginning, and all the selected pets info on to an out file (application.txt).

Here's how the animal stats in my text file looks:

Name: Aimee
Sex: Female
Age: 3 Months
Color: Black with white chest
Temperment: Hyper
Hair: Short
Fixed: No

And here's the code:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;
bool is_valid_name(string name){
char ch; //make sure user can't enter above 20 character or anything other than letters
   if(name.length() > 20) return false;
   for(int i = 0; i < name.length(); ++i){
       ch = name[i];
   if(!((ch >= 'a' && ch <= 'z') || (ch> = 'A' && ch <= 'Z'))){
   return false;
       }
   }
   return true;
}
bool is_valid_phone_number(string number){
char ch; //make sure user can only enter numbers, and no more than 10
   if(number.length() != 10) return false;
   for(int i = 0; i < number.length(); ++i){
       ch = number[i];
   if(!(ch >= '0' && ch <= '9')){
   return false;
       }
   }
   return true;
}
void get_details(string &fN, string &lN, string& pN){
   cout << "Enter first name: ";
   cin >> fN;
   while(!is_valid_name(fN)){
       cout << "Invalid name. Try again: ";
       cin >> fN;
}
   cout << "Enter last name: ";
   cin >> lN;
   while(!is_valid_name(lN)){
       cout << "Invalid name. Try again: ";
       cin >> lN;
}  
   cout << "Enter phone number: ";
   cin >> pN;
   while(!is_valid_phone_number(pN)){
       cout << "Invalid Phone. Try again: ";
       cin >> pN;
}
//create number format when printed at end
pN = "(" + pN.substr(0, 3) + ")" + pN.substr(3, 3) + "-" + pN.substr(6, 4);
}
void get_details(char *file, string *animal){ //creating a function get_details to read details from file.Passing file name and animal array as parameters.
   string input;
   int i=0;
   //Open file
   std::ifstream inFile;
   inFile.open(file);
   while ( getline (inFile, input))
   {
       //Get input
       animal[i] = input; //storing input to animal array.
       i = i+1;
       //Print input
       //put all info in order to look like the text file
   }
   cout <<animal[0] << " " << animal[1] << " "<< animal[2] << " "<<animal[3] << " "<< animal[4] << " "<< animal[5] << " "<< animal[6] << " "<< endl; //printing details on screen.
   //Close file
   inFile.close();
}
void get_animal(string *animal){ //receiving animal array as input.


   cout << " 1. Cat" << endl;
   cout << " 2. Dog" << endl;
   cout << "Which animal do you want? ";
   int choice;
   char exit;
   char opt;
   cin >> choice;
   while(choice < 1 || choice > 2){
       cout << "Invalid choice. Try again: ";
       cin >> choice;
}
if(choice == 1)
{
do{ //do while to loop till user seletcs a pet.
    cout << " 1. Aimee - Kitten" << endl;
   cout << " 2. Dana - Kitten" << endl;
   cout << " 3. Mike - Youth" << endl;
   cout << " 4. Carl - Adult" << endl;
   cout << " 5. Candy - Adult" << endl;
   cout << " 6. Exit" << endl;
   cout << "Enter your choice: ";
  
do{
   cin >> choice;

// below will pull from my input file and display all stats
   switch(choice) //converted the if-else-if to switch case.
   {
           case 1:
               get_details((char *)"cat1.txt",animal); //calling get_details function to read inputs from the filename passed and store it in animal array.
               break;
           case 2:
               get_details((char *)"cat2.txt",animal);
               break;
           case 3:
               get_details((char *)"cat3.txt",animal);
               break;
           case 4:
               get_details((char *)"cat4.txt",animal);
               break;
           case 5:
               get_details((char *)"cat5.txt",animal);
               break;
            case 6:
               return;
           default :
               cout << "Invalid Option.Try Again : ";
   }
   }while(choice < 1 || choice > 5);
   cout << "Is this the animal you'd like to adopt? (Y/N) : ";
   cin >> opt;
}while(opt == 'N' || opt == 'n');
}
else{
   do {
//dog area
   cout << " 1. Maggie - Puppy" << endl;
   cout << " 2. Tina - Puppy" << endl;
   cout << " 3. Rover - Youth" << endl;
   cout << " 4. Buck - Adult" << endl;
   cout << " 5. Daisy - Adult" << endl;
   cout << " 6. Exit" << endl;
   cout << "Enter your choice: ";
   do {
    cin >> choice;
// below will pull from my input file and display all stats
   switch(choice)
   {
           case 1:
               get_details((char *)"dog1.txt",animal);
               break;
           case 2:
               get_details((char *)"dog2.txt",animal);
               break;
           case 3:
               get_details((char *)"dog3.txt",animal);
               break;
           case 4:
               get_details((char *)"dog4.txt",animal);
               break;
           case 5:
               get_details((char *)"dog5.txt",animal);
               break;
           case 6:
               return;
           default :
               cout << "Invalid Option.Try Again : ";
   }
   }while(choice < 1 || choice > 5);
       cout << "Is this the animal you'd like to adopt? (Y/N) : ";
   cin >> opt;
}while(opt == 'N' || opt == 'n');
}
}
//get all stats to print to a output file when submission is done
void print_details(string fN, string lN, string pN, string *animal) //reciving animal array in a pointer
{
   ofstream outFile;
   outFile.open("application.txt");
   outFile << "Name: "<< fN << " "<< lN << " Phone Number: " << pN << " Animal Details "<<animal[0] << " "<<animal[1]<<" "<<animal[2]<<" "<<animal[3]<<" "<<animal[4]<<" "<<animal[5]<<" "<<animal[6]<< endl;
   outFile.close();
}

int main(){
string fN, lN, pN, animal[7]; //creating animal array to store allinputs from file
get_details(fN, lN, pN);
get_animal(animal);
print_details(fN, lN, pN, animal);
return 0;
}

Explanation / Answer

also added comment to the code.

Functions :
   a) is_valid_name (string name) : This function is taking string as input and is for checking the validity of user's name in the input. Monitoring if the user is not typing more than 20 characters and also ensuring if he is not typing digit or symbol in name.

   b) is_valid_phone_number (string number) : This function is taking string as input and is for checking the validity of user's phone number in the input. Monitoring if the user is not typing more than 10 digits and also ensuring if input is digit.

   c) get_details(string &fN, string &lN, string& pN) : This function is taking the 2 argument first name , second name and phone number. In this fuction is we are storing the user input and also validating if the user input is correct.

   d) get_details(char *file, string *animal) : This function is taking 2 arguments char file and string animal. This funtion takes the files details containing the details of the animals and showing that to the user. Also inside this function it is asked to users it details and then type of animal he likes and later showing the details of the breed of animal he choose and ask if he want to buy it or not.

code :

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>


using namespace std;

/*This function is validating the name entered by the user is characters*/
bool is_valid_name(string name){
char ch; //make sure user can't enter above 20 character or anything other than letters
if(name.length() > 20) return false;
for(int i = 0; i < name.length(); ++i){
ch = name[i];
// checking if it is a characters and if its not return false
if(!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))){
return false;
}
}
return true;
}
/*This function is validating the phone number entered by the user is digits*/
bool is_valid_phone_number(string number){
char ch; //make sure user can only enter numbers, and no more than 10
if(number.length() != 10) return false;
for(int i = 0; i < number.length(); ++i){
ch = number[i];
// condition to check if number is digit
if(!(ch >= '0' && ch <= '9')){
return false;
}
}
return true;
}

/*This function is taking the details of the user and also validating the data*/
void get_details(string &fN, string &lN, string& pN){
// ask user to enter the first name
cout << "Enter first name: ";
cin >> fN;
//check the validity of the first name
while(!is_valid_name(fN)){
cout << "Invalid name. Try again: ";
cin >> fN;
}
// ask user to enter the last name
cout << "Enter last name: ";
cin >> lN;
//check the validity of the last name
while(!is_valid_name(lN)){
cout << "Invalid name. Try again: ";
cin >> lN;
}
// ask user to enter the phone number
cout << "Enter phone number: ";
cin >> pN;
//check the validity of the phone number
while(!is_valid_phone_number(pN)){
cout << "Invalid Phone. Try again: ";
cin >> pN;
}
//create number format when printed at end
pN = "(" + pN.substr(0, 3) + ")" + pN.substr(3, 3) + "-" + pN.substr(6, 4);
}

/*this function is getting details of the animal from the file*/
void get_details(char *file, string *animal){ //creating a function get_details to read details from file.Passing file name and animal array as parameters.
string input;
int i=0;
//Open file
std::ifstream inFile;
inFile.open(file);
while ( getline (inFile, input))
{
//Get input
animal[i] = input; //storing input to animal array.
i = i+1;
//Print input
//put all info in order to look like the text file
}
cout <<animal[0] << " " << animal[1] << " "<< animal[2] << " "<<animal[3] << " "<< animal[4] << " "<< animal[5] << " "<< animal[6] << " "<< endl; //printing details on screen.
//Close file
inFile.close();
}
void get_animal(string *animal){ //receiving animal array as input.
// asking user to choose the animal Type
cout << " 1. Cat" << endl;
cout << " 2. Dog" << endl;
cout << "Which animal do you want? ";
int choice;
char exit;
char opt;
cin >> choice;
// check if the user's inout is 1 or 2
while(choice < 1 || choice > 2){
cout << "Invalid choice. Try again: ";
cin >> choice;
}
// if the user likes cat
if(choice == 1)
{
do{ //do while to loop till user seletcs a pet.
cout << " 1. Aimee - Kitten" << endl;
cout << " 2. Dana - Kitten" << endl;
cout << " 3. Mike - Youth" << endl;
cout << " 4. Carl - Adult" << endl;
cout << " 5. Candy - Adult" << endl;
cout << " 6. Exit" << endl;
cout << "Enter your choice: ";

do{
cin >> choice;

// below will pull from my input file and display all stats
switch(choice) //converted the if-else-if to switch case.
{
case 1:
get_details((char *)"cat1.txt",animal); //calling get_details function to read inputs from the filename passed and store it in animal array.
break;
case 2:
get_details((char *)"cat2.txt",animal);
break;
case 3:
get_details((char *)"cat3.txt",animal);
break;
case 4:
get_details((char *)"cat4.txt",animal);
break;
case 5:
get_details((char *)"cat5.txt",animal);
break;
case 6:
return;
default :
cout << "Invalid Option.Try Again : ";
}
}while(choice < 1 || choice > 5);
// ask user if he wants to adopt it
cout << "Is this the animal you'd like to adopt? (Y/N) : ";
cin >> opt;
}while(opt == 'N' || opt == 'n'); // asking user again if he is not adopting the pet
}
else{ // if the user likes dog
do {
//dog area
cout << " 1. Maggie - Puppy" << endl;
cout << " 2. Tina - Puppy" << endl;
cout << " 3. Rover - Youth" << endl;
cout << " 4. Buck - Adult" << endl;
cout << " 5. Daisy - Adult" << endl;
cout << " 6. Exit" << endl;
cout << "Enter your choice: ";
do {
cin >> choice;
// below will pull from my input file and display all stats
switch(choice)
{
case 1:
get_details((char *)"dog1.txt",animal);
break;
case 2:
get_details((char *)"dog2.txt",animal);
break;
case 3:
get_details((char *)"dog3.txt",animal);
break;
case 4:
get_details((char *)"dog4.txt",animal);
break;
case 5:
get_details((char *)"dog5.txt",animal);
break;
case 6:
return;
default :
cout << "Invalid Option.Try Again : ";
}
}while(choice < 1 || choice > 5);
// ask user if he wants to adopt it
cout << "Is this the animal you'd like to adopt? (Y/N) : ";
cin >> opt;
}while(opt == 'N' || opt == 'n'); // asking user again if he is not adopting the pet
}
}
//get all stats to print to a output file when submission is done
void print_details(string fN, string lN, string pN, string *animal) //reciving animal array in a pointer
{
ofstream outFile;
outFile.open("application.txt");
outFile << "Name: "<< fN << " "<< lN << " Phone Number: " << pN << " Animal Details "<<animal[0] << " "<<animal[1]<<" "<<animal[2]<<" "<<animal[3]<<" "<<animal[4]<<" "<<animal[5]<<" "<<animal[6]<< endl;
outFile.close();
}
int main(){
string fN, lN, pN, animal[7]; //creating animal array to store allinputs from file
get_details(fN, lN, pN);
get_animal(animal);
print_details(fN, lN, pN, animal);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote