Answer to Question #204964 in C++ for Saqib

Question #204964

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.



1
Expert's answer
2021-06-09T16:03:50-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog