Class Problem

Hi there,

I am current stuck on these problems with my current code.
WARNING: My current code has 3 different files so it might get confusing.
What I'm confused with is how go about doing things so my code works similar to the output below.

I. Write statements that modify the Cat objects in myCats such that each Cat
object has a random birth year between 2010 and 2016 and a random name from the
following array:

const int NUM_BREEDS = 10;
string catBreeds[NUM_BREEDS] = {“Abyssinian”, “American
Shorthair”, “Birman”, “Burmese”, “Exotic Shorthair”, “Main
Coon”, “Oriental”, “Sphynx”, “Persian”, “Siamese”};

Hint: use the rand() function to generate random numbers. Also, make sure that a new set of
random values is generated every time you run the program.

J. Write statements that print the Cat objects in myCats. Display your results in a
tabular format as shown in the sample screenshots below.

Hint: use the function setw( ) to display your results in a tabular format

Output should look something like this:
Cat Breed Year
-------------------------
1 Oriental 2012
2 Sphynx 2012
3 Exotic Shorthair 2015
4 Birman 2016
5 Oriental 2013
6 "" 2011
7 "" 2010
8 "" 2013
9 "" 2014
10 "" 2012
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Cat.h
#ifndef CAT
#define CAT
#include <iostream>
#include <string>

using std::string;

class Cat
{
private:
	string newName;
	int newYear = 2010;

public:
	Cat();
	Cat(string, int);
	string getName() const;
	int getYear() const;
	void setName(string);
	void setYear(int);
	void Cat::setW(string, int);
};
#endif

//Cat.cpp
#include "Cat.h"

using std::cout;
using std::endl;

Cat::Cat()
{
	newName = newName;
	newYear = 2010;
}
Cat::Cat(string name, int year)
{
	newName = name;
	newYear = year;
}
string Cat::getName() const
{
	return newName;
}
int Cat::getYear() const
{
	return newYear;
}
void Cat::setName(string name)
{
	newName = name;
}
void Cat::setYear(int birthYear)
{
	newYear = birthYear;
}
void Cat::setW(string name, int size)
{
	cout << "      Cat breed       Year" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << 
	}
}

//main.cpp
#include <iostream>
#include <string>
#include "Cat.h"

using namespace std;

int main()
{
	string name;
	int year;
	Cat myCats[25];
	const int NUM_BREEDS = 10;

	string catBreeds[NUM_BREEDS] = { "Abyssinian", "American Shorthair", "Birman", "Burmese", "Exotic Shorthair", "Main Coon", "Oriental", "Sphynx", "Persian", "Siamese" };

	

	return 0;
}
I suggest that you make setw() to a freestanding function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
void setw( Cat & cat)
{
    std::cout << cat.getYear() << '\t' << cat.getName() << '\n';
}

int main()
{
    ...
    std::srand(std::time(nullptr));  // sets a start seed by actual time
    std::vector<Cat> v_cats;;
    for( int i = 0; i < 100; ++ i)
    {
        v_cats.push_back( Cat( catBreeds[ std::rand() % NUM_BREEDS], std::rand() % 5 + 2011) );
    }
    for( Cat & cat : v_cats) setw( cat );
}
Last edited on
I suggest that you make setw() to a freestanding function.

Are you aware of std::setw()?
Are you aware of std::setw()?

I wasn't :-(
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
#include<vector>
#include<cstdlib>
#include<ctime> 
...
void print_cats( std::vector<Cat> & cats )
{
    for ( auto & cat : cats)
    {
        std::cout << std::setw(18) << cat.getName() << cat.getYear() << '\n';
    }
}

int main()
{
    ...
    std::srand(std::time(nullptr));  // sets a start seed by actual time
    std::vector<Cat> v_cats;
    for( int i = 0; i < 100; ++ i)
    {
        v_cats.push_back( Cat( catBreeds[ std::rand() % NUM_BREEDS], std::rand() % 5 + 2011) );
    }
    print_cats( v_cats );
}

Here you need to provide a matching Cat constructor.
Last edited on
Topic archived. No new replies allowed.