0

So essentially, I have created a function that has 3 parameters and it adds the total amount of all 3 of the arguments passed to it and then prints an asterisk for each number until the total has been reached. An example of the function call would be:

stars(6, 1, 4);

and the output is just 11 asterisks on the same line. So basically, it adds up all the arguments, and then loops through, printing an asterisk for each iteration of the loop. Now, I also want to be able to call it in my main() like this:

stars(2, 5);

but every time I call it with only 2 arguments, I get an error stating that there are too few arguments to function 'void stars(int, int, int)'. I have no clue how to go about this honestly, and would love to be pointed in the right direction so I can solve this. Thanks! Btw, my function looks like this:

void stars(int arg1, int arg2, int arg3){
        int total = arg1 + arg2 + arg3;

        for (int i = 0; i < total; i++) {
            cout << "*";
        }

        cout << "\n";
}
Josh Dazey
  • 33
  • 5
  • 1
    Have patience, and keep reading and doing practice problems from your C++ textbook, one chapter at a time. At some point you'll reach a chapter about templates, then later a chapter about fold expressions, and all will be crystal clear. You can't just jump into fold expressions head first, full speed ahead, man-the-torpedoes style. Without having a full and complete understanding of the basics you will not know which end is up. So, just keep on following your C++ textbook, one chapter at a time, and eventually you'll get there. There is no instant gratification with C++, it takes time. – Sam Varshavchik Jul 21 '22 at 01:33
  • 2
    Well, if all you want is to be able to call it with two arguments, make it `void stars(int arg1, int arg2, int arg3 = 0) { ... }` – Igor Tandetnik Jul 21 '22 at 01:36
  • @SamVarshavchik I can’t stand that kind of advice. It’s patronizing and rude. C++ is so huge there is no reason someone can’t learn some small something that’s more advanced at any time. We might as well still be telling people they have to learn C before C++. Especially as a very simple solution (default arguments) is an obvious first answer for the beginner. – Dúthomhas Jul 21 '22 at 02:13
  • Just wanted to say, I overcame this problem by using overloading functions. I know this may not be the best fix, but it is what is covered in my textbook chapter which focuses on functions. I'm sure I will learn better methods later on to overcome an issue like this in the future. – Josh Dazey Jul 21 '22 at 09:31
  • Thank you for your advice, @Dúthomhas, it was given all the consideration that it was entitled for. – Sam Varshavchik Jul 21 '22 at 10:54

2 Answers2

1

try making the argument type of your function an array for more flexibility like this since your code looks hard coded for a specific number

void stars(int arg[10]){  //10 is an exemple
    /* Your code */
}

then you need to loop the array to get the total with a simple for loop .learn more about arrays
the new way to call your function is now

stars({5,7,8});

or:

stars({1,2,3,5,8});

etc...

  • At least use `std::initializer_list args` instead of `int arg[10]`... Othwise how do you know the actual number of items? – Phil1970 Jul 21 '22 at 01:53
0

To keep the exact same syntax for any number of arguments, you can use a variadic template.

template <typename... Args>
void stars(Args... args) {
    int total = (... + args);
    for (int i = 0; i < total; i++) std::cout << '*';
    cout << '\n';
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80