Hello chadwillis19,
I will start with reformatting your code. The comments I add should help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <string> // <--- Added.
// to do: figure out why tries isnt fucking incrementing
// task functions
int it_support(std::string& on_off, int tries, int new_try)
{
if (tries >= 3)
{
std::cout << "\nBring in your computer for repairs.\n";
}
else
{
//tries + new_try; // <--- Has no affect. The result is not stored in anything.
std::cout << "\nHello. IT.\n";
std::cout << "Have you tried turning it off and on again? y/n: ";
std::cin >> on_off;
}
return tries + new_try;
//return ++tries; // <--- Does the same as above.
}
void Jenn()
{
std::cout << "\nOh hi Jen!\n";
}
void Roy()
{
std::cout << "\nYou stole the stress machine? But that's stealing!\n";
}
int main()
{
//counts the amount of times you've called IT
std::string on_off_attempt; // <--- Value is changed in the function, but never used.
int tries{}; // <--- ALWAYS initialize all your variables.
constexpr int new_try{ 1 }; // <--- Not really needed here. Can be defined in the function,
// but it is not needed there either. And should be a constant.
//tries{}; // <--- These need a type, but then they would be duplicates and errors.
//new_try{ 1 };
// IT support function
tries = it_support(on_off_attempt, tries, new_try);
// Check in with Jenn
Jenn();
// Conduct IT support again...
tries = it_support(on_off_attempt, tries, new_try);
// Check in with Roy
Roy();
// Conduct IT support yet again...zzzz...
tries = it_support(on_off_attempt, tries, new_try);
// checking to see tries value DELETE THIS WHEN SOLVED
std::cout << "\ntries = " << tries << " \n";
return 0; // <--- Not required, but makes a good break point for testing.
}
|
I have no idea what IDE you are using or what errors you are getting when you compile.
You may not have a problem with
std::string on_off_attempt;
, but when you get to
std::cin >> on_off;
"std::cin" has no idea how to process the string with out the "<string>" header file. The same is true for a "std::cout".
For your function:
int it_support(std::string& on_off, int tries, int new_try)
. A function can only return 1 thing, which you are doing, but on the other hand what is returned could be a "struct", "tuple" or something else that can contain more than 1 item. The "std::string" is being passed by reference which means that it is using what is defined in "main" and not a local variable. More complicated objects like "string"s and "vector"s should be passed by reference. Simple variables like; bool, char, int and double can easily be passed by value.
Passing by value just means that the function is making a copy of the variable to use.
In your function parameters "int new_try" is not needed. It may be there to teach a point, but you can do with out it, see line 23.
The next 2 functions are OK although I did add a (\n) to the beginning of the strings for an easier to read output.
The comments in "main" should be enough. If not let me know.
Line 45 is how you make use of the return value of the function call.
By line 57 the value of "tries" will be 3, but since you never call the function again the if statement in the function will never become true.
You could start the function as:
1 2 3 4 5 6 7 8
|
int it_support(std::string& on_off, int tries, int new_try)
{
tries++;
if (tries >= 3)
// Other code.
return tries;
|
Here are some things that you may find useful:
Your use of {}s and which style you use is your choice. Just be consistent with them as you have demonstrated.
You can check out different styles at
https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements
Personally I like the "Allman" style. It makes your code easier to read and to find errors. That is 1 part. The other is proper indentation and some blank lines to break up the code.
ALWAYS initialize all your variables. This is a good example:
1 2 3 4 5 6 7 8 9
|
int main()
{
//counts the amount of times you've called IT
std::string on_off_attempt;
int tries;
int new_try;
// IT support function
it_support(on_off_attempt, tries, new_try);
|
In line 5 "tries" is defined and space on the stack is reserved for this variable. Uninitialized it tries to use whatever is stored there and tries to make an int out of it. On my computer I usually get (-858993460) for this garbage value.
On line 9 you are sending this garbage value to your function and your compiler should complain about it. I get the error "
uninitialized local variable 'tries' used". Not what you want.
Your comments are good, but a bit over done, but not a problem.
Lastly a good variable and function name can make a big difference. This is usually a verb that describes what it is or what it does.
At 1st the functions "Jenn" and "Roy" did not make any sense until I worked with the program a bit.
Andy