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 forma.
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