Write a function that, when you call it, displays a message telling how many times it has been called: “I have been called 3 times”, for instance. Write a main() program that ask the user to call the function, if the user presses ‘y’ the function is called otherwise if ‘n’ is pressed the program terminates.
#include <iostream>
void function()
{
static int count = 0;
count += 1;
std::cout << "I have been called " << count << " times" << std::endl;
}
int main()
{
std::string line;
while (true) {
std::cin >> line;
if (line == "y")
function();
if (line == "n")
break;
if (line != "y" and line != "n")
std::cout << "oops..." << std::endl;
}
return 0;
}
Comments
Leave a comment