Linked Lists Problems

I am a college student trying to write a program that has a user enter a tracking number, type of animal, and boarding charge. If the user wants to enter more than one set of data then they are able to. I am supposed to then have the program print the list of information. I am stuck in trying to figure out how to keep the information entered if more than one set of information is entered. I am confused completely at how this can be done. Can someone help me?
1
2
3
4
5
6
7
8
9
10
class Animal{

public:
// Functions

private:
  string animalType;
  int trackingNumber;
  double boardingCharge;
};
How would you input the information and let the user decide when they were done putting the info in? I have the output part of the program decently figured out.
Something like this, perhaps?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <vector>

struct animal
{
    int tracking_number = 0 ;
    std::string type ;
    double boarding_charge = 0 ;
};

int main()
{
    std::vector<animal> list ;

    animal a ;
    while( std::cout << "enter tracking number (enter a non-positive value (eg. -1) to end input): " &&
           std::cin >> a.tracking_number && a.tracking_number >= 0 )
    {
        std::cout << "type of animal: " ;
        std::cin >> a.type ; // assumes that there are no embedded spaces in type

        std::cout << "boarding charge: " ;
        std::cin >> a.boarding_charge ;

        // add this animal to the list of animals
        list.push_back(a) ;
    }

    // print out contents of list
    for( const animal& a : list )
        std::cout << a.tracking_number << ' ' << a.type << ' ' << a.boarding_charge << '\n' ;
}
In my class we have not used vectors yet. Just using classes, structs, and linked lists.
Topic archived. No new replies allowed.