Using a loop to convert integer to asterisk

Hey everyone,
I kind of hit a wall with my program that requires me to take these two fake videos:

Video video1("Title One", "www.youtube.com/one", "Comment ONE", 1.1, 1);
Video video2("Title Two", "www.youtube.com/two", "Comment TWO", 2.2, 2);

and then convert them to:

Title One, www.youtube.com/one, Comment ONE, 1.1, *
Title Two, www.youtube.com/two, Comment TWO, 2.2, **

where the asterisks represent ratings.

I made a get_rating() function in my .cpp file but am having trouble converting them correctly.

int Video::get_rating()
{
int i;
for( i = 0; i <5; i++)
{
cout << "*" << endl;
}
}

Obviously I know this is incorrect so if anyone knows of a source to get help or has helpful input then that would be greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> ratings;
    std::string s{};
    int rating = 5;
    for (int i = 0; i < rating; ++i)
    {
        s += '*';
    }
    ratings.push_back(s);
    for (const auto& elem: ratings)
    {
        std::cout << elem << '\n';
    }
}
lots of ways to defur a feline.

const string starz[11] = {".", "*", "**", "***", ... fill in the rest}

string getstars(int s)
{
if(s > 10) /// throw afit();
else
return stars[s];
}

This will return . for 0, * for 1, ** for 2, ... allowing up to a 10 star rating, or set up 5 stars, whatever.

Last edited on
Use std::string's fill constructor.
1
2
int rating{ 5 };
std::cout << "rating: " << std::string( rating, '*' ) << '\n';
I just want to look at your code really quick, just to give suggestions. (As a student who's been through this before)
1
2
3
4
5
6
7
8
9

int Video::get_rating()
{
int i;
for( i = 0; i <5; i++)
{
cout << "*" << endl;
}
}


Usually a get-function (also called a getter) has only one job, return the value of a private member. Is there a rating member in your video class? In that case, int get_rating() should simply return rating;, I'm not saying that rating might not also have to be modified in some way if it requires it, but usually that is done by modifier functions before the getter is used. In main that returned value should be caught by a variable that is waiting for an assignment...

1
2
Video bobFallsDown("videoFile", other variables as needed including rating);
int vidRating = bobFallsDown.get_rating();


That's the common practice for getters, setters being just as simple but in the opposite direction (hope that made sense).

If you want to output something to the console, I prefer to call it something like print_rating() or otherwise something clear that it is a console-output function for a specific member, and it should also only do a simple task like your for loop to print the appropriate number of stars. I wouldn't even use endl since I don't know if I want those stars to be part of a larger paragraph (leave that to more complex print functions or for main to deal with). Usually a print function is going to be a void function since it doesn't need to return anything and even better you can type const after its declaration to tell the compiler that the function itself will not change any of the object members.

Also try to keep in mind something called function decomposition. Basically if you have a complex function then try to break it down into littler functions. Like a large print function for the whole object could now be written;
1
2
3
4
5
6
7
void Video::print()
{
    print_title();
    print_address();
    print_comment();
    print_rating();
}


I know I'm getting a bit dense in "common practice", if your instructor says anything that doesn't fit with what I've said then listen to the teacher, right?
Last edited on
Topic archived. No new replies allowed.