FAQ: Functions - Review of C++ Functions

This community-built FAQ covers the “Review of C++ Functions” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Review of C++ Functions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Mine won’t execute

it.cpp

#include <iostream>
#include "functions.hpp"
int main() {
  
  // Conduct IT support
  // Check in with Jenn

  // Conduct IT support again...
  // Check in with Roy
  // Conduct IT support yet again...zzzz...
  
}

header file functions.hpp

std::string hello_it(std::string on_off_attempt);

std::string hi_jen();

std::string hello_it(std::string on_off_attempt);

std::string hi_roy();

std::string hello_it(std::string on_off_atempt);

definitions file functions.cpp


std::string hello_it(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;
}

std::string hi_jen()
{
   std::cout << "Oh hi Jen!\n";
}

std::string hi_roy();
{
  std::cout << "You stole the stress machine? But that's stealing!\n";
}

Ok, I got it to work like this, any ideas in how to further refactor ?

#include
std::string it_crowd(int tries){

std::string yes_or_no;
std::string attempts = “Hello IT. \n”“Have you tried turning off and on again? y/n\n”;
int x = 0;
while (tries > 0) {
std::cout << attempts;
std::cin >> yes_or_no;
x++;
tries–;
if (x == 1) {
std::cout << “Hello Jen!\n”;
}
else if (x == 2) {
std::cout << “You stole the stress machine? But that’s stealing!\n”;
}
}
return attempts;
}

int main() {
it_crowd(4);
}

I have yet to learn what this “.hpp” is but i think one of the issues is here :

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

It appears that on the original code provided the “on_off_attempt” doesn’t really return any meaningful data to the function, it’s just there to bring up the prompt after “Have you tried turning it off and on again? y/n\n”. In your code it appears that whatever you input will go to the only parameter in “hello_it()” I would think that would cause some issues.

The parameter from what I understand is just a variable, which you input data when you call the function so if you are calling “hello_it()” here on the second block :

std::string hello_it(std::string on_off_attempt);

std::string hi_jen();

std::string hello_it(std::string on_off_attempt);

std::string hi_roy();

std::string hello_it(std::string on_off_attempt);

Your are inputting " std::string on_off_attempt " into " std::sin >> on_off_attempt"

Probably poorly explained if its even correct, maybe someone that knows better will post :smile:

how do i pass the and of the functions chapter c++?? please help

Hello @alex1010110. Welcome to the forum.

You’ll have to post a specific question, or share the code your working on that you’re having trouble with along with a question. A link to the exercise would also be helpful.