SlideShare a Scribd company logo
Note: Modified code
code:
#include
#include
#include
#include
#include
using namespace std;
#pragma warning(disable: 4996)
typedef enum { male = 0, female = 1 } gender; // enumeration type gender
struct dog {
char name[30];
gender genderValue;
char breed[30];
int age;
float weight;
};
int count = 0; // the amount of dogs currently stored in the list (initialized at 0)
struct dog list[30]; // initialize list of dogs
// forward declaration of functions
void flush();
void branching(char);
void helper(char);
int add(char*, char*, char*, int, float, struct dog*); // 30 points
char* search(char*, int, struct dog*); // 10 points
void display();
void save(char* fileName);
void load(char* fileName); // 10 points
int main()
{
load("Dog_List.txt"); // load list of dogs from file (if it exists)
char ch = 'i';
printf("Assignment 5: Array of Structs and Enum Types  ");
printf("Dog Adoption Center  ");
do
{
printf("Please enter your selection: ");
printf("ta: add a new dog to the list ");
printf("ts: search for a dog on the list ");
printf("td: display list of dogs ");
printf("tq: quit and save your list ");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
save("Dog_List.txt"); // save list of dogs to file (overwrite if it exists)
return 0;
}
// consume leftover ' ' characters
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case 'a':
case 's': helper(c); break;
case 'd': display(); break;
case 'q': break;
default: printf("Invalid input! ");
}
}
// The helper function is used to determine how much information is needed and which function
to send that information to.
// It uses values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it
works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void helper(char c)
{
char input[100];
if (c == 'a')
{
printf(" Please enter the dog's information in the following format: ");
printf("tname:gender:breed:age:weight ");
fgets(input, sizeof(input), stdin);
// discard ' ' chars attached to input
input[strlen(input) - 1] = '0';
char* name = strtok(input, ":"); // strtok used to parse string
char* genderValueString = strtok(NULL, ":");
char* breed = strtok(NULL, ":");
int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int
float weight = atof(strtok(NULL, ":")); // atof used to convert string to float
int result = add(name, genderValueString, breed, age, weight, list);
if (result == 0)
printf(" That dog is already on the list  ");
else
printf(" Dog added to list successfully  ");
}
else // c = 's'
{
printf(" Please enter the dog's information in the following format: ");
printf("tname:age ");
fgets(input, sizeof(input), stdin);
char* name = strtok(input, ":"); // strtok used to parse string
int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int
char* result = search(name, age, list);
if (result == NULL)
printf(" That dog is not on the list  ");
else
printf(" Breed: %s  ", result);
}
}
// Q1 : add (30 points)
// This function is used to insert a new dog into the list.
// Your list should be sorted alphabetically by name, so you need to search for the correct index
to add into your list.
// If a dog already exists with the same name, then those dogs should be sorted by age.
// Do not allow for the same dog to be added to the list multiple times. (same name and same
age).
// If the dog already exists on the list, return 0. If the dog is added to the list, return 1.
//
// NOTE: You must convert the string "genderValueString to an enum type and store it in the
list. This will be tested.
// (You must store all of the required information correctly to pass all of the test cases)
// NOTE: You should not allow for the same dog to be added twice, you will lose points if you
do not account for this.
// (That means that dogs on the list are allowed to have the same name OR the same age, but not
both).
//
// You are not required to use pointer operations for your list but you may do so if you'd like.
// 'list' is passed to this function for automated testing purposes only, it is global.
int add(char* name, char* genderValueString, char* breed, int age, float weight, struct dog* list)
{
struct dog* temp;
gender gen;
char* test="M";
int i;
if(count>0)
{
for(i=0;i
Solution
Note: Modified code
code:
#include
#include
#include
#include
#include
using namespace std;
#pragma warning(disable: 4996)
typedef enum { male = 0, female = 1 } gender; // enumeration type gender
struct dog {
char name[30];
gender genderValue;
char breed[30];
int age;
float weight;
};
int count = 0; // the amount of dogs currently stored in the list (initialized at 0)
struct dog list[30]; // initialize list of dogs
// forward declaration of functions
void flush();
void branching(char);
void helper(char);
int add(char*, char*, char*, int, float, struct dog*); // 30 points
char* search(char*, int, struct dog*); // 10 points
void display();
void save(char* fileName);
void load(char* fileName); // 10 points
int main()
{
load("Dog_List.txt"); // load list of dogs from file (if it exists)
char ch = 'i';
printf("Assignment 5: Array of Structs and Enum Types  ");
printf("Dog Adoption Center  ");
do
{
printf("Please enter your selection: ");
printf("ta: add a new dog to the list ");
printf("ts: search for a dog on the list ");
printf("td: display list of dogs ");
printf("tq: quit and save your list ");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
save("Dog_List.txt"); // save list of dogs to file (overwrite if it exists)
return 0;
}
// consume leftover ' ' characters
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
// branch to different tasks
void branching(char c)
{
switch (c)
{
case 'a':
case 's': helper(c); break;
case 'd': display(); break;
case 'q': break;
default: printf("Invalid input! ");
}
}
// The helper function is used to determine how much information is needed and which function
to send that information to.
// It uses values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it
works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void helper(char c)
{
char input[100];
if (c == 'a')
{
printf(" Please enter the dog's information in the following format: ");
printf("tname:gender:breed:age:weight ");
fgets(input, sizeof(input), stdin);
// discard ' ' chars attached to input
input[strlen(input) - 1] = '0';
char* name = strtok(input, ":"); // strtok used to parse string
char* genderValueString = strtok(NULL, ":");
char* breed = strtok(NULL, ":");
int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int
float weight = atof(strtok(NULL, ":")); // atof used to convert string to float
int result = add(name, genderValueString, breed, age, weight, list);
if (result == 0)
printf(" That dog is already on the list  ");
else
printf(" Dog added to list successfully  ");
}
else // c = 's'
{
printf(" Please enter the dog's information in the following format: ");
printf("tname:age ");
fgets(input, sizeof(input), stdin);
char* name = strtok(input, ":"); // strtok used to parse string
int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int
char* result = search(name, age, list);
if (result == NULL)
printf(" That dog is not on the list  ");
else
printf(" Breed: %s  ", result);
}
}
// Q1 : add (30 points)
// This function is used to insert a new dog into the list.
// Your list should be sorted alphabetically by name, so you need to search for the correct index
to add into your list.
// If a dog already exists with the same name, then those dogs should be sorted by age.
// Do not allow for the same dog to be added to the list multiple times. (same name and same
age).
// If the dog already exists on the list, return 0. If the dog is added to the list, return 1.
//
// NOTE: You must convert the string "genderValueString to an enum type and store it in the
list. This will be tested.
// (You must store all of the required information correctly to pass all of the test cases)
// NOTE: You should not allow for the same dog to be added twice, you will lose points if you
do not account for this.
// (That means that dogs on the list are allowed to have the same name OR the same age, but not
both).
//
// You are not required to use pointer operations for your list but you may do so if you'd like.
// 'list' is passed to this function for automated testing purposes only, it is global.
int add(char* name, char* genderValueString, char* breed, int age, float weight, struct dog* list)
{
struct dog* temp;
gender gen;
char* test="M";
int i;
if(count>0)
{
for(i=0;i

More Related Content

PDF
READ BEFORE YOU START Please read the given Word document fo.pdf
PDF
Can someoen help me to write c++ program including C++ inheritance, .pdf
PDF
READ BEFORE YOU START You are given a partially completed pr.pdf
PDF
My Program below this is supposed to do the following below here but.pdf
PDF
Program In C You are required to write an interactive C program that.pdf
PDF
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
PDF
This code i would want to work ssh compiling for the this ta.pdf
DOCX
compiler_yogesh lab manual graphic era hill university.docx
READ BEFORE YOU START Please read the given Word document fo.pdf
Can someoen help me to write c++ program including C++ inheritance, .pdf
READ BEFORE YOU START You are given a partially completed pr.pdf
My Program below this is supposed to do the following below here but.pdf
Program In C You are required to write an interactive C program that.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
This code i would want to work ssh compiling for the this ta.pdf
compiler_yogesh lab manual graphic era hill university.docx

Similar to Note Modified codecode#includeiostream #include stdio.h.pdf (20)

PDF
DS LAB MANUAL.pdf
DOCX
Cs pritical file
PPTX
5. chapter iv
PDF
C Programming Project
DOCX
CS 107 – Introduction to Computing and Programming – Spring 20.docx
DOCX
PDF
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
DOCX
Application Animal Characteristics In today’.docx
DOCX
Zoo management adri jovin
DOCX
BESTest
DOCX
Design problem
PDF
Basic C Programming Lab Practice
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
PDF
C language concept with code apna college.pdf
DOCX
Wildlife conservation project management adri jovin
DOC
Complier File
PDF
In this project you will define some interfaces, abstract classes, a.pdf
DOC
Student record
TXT
System programs in C language.
DS LAB MANUAL.pdf
Cs pritical file
5. chapter iv
C Programming Project
CS 107 – Introduction to Computing and Programming – Spring 20.docx
#includestdio.h#includestring.h#includestdlib.h#define M.pdf
Application Animal Characteristics In today’.docx
Zoo management adri jovin
BESTest
Design problem
Basic C Programming Lab Practice
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
C language concept with code apna college.pdf
Wildlife conservation project management adri jovin
Complier File
In this project you will define some interfaces, abstract classes, a.pdf
Student record
System programs in C language.

More from sharnapiyush773 (20)

PDF
“According to new research in the Journal of Research in Personality.pdf
PDF
Trueas each officer has fixed paroleelesratio = no of parollesn.pdf
PDF
to upperSolutionto upper.pdf
PDF
there are several theory on defining acid and base, Lewis acids and .pdf
PDF
The Statement is False. As there are many applications of hyperbo;ic.pdf
PDF
The Sarbanes-Oxley , 2002 contains following provisions which determ.pdf
PDF
The main motivation behind software reuse is to avoid wastage of tim.pdf
PDF
The expression evaluation is compiler dependent, and may vary. A g.pdf
PDF
public class Storm {   Attributes    private String stormName;.pdf
PDF
picture is missingSolutionpicture is missing.pdf
PDF
package employeeType.employee;public class Employee {    private.pdf
PDF
OSI (Open Systems Interconnection) is reference model for how applic.pdf
PDF
Oligotrophic area Usually, an olidgotropic organism is a one that .pdf
PDF
n = N 2^(rt)n = no of people at a given timeN = initial populat.pdf
PDF
Maroochy Shire Sewage Spill Case Study Student’s Name Institution Af.pdf
PDF
It lacks the origin of replication which is most important for the i.pdf
PDF
In developmental biology, an embryo is divided into two hemispheres.pdf
PDF
Bryophytes- Development of primitive vasculature for water transport.pdf
PDF
givenSolutiongiven.pdf
PDF
Ethernet II framing (also known as DIX Ethernet, named after DEC, In.pdf
“According to new research in the Journal of Research in Personality.pdf
Trueas each officer has fixed paroleelesratio = no of parollesn.pdf
to upperSolutionto upper.pdf
there are several theory on defining acid and base, Lewis acids and .pdf
The Statement is False. As there are many applications of hyperbo;ic.pdf
The Sarbanes-Oxley , 2002 contains following provisions which determ.pdf
The main motivation behind software reuse is to avoid wastage of tim.pdf
The expression evaluation is compiler dependent, and may vary. A g.pdf
public class Storm {   Attributes    private String stormName;.pdf
picture is missingSolutionpicture is missing.pdf
package employeeType.employee;public class Employee {    private.pdf
OSI (Open Systems Interconnection) is reference model for how applic.pdf
Oligotrophic area Usually, an olidgotropic organism is a one that .pdf
n = N 2^(rt)n = no of people at a given timeN = initial populat.pdf
Maroochy Shire Sewage Spill Case Study Student’s Name Institution Af.pdf
It lacks the origin of replication which is most important for the i.pdf
In developmental biology, an embryo is divided into two hemispheres.pdf
Bryophytes- Development of primitive vasculature for water transport.pdf
givenSolutiongiven.pdf
Ethernet II framing (also known as DIX Ethernet, named after DEC, In.pdf

Recently uploaded (20)

PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
GDM (1) (1).pptx small presentation for students
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O7-L3 Supply Chain Operations - ICLT Program
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
master seminar digital applications in india
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
2.FourierTransform-ShortQuestionswithAnswers.pdf

Note Modified codecode#includeiostream #include stdio.h.pdf

  • 1. Note: Modified code code: #include #include #include #include #include using namespace std; #pragma warning(disable: 4996) typedef enum { male = 0, female = 1 } gender; // enumeration type gender struct dog { char name[30]; gender genderValue; char breed[30]; int age; float weight; }; int count = 0; // the amount of dogs currently stored in the list (initialized at 0) struct dog list[30]; // initialize list of dogs // forward declaration of functions void flush(); void branching(char); void helper(char); int add(char*, char*, char*, int, float, struct dog*); // 30 points char* search(char*, int, struct dog*); // 10 points void display(); void save(char* fileName); void load(char* fileName); // 10 points int main() { load("Dog_List.txt"); // load list of dogs from file (if it exists) char ch = 'i'; printf("Assignment 5: Array of Structs and Enum Types "); printf("Dog Adoption Center "); do
  • 2. { printf("Please enter your selection: "); printf("ta: add a new dog to the list "); printf("ts: search for a dog on the list "); printf("td: display list of dogs "); printf("tq: quit and save your list "); ch = tolower(getchar()); flush(); branching(ch); } while (ch != 'q'); save("Dog_List.txt"); // save list of dogs to file (overwrite if it exists) return 0; } // consume leftover ' ' characters void flush() { int c; do c = getchar(); while (c != ' ' && c != EOF); } // branch to different tasks void branching(char c) { switch (c) { case 'a': case 's': helper(c); break; case 'd': display(); break; case 'q': break; default: printf("Invalid input! "); } } // The helper function is used to determine how much information is needed and which function to send that information to. // It uses values that are returned from some functions to produce the correct ouput. // There is no implementation needed here, but you should study this function and know how it works.
  • 3. // It is always helpful to understand how the code works before implementing new features. // Do not change anything in this function or you risk failing the automated test cases. void helper(char c) { char input[100]; if (c == 'a') { printf(" Please enter the dog's information in the following format: "); printf("tname:gender:breed:age:weight "); fgets(input, sizeof(input), stdin); // discard ' ' chars attached to input input[strlen(input) - 1] = '0'; char* name = strtok(input, ":"); // strtok used to parse string char* genderValueString = strtok(NULL, ":"); char* breed = strtok(NULL, ":"); int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int float weight = atof(strtok(NULL, ":")); // atof used to convert string to float int result = add(name, genderValueString, breed, age, weight, list); if (result == 0) printf(" That dog is already on the list "); else printf(" Dog added to list successfully "); } else // c = 's' { printf(" Please enter the dog's information in the following format: "); printf("tname:age "); fgets(input, sizeof(input), stdin); char* name = strtok(input, ":"); // strtok used to parse string int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int char* result = search(name, age, list); if (result == NULL) printf(" That dog is not on the list "); else printf(" Breed: %s ", result); }
  • 4. } // Q1 : add (30 points) // This function is used to insert a new dog into the list. // Your list should be sorted alphabetically by name, so you need to search for the correct index to add into your list. // If a dog already exists with the same name, then those dogs should be sorted by age. // Do not allow for the same dog to be added to the list multiple times. (same name and same age). // If the dog already exists on the list, return 0. If the dog is added to the list, return 1. // // NOTE: You must convert the string "genderValueString to an enum type and store it in the list. This will be tested. // (You must store all of the required information correctly to pass all of the test cases) // NOTE: You should not allow for the same dog to be added twice, you will lose points if you do not account for this. // (That means that dogs on the list are allowed to have the same name OR the same age, but not both). // // You are not required to use pointer operations for your list but you may do so if you'd like. // 'list' is passed to this function for automated testing purposes only, it is global. int add(char* name, char* genderValueString, char* breed, int age, float weight, struct dog* list) { struct dog* temp; gender gen; char* test="M"; int i; if(count>0) { for(i=0;i Solution Note: Modified code code: #include #include #include
  • 5. #include #include using namespace std; #pragma warning(disable: 4996) typedef enum { male = 0, female = 1 } gender; // enumeration type gender struct dog { char name[30]; gender genderValue; char breed[30]; int age; float weight; }; int count = 0; // the amount of dogs currently stored in the list (initialized at 0) struct dog list[30]; // initialize list of dogs // forward declaration of functions void flush(); void branching(char); void helper(char); int add(char*, char*, char*, int, float, struct dog*); // 30 points char* search(char*, int, struct dog*); // 10 points void display(); void save(char* fileName); void load(char* fileName); // 10 points int main() { load("Dog_List.txt"); // load list of dogs from file (if it exists) char ch = 'i'; printf("Assignment 5: Array of Structs and Enum Types "); printf("Dog Adoption Center "); do { printf("Please enter your selection: "); printf("ta: add a new dog to the list "); printf("ts: search for a dog on the list "); printf("td: display list of dogs "); printf("tq: quit and save your list ");
  • 6. ch = tolower(getchar()); flush(); branching(ch); } while (ch != 'q'); save("Dog_List.txt"); // save list of dogs to file (overwrite if it exists) return 0; } // consume leftover ' ' characters void flush() { int c; do c = getchar(); while (c != ' ' && c != EOF); } // branch to different tasks void branching(char c) { switch (c) { case 'a': case 's': helper(c); break; case 'd': display(); break; case 'q': break; default: printf("Invalid input! "); } } // The helper function is used to determine how much information is needed and which function to send that information to. // It uses values that are returned from some functions to produce the correct ouput. // There is no implementation needed here, but you should study this function and know how it works. // It is always helpful to understand how the code works before implementing new features. // Do not change anything in this function or you risk failing the automated test cases. void helper(char c) { char input[100]; if (c == 'a')
  • 7. { printf(" Please enter the dog's information in the following format: "); printf("tname:gender:breed:age:weight "); fgets(input, sizeof(input), stdin); // discard ' ' chars attached to input input[strlen(input) - 1] = '0'; char* name = strtok(input, ":"); // strtok used to parse string char* genderValueString = strtok(NULL, ":"); char* breed = strtok(NULL, ":"); int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int float weight = atof(strtok(NULL, ":")); // atof used to convert string to float int result = add(name, genderValueString, breed, age, weight, list); if (result == 0) printf(" That dog is already on the list "); else printf(" Dog added to list successfully "); } else // c = 's' { printf(" Please enter the dog's information in the following format: "); printf("tname:age "); fgets(input, sizeof(input), stdin); char* name = strtok(input, ":"); // strtok used to parse string int age = atoi(strtok(NULL, ":")); // atoi used to convert string to int char* result = search(name, age, list); if (result == NULL) printf(" That dog is not on the list "); else printf(" Breed: %s ", result); } } // Q1 : add (30 points) // This function is used to insert a new dog into the list. // Your list should be sorted alphabetically by name, so you need to search for the correct index to add into your list. // If a dog already exists with the same name, then those dogs should be sorted by age.
  • 8. // Do not allow for the same dog to be added to the list multiple times. (same name and same age). // If the dog already exists on the list, return 0. If the dog is added to the list, return 1. // // NOTE: You must convert the string "genderValueString to an enum type and store it in the list. This will be tested. // (You must store all of the required information correctly to pass all of the test cases) // NOTE: You should not allow for the same dog to be added twice, you will lose points if you do not account for this. // (That means that dogs on the list are allowed to have the same name OR the same age, but not both). // // You are not required to use pointer operations for your list but you may do so if you'd like. // 'list' is passed to this function for automated testing purposes only, it is global. int add(char* name, char* genderValueString, char* breed, int age, float weight, struct dog* list) { struct dog* temp; gender gen; char* test="M"; int i; if(count>0) { for(i=0;i