86.9k views
4 votes
This program will store a roster of most popular videos with kittens. The roster can include at most 10 kittens.You will implement structures to handle kitten information. You will also use functions to manipulate the structure. (1) Create a structure kitten. The structure should contain the following attributes: name; string color; string score; integer Important! The name of the structure and each of its field must match exactly for the program to work and be graded correctly. (2) Create a struct roster. This struct should contain the following attributes: kittens: an array of struct kitten, with size 10 size; int. This variable will be updated based on the current number of kittens in the roster. In main(), create a roster called kittenRoster. This will store your kitten roster. (3) Implement function printMenu(). This function prints out a menu of options for the user to modify the roster. Each option is represented by a single character. Ex:

User SMshrimant
by
5.6k points

1 Answer

3 votes

Answer:

#include<iostream>

#include<string>

#include <fstream>

using namespace std;

struct kitten

{

string name,color;

int score;

};

struct roster

{

struct kitten kittens[10];

int size=0;

};

void printMenu()

{

cout<<"MENU\\"<<"a - Add kitten\\b - Delete kitten\\u - Update kitten color and cuteness score\\f - Find kitten\\l - Load kitten info from file\\s - Save kitten info to file\\o - Output roster\\q - Quit\\\\Choose an option : ";

}

int findKitten(string str, struct roster* ros)

{

for(int i=0; i<ros->size; i++)

{

if(ros->kittens[i].name == str)

{

cout<<"KittenName: "<<ros->kittens[i].name<<" Color: "<<ros->kittens[i].color<<" Score: "<<ros->kittens[i].score<<"\\";

return i;

}

}

return -1;

}

void addKitten(struct kitten* kit, struct roster* ros)

{

if(ros->size>=10)

{

cout<<"Impossible to add new kitten: roster is full\\";

return;

}

ros->size++;

ros->kittens[ros->size]=*kit;

cout<<"Successfully added new kitten to roster\\";

}

bool deleteKitten(string name,struct roster* ros)

{

int index = findKitten(name,ros);

if(index == -1)

return false;

while(index<ros->size-1)

{

ros->kittens[index] = ros->kittens[index+1];

}

ros->size--;

return true;

}

bool updateKitten(struct kitten* kit, struct roster* ros)

{

int index = findKitten(kit->name,ros);

if(index == -1)

{

return false;

}

ros->kittens[index] = *kit;

return true;

}

void printRoster(struct roster* ros)

{

cout<<"ROSTER\\";

for(int i=0; i<ros->size; i++)

{

cout<<"Kitten "<<i+1<<" -- Name: "<<ros->kittens[i].name<<", Color: "<<ros->kittens[i].color<<", Score "<<ros->kittens[i].score<<"\\";

}

}

void getKittenFromFile(string str, struct roster* ros)

{

ifstream myfile;

myfile.open(str.c_str());

if(!myfile)

{

cout<<"Error! File not found.\\";

return;

}

for(ros->size=0; myfile; ros->size++)

{

if(!myfile.eof())

{

myfile>>ros->kittens[ros->size].name;

myfile>>ros->kittens[ros->size].color;

myfile>>ros->kittens[ros->size].score;

}

else

break;

}

ros->size++;

}

void printToFile(string str, struct roster* ros)

{

ofstream myfile;

myfile.open(str.c_str());

myfile<<"ROSTER\\";

for(int i=0; i<ros->size; i++)

myfile<<"Kitten "<<i+1<<" -- Name: "<<ros->kittens[i].name<<", Color: "<<ros->kittens[i].color<<", Score "<<ros->kittens[i].score<<"\\";

myfile.close();

}

int main()

{

struct roster kittenRoster;

struct kitten kit;

int index;

char ch;

string name;

bool bol;

int loop =1;

while(loop)

{

printMenu();

cin>>ch;

switch(ch)

{

case 'a' :

cout<<"Enter a new kitten's name: ";

cin>>kit.name;

cout<<"Enter the kittne's color: ";

cin>>kit.color;

cout<<"Enter the kitten's cuteness score: ";

cin>>kit.score;

addKitten(&kit,&kittenRoster);

break;

case 'd' :

if (kittenRoster.size == 0)

{

cout<<"Cannot delete from empty roster.\\";

break;

}

cout<<"Enter kitten name to delete: ";

cin>>kit.name;

bol = deleteKitten(kit.name,&kittenRoster);

if(bol == false)

cout<<"Error! Kitten not found\\";

break;

case 'u' :

cout<<"Enter a kitten's name: ";

cin>>kit.name;

cout<<"Enter the new color: ";

cin>>kit.color;

cout<<"Enter the new score: ";

cin>>kit.score;

bol = updateKitten(&kit,&kittenRoster);

if(bol)

cout<<"Successfully updates kitten info\\";

else

cout<<"Cannot find kitten\\";

break;

case 'f' :

cin>>kit.name;

index = findKitten(kit.name,&kittenRoster);

if (index == -1)

cout<<"Kitten not found.\\";

break;

case 'l' :

cout<<"Enter the file name :";

cin>>name;

getKittenFromFile(name,&kittenRoster);

break;

case 's' :

cout<<"Enter the file name :" ;

cin>>name;

printToFile(name,&kittenRoster);

break;

case 'o' :

printRoster(&kittenRoster);

break;

case 'q' :

loop = 0;

break;

default:

cout<<"Invalid option. Please try again\\";

}

}

}

Step-by-step explanation:

  • Define a kitten structure with name, color and score properties.
  • Define a roster structure with kittens and size properties.
  • Call the printMenu method inside the main method.
User Aleksandrbel
by
4.6k points