Question

This program will store a roster of most popular videos with kittens. The roster can include...

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:

MENU
a - Add kitten
d - 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:

After creating a roster from file, the program outputs the menu. The program will continue to output the menu, until the user chooses the option Quit. If user chooses an option that doesn't exist, display error message

Invalid option. Please try again.

You can safely assume that user will only enter lower-case letters for option.

(4) Implement a function findKitten(). This function takes two arguments: a string name, and a roster struct. If the kitten's name is in the roster, return the position of the kitten in roster. Otherwise, return -1.

(5) Implement a function addKitten(). This function takes two arguments: a kitten struct and a roster struct. If the roster is full, display error message

Impossible to add new kitten: roster is full.

Otherwise, it will add the new kitten to the end of the roster and display the message. The size of the roster will be updated accordingly.

Successfully added new kitten to roster.

(6) Implement function deleteKitten(). This function takes string name and a roster struct as arguments. It deletes kitten with the same name in roster (i.e., remove the kitten from the roster). You can use findKitten() to check if the kitten exists in the roster. If kitten is not found, return false. Otherwise, delete the kitten, and return false. Remember to update the size of the roster accordingly and pay attention not to leave unused spaces in the roster array.

(7) Implement function getKittenFromFile(). This function takes two arguments: the name of a file that stores kitten info, and a struct roster. The function reads the file, and stores kittens info into roster.

The format of the file is as follows (a sample text file is provided as an example):

<kitten's name>
<kitten's color>
<kitten's cuteness>
...

If the file is not opened successfully, print

Error! File not found.

You can assume that the number of kittens in the file will not exceed the size of the array.

This function will be tested using unit testing.

(8) Implement  updateKitten() function. This function takes 2 arguments: a kitten struct, and a roster struct. First, it needs to look up the kitten name in the roster (You can use findKitten() to look up kitten's name). If not found, return false. If found, update the corresponding kitten's color and cuteness score, and return true.

(9) Implement printToFile() function. This function takes a string filename, and a roster as arguments, saves the roster into a text file with the following format:

ROSTER
Kitten 1 -- Name: Peanut Butter, Color: light brown, Score: 10
Kitten 2 -- Name: Addie, Color: grey, Score: 42
...

(10) Implement a function printRoster(). This function takes as argument a roster and prints all the kittens info to standard output, each one on a new line.

Ex:

ROSTER
Kitten 1 -- Name: Willow, Color: white, Score: 10
Kitten 2 -- Name: Addie, Color: grey, Score: 42
...

This function will be called when the user selects option 'o'.

(11) Implement the "Add kitten" menu option. Prompt the user for a new kitten's name, color and score.

Ex:

Enter a new kitten's name:
Rocky Balboa
Enter the kittens's color:
white
Enter the kittens's cuteness score:
82

Store the user input into a kitten struct, then use addKitten() function to add the new kitten to roster. Note that the kitten's name may contain spaces.

(12) Implement option "Delete kitten". If the user chooses the option when the roster is empty, immediately print the message:

Cannot delete from empty roster.

If the roster is not empty, prompt the user for a kitten's name. Remove the kitten with the specified name from the roster using deleteKitten()

Enter kitten name to delete:
Rocky Balboa

If deleteKitten() returns false (i.e. kitten is not in the roster), inform the user:

Error! Kitten not found.

(13) Implement the "Update kitten color and cuteness score" menu option, Prompt the user for a kitten's name, color and score.

Ex:

Enter a kitten name:
Jingle Bell
Enter a new color for the kitten:
Pink
Enter a new cuteness score for the kitten:
98

Call updateKitten(). If update unsuccessful, print the following error message:

Cannot find kitten.

Otherwise, display message:

Successfully updated kitten info.

(14) Implement "Find kitten" menu option. Prompt the user for kitten's name.

Ex:

Enter a kitten name:
Grump Kit

Use findKitten(). If kitten is not found, print the following error message:

Kitten not found.

Otherwise, print out kitten info in the following format:

kittenName info: color, cuteness score

(15) Implement option "Load kitten info from file". Prompt the user for input file name.

Enter file name:

Use getKittenFromFile().

(16) Implement option "Save kitten info to file". Prompt the user for output file name.

Enter file name:

Use printToFile().

IMPORTANT:

  • Any attempt to hard code the program output will result in a grade of 0.
  • The use of vectors is not allowed in this lab and will result in a grade of 0.

file:

Willow
white
10
Eddie
brown
50
Carrot
grey
99
Maxie
purple
100
Furry
green
45

use get line for ^^

in c++ language

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

Explanation::

Code in C++ is given below

Output is given at the end of the code

Code in C++:

#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\n"<<"a - Add kitten\nd - Delete kitten\nu - Update kitten color and cuteness score\nf - Find kitten\nl - Load kitten info from file\ns - Save kitten info to file\no - Output roster\nq - Quit\n\nChoose an option : ";

}

