C++ program presentation. I have to write a detailed presentation about my code,
ID: 3779428 • 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
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.