Here is the actual task: Write a function that usually takes one argument β€” the address of the string β€” and outputs this string once. However, if the second argument of the type int specified, which is not equal to zero, then this function outputs the string as many times as there were calls to this function by the time of its given call. (Please note that the number of output lines is not equal to the value of the second argument ; it is equal to the number of function calls by the time of the last call .)

Indeed, this is not a very useful function, but it will force you to apply some of the methods discussed in this chapter. Write a simple program to demonstrate this feature.

This chapter covered: Built-in functions, Reference variables, Passing arguments to a function by reference, Default arguments, Function overloading, Function templates, Function template specifications.

That's what I could write, I know what is wrong. Please tell me how you can solve.

 #include <iostream> void out(char* a); void out(char *a, int n); int n = 0; int main() { char ch[] = "Hello"; char * pd = ch; out(pd); out(pd); out(pd); out(pd); out(pd); std::cout << std::endl; out(pd, n); std::cout << std::endl; return 0; } void out(char *a) { std::cout << a << std::endl; n++; } void out(char *a, int n) { if (n != 0) { for (int i = 0; i < n; i++) { std::cout << a << std::endl; } } } 

    2 answers 2

    As for me, the easiest way to do this is as shown below. The static variable n counts the number of calls, the second argument has a default value of 0 ...

     void out(const char* a, int x = 0) { static int n = 0; ++n; cout << a << endl; if (x) { for(int i = 0; i < n-1; ++i) cout << a << endl; } } 

    True, this function considers the current call. But if it is not necessary to do this, then simply transfer ++n to the end of the function.

      and I decided like this (for the same textbook I'm doing now)

       int main() { string a_string; int number_of_calls = 0; for (int i = 0; i < 4; i++) { cout << "Enter new line: "; getline(cin, a_string); repeat(a_string, number_of_calls); } return 0; } void repeat(const string& a_string) { cout << a_string << endl; } void repeat(const string& a_string, int& number_calls) { if (number_calls > 0) { for (int i = 0; i < number_calls; i++) cout << a_string << endl; number_calls++; } else { cout << "Aha, first time!\n" << endl; number_calls = 1; } } 
      • At a minimum, the condition does not work. Note that the number of output lines is not equal to the value of the second argument; it is equal to the number of function calls by the time of the last call. - you need to transfer exactly the number of calls. And also, what will happen if the program suddenly changes number_of_calls - even by accident? - Harry
      • > what will happen if the program suddenly changes the number_of_calls - even by accident - you need to add const before it, yes. - al m
      • on the other hand, if you add a const, this function will not change its value, but I just wanted it to change it. - al m
      • so why the condition is not met? for example, this function is called for the first time - it does not repeat the string. calls to the second - prints once, and the third - twice. - al m
      • Because the condition clearly states that the number of rows to be output is not equal to the value of the second argument. - Harry