int findKitten(string str, struct roster* ros){

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

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

      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.\n";

    return;

  }

  /**

  * First ADD and then inrement size

  */

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

  ros->size++;

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

}

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];

    index=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\n";

  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<<"\n";

  }

}

void getKittenFromFile(string str, struct roster* ros){

  ifstream myfile;

  myfile.open(str.c_str());

  if(!myfile){

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

    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;

  }

}

void printToFile(string str, struct roster* ros){

  ofstream myfile;

  myfile.open(str.c_str());

  myfile<<"ROSTER\n";

  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<<"\n";

  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' :

        cin.ignore();

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

        getline(cin,kit.name);

        cout<<"Enter the kitten's color:"<<endl;

        getline(cin,kit.color);

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

        cin>>kit.score;

        addKitten(&kit,&kittenRoster);

      break;

      

      case 'd' :

      if (kittenRoster.size == 0){

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

      }else{

        cin.ignore();

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

        getline(cin,kit.name);

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

        if(bol == false)

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

      }

      break;

      

      case 'u' :

        cin.ignore();

        cout<<"Enter a kitten name:"<<endl;

        getline(cin,kit.name);

        cout<<"Enter a new color for the kitten:"<<endl;

        getline(cin,kit.color);

        cout<<"Enter a new cuteness score for the kitten:"<<endl;

        cin>>kit.score;

        bol = updateKitten(&kit,&kittenRoster);

        if(bol)

          cout<<"Successfully updated kitten info.\n";

        else

          cout<<"Cannot find kitten.\n";

      break;

      

      case 'f' :

        cin.ignore();

        cout<<"Enter a kitten name:"<<endl;

        getline(cin,kit.name);

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

        if (index == -1)

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

        else{

          cout<<kittenRoster.kittens[index].name<<" info: "<<kittenRoster.kittens[index].color<<", "<<kittenRoster.kittens[index].score<<"\n";

        }

      break;

      

      case 'l' :

        cin.ignore();

        cout<<"Enter the file name :"<<endl;

        getline(cin,name);

        getKittenFromFile(name,&kittenRoster);

      break;

      

      case 's' :

        cin.ignore();

        cout<<"Enter the file name :" ;

        getline(cin,name);

        printToFile(name,&kittenRoster);

      break;

      

      case 'o' :

        printRoster(&kittenRoster);

      break;

      

      case 'q' :

        loop = 0;

      break;

      

      default:

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

      break;

    }

  }

}

OUTPUT:

TEST CASE 1:

MENU

a - Add kitten

d - 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 : o

ROSTER

MENU

a - Add kitten

d - 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 : l

Enter the file name :

input.txt

MENU

a - Add kitten

d - 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 : o

ROSTER

Kitten 1 -- Name: Willow, Color: white, Score 10

Kitten 2 -- Name: Eddie, Color: brown, Score 50

Kitten 3 -- Name: Carrot, Color: grey, Score 99

Kitten 4 -- Name: Maxie, Color: purple, Score 100

Kitten 5 -- Name: Furry, Color: green, Score 45

MENU

a - Add kitten

d - 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 : s

Enter the file name :output.txt

MENU

a - Add kitten

d - 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 : q

input.txt:

Willow

white

10

Eddie

brown

50

Carrot

grey

99

Maxie

purple

100

Furry

green

45

output.txt:

ROSTER

Kitten 1 -- Name: Willow, Color: white, Score 10

Kitten 2 -- Name: Eddie, Color: brown, Score 50

Kitten 3 -- Name: Carrot, Color: grey, Score 99

Kitten 4 -- Name: Maxie, Color: purple, Score 100

Kitten 5 -- Name: Furry, Color: green, Score 45

TEST CASE 2:

MENU

a - Add kitten

d - 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 : k

Invalid option. Please try again.

MENU

a - Add kitten

d - 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 : a

Enter a new kitten's name:

Jingle Bell

Enter the kitten's color:

white

Enter the kitten's cuteness score:

78

Successfully added new kitten to roster.

MENU

a - Add kitten

d - 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 : a

Enter a new kitten's name:

Rocky Balboa

Enter the kitten's color:

black

Enter the kitten's cuteness score:

82

Successfully added new kitten to roster.

MENU

a - Add kitten

d - 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 : o

ROSTER

Kitten 1 -- Name: Jingle Bell, Color: white, Score 78

Kitten 2 -- Name: Rocky Balboa, Color: black, Score 82

MENU

a - Add kitten

d - 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 : f

Enter a kitten name:

Rocky Bal

Kitten not found.

MENU

a - Add kitten

d - 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 : f

Enter a kitten name:

Rocky Balboa

Rocky Balboa info: black, 82

MENU

a - Add kitten

d - 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 : u

Enter a kitten name:

