简体   繁体   中英

C++ simple program realization

I need your help to get started with a good implementation thinking.

I need to create a simple program with the following: 1. A function with two parameters, first parameter is a name (char*), and the second parameter is the number of times you would like to print this name to the screen(int). If the second parameter wasn't supplied in the function call, it should print the name 10 times. If the first parameter wasn't supplied in the function call, it should print the author name / the writer of the program (ie my name).

I was thinking to create the following function with default parameteres:

void printTextToScreenNTimes(char * text = "guy", int n = 10);

This function implementation is the following :

void printTextToScreenNTimes(char * text, int n) {
  int i;
  for (i = 0; i < n; i++)
    cout << text << " ";

}

My problem arises as I try to print my default name 2 times. For example if I want only using the following function call:

void printTextToScreenNTimes(3);

I would like the function to print the default name (in this case "guy") 3 times but It's impossible because I must fill the first parameter.

Do I have no choice but using a global parameter holding my name?

Overloading:

void printTextToScreenNTimes(int x)
{
    printTextToScreeNTimes("guy", x);
}

I think you are reading too much into your requirements. I think the second part about not supplying the name only applies when the first part (not supplying the count) also applies.

But in any case you can solve it using overloading as Luchian has just explained.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM