FAQ: Functions - Review of C++ Functions

I tried your code in visual studio code and it only works for when we input “y”. When responding with “n” the program ends.

What I actually did in this exercise is just to replicate the main inside a void by calling that void function inside of main. This way I don’t have to repeat myself everytime I want that standard response from IT.

#include <iostream>

void support() {

  std::string on_off_attempt;
  std::cout << "Hello. IT.\n";
  std::cout << "Have you tried turning it off and on again? y/n\n";
  std::cin >> on_off_attempt;

}

int main() {
  
  // Conduct IT support
  support();
  
  // Check in with Jenn
  std::cout << "Oh hi Jen!\n";
  
  // Conduct IT support again...
  support();

  // Check in with Roy
  std::cout << "You stole the stress machine? But that's stealing!\n";
  
  // Conduct IT support yet again...zzzz...
  support();
  
}

1 Like

//I tried this. It worked. But i have no idea why “on_off_attempt” supposed to do here. Maybe it only needs to be included to output next check with IT lines.
#include

std::string IT(int x) {

while (x < 4) {

std::string on_off_attempt;

std::cout << “Hello. IT.\n”;

std::cout << “Have you tried turning it off and on again? y/n\n”;

std::cin >> on_off_attempt;

if (x == 1) {

  std::cout << "Oh hi Jen!\n";

} else if (x == 2) {

  std::cout << "You stole the stress machine? But that's stealing!\n";

} else {

  std::cout << "Have you tried turning it off and on again? y/n\n";

  std::cin >> on_off_attempt;

} x++;

}

}

int main() {

IT(1);

IT(2);

IT(3);

}