Rocky Bal

Enter a new color for the kitten:

pink

Enter a new cuteness score for the kitten:

89

Cannot find kitten.

MENU

a - Add kitten

d - 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 : u

Enter a kitten name:

Rocky Balboa

Enter a new color for the kitten:

pink

Enter a new cuteness score for the kitten:

89

Successfully updated kitten info.

MENU

a - Add kitten

d - 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 : o

ROSTER

Kitten 1 -- Name: Jingle Bell, Color: white, Score 78

Kitten 2 -- Name: Rocky Balboa, Color: pink, Score 89

MENU

a - Add kitten

d - 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 : a

Enter a new kitten's name:

Pixie

Enter the kitten's color:

brown

Enter the kitten's cuteness score:

100

Successfully added new kitten to roster.

MENU

a - Add kitten

d - 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 : o

ROSTER

Kitten 1 -- Name: Jingle Bell, Color: white, Score 78

Kitten 2 -- Name: Rocky Balboa, Color: pink, Score 89

Kitten 3 -- Name: Pixie, Color: brown, Score 100

MENU

a - Add kitten

d - 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 : d

Enter kitten name to delete:

Jingle Bell

MENU

a - Add kitten

d - 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 : o

ROSTER

Kitten 1 -- Name: Rocky Balboa, Color: pink, Score 89

Kitten 2 -- Name: Pixie, Color: brown, Score 100

MENU

a - Add kitten

d - 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 : q

Please Provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
This program will store a roster of most popular videos with kittens. The roster can include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

  • 5.19 Ch 5 Program: Soccer team roster (Vectors) (C++) This program will store roster and rating...

    5.19 Ch 5 Program: Soccer team roster (Vectors) (C++) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings in another int vector. Output these vectors (i.e., output the roster). (3 pts) Ex:...

  • Java 7.17 Clone of LAB*: Program: Soccer team roster This program will store roster and rating...

    Java 7.17 Clone of LAB*: Program: Soccer team roster This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0-99, but this is NOT enforced by the program nor tested) and the player's rating (1-9, not enforced like jersey numbers). Store the jersey numbers in one int array and the ratings in another int...

  • 5.22 LAB*: Program: Soccer team roster steam This program will store roster and rating information for...

    5.22 LAB*: Program: Soccer team roster steam This program will store roster and rating information for a soccer team Coaches rate players during tryouts to ensure (1) Prompt the user to input five pairs of numbers: A player's jersey number (0.99) and the player's rating (1-9) Store in one int array and the ratings in another int array Output these arrays (e. output the roster) (3 pts) numbers EX Enter player 1 jersey number: Enter player l's rating: Enter player...

  • 5.19 Lab 11b: Soccer team roster Instructor note: NOTE Unlike our other assignments, this one has...

    5.19 Lab 11b: Soccer team roster Instructor note: NOTE Unlike our other assignments, this one has only two programs: People's Weights and this one. To earn 30 points, you must do both assignments. Allow ample time to complete them Passing arrays to methods Review Chapter 6.8 Array Parameters if you wish to use methods in your program (a good idea). Since we pass a reference to the array's location in memory, changes made to the array within the method will...

  • 11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" pr...

    11.12 LAB*: Program: Online shopping cart (continued)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class to contain a new attribute. (2 pts)item_description (string) - Set to "none" in default constructorImplement the following method for the ItemToPurchase class.print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter.Ex. of print_item_description() output:Bottled Water: Deer Park, 12 oz.(2) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs...

  • Assignment Write a menu-driven C++ program to manage a class roster of student names that can...

    Assignment Write a menu-driven C++ program to manage a class roster of student names that can grow and shrink dynamically. It should work something like this (user input highlighted in blue): Array size: 0, capacity: 2 MENU A Add a student D Delete a student L List all students Q Quit ...your choice: a[ENTER] Enter the student name to add: Jonas-Gunnar Iversen[ENTER] Array size: 1, capacity: 2 MENU A Add a student D Delete a student L List all students...

  • Write a menu-driven C++ program to manage a class roster of student names that can grow...

    Write a menu-driven C++ program to manage a class roster of student names that can grow and shrink dynamically. It should work something like this (user input highlighted in blue): Array size: 0, capacity: 2 MENU A Add a student D Delete a student L List all students Q Quit ...your choice: a[ENTER] Enter the student name to add: Jonas-Gunnar Iversen[ENTER] Array size: 1, capacity: 2 MENU A Add a student D Delete a student L List all students Q...

  • Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me e...

    Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me errors. Thanks in advance!! This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of...

  • 7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consid...

    7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase namedtuple to contain a new attribute. (2 pts) item_description (string) - Set to "none" in the construct_item() function Implement the following function with an ItemToPurchase as a parameter. print_item_description() - Prints item_name and item_description attribute for an ItemToPurchase namedtuple. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT