At the end of the Functions section of the C++ course, there’s an optional project to create a function to run a repeated bit of input and output. Since it’s the review portion, there’s no hints or ‘show solution’ button, so I’m at a loss for understanding where I went wrong.
#include <iostream>
int main() {
// Conduct IT support
std::string it_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;
}
// Check in with Jenn
std::cout << "Oh hi Jen!\n";
// Conduct IT support again...
it_support();
// Check in with Roy
std::cout << "You stole the stress machine? But that's stealing!\n";
// Conduct IT support yet again...zzzz...
it_support();
}
Apparently it_support(); isn’t declared. What am I missing? The review portion has no hints or the ‘Show Solution’ button, so I’m at a loss.
Hello, @codeblaster44138, and welcome to the forums.
Could you post a link to the exercise? Also please review How do I format code in my posts?, to see how to make your code appear in future posts as it does now. (I edited it.)
1 Like
You’re declaring it in your main function, you should declare the function outside your main function (either above, or below).
Example:
void foo()
{
//foo definition goes here
}
int main()
{
foo();
return 0;
}
or
void foo(); // you can declare it here and define it below
int main()
{
foo();
//since c++ is compiled, it's ok to have it in the main,
//as it will be defined after
//compilation in your output file.
return 0;
}
void foo()
{
//foo definition goes here
}
In many contexts the latter is more readable.
There’s also the matter of how you’re taking in your string. Since it’s a char
in this case you’re totally fine, but if it needed to be a word with a space you would get into a little bit of trouble. I only say this not to associate a straightforward cin
with string
.
Example:
#include <iostream>
#include <string>
int main()
{
std::string s;
std::cout << "Please say something: ";
std::cin >> s;
std::cout << "\n" << s;
return 0;
}
Output:
Please say something: something
out >>> something
Please say something: hello world
out >>> hello
!! not what we want
so a follow-up thing to do is to look up how to take in a string input with spaces.
1 Like
Had a feeling it’d be something (relatively) obvious I missed. Thanks 
No worries, to me C++ is one of the least obvious languages. So I’m always ready to be destroyed by it hahahhahah.