trouble changing values from a function C++

I've been learning to code for 3-4 weeks now using codecademy, i came to the end of a lesson where i need to refactor existing code using functions. tried to add a separate function where it counts the amount of times you call IT. However no matter what I try it won't increment a value or change a value at all. I think maybe I don't fully understand how to return a function.

(note, i did previously try incrementing tries using tries++, however that didn't work so i figured to declare a whole new integer to add on to tries called new_tries.)

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
#include <iostream>

// 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 << "Bring in your computer for repairs.\n";
   }
   else {
  tries + new_try;
  std::cout << "Hello. IT.\n";
  std::cout << "Have you tried turning it off and on again? y/n\n";
  std::cin >> on_off;
   }
   return(tries + new_try);
}

void Jenn(){
std::cout << "Oh hi Jen!\n";
}
void Roy(){
std::cout << "You 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;
  int tries;
  int new_try;
  tries = 0;
  new_try = 1;
// IT support function
  it_support(on_off_attempt, tries, new_try);
  // Check in with Jenn
  Jenn();
  // Conduct IT support again...
  it_support(on_off_attempt, tries, new_try);
  // Check in with Roy
  Roy();
  // Conduct IT support yet again...zzzz...
    it_support(on_off_attempt, tries, new_try);
// checking to see tries value DELETE THIS WHEN SOLVED
    std::cout << "tries = " << tries << " \n";
}

This is terrible! it_support() should clearly be called Roy(), while Roy() is obviously supposed to be Moss(). Amateur hour! I have tears in my eyes.

There's multiple problems here.
Line 11 does nothing, since you don't assign the result of the addition to anything. I assume you meant to use the += operator, but the new_try variable is pretty pointless, since it never changes value. You could simply do tries++, or tries += 1.
The return value from the function is never used for anything, but again I suspect what you were really trying to do was change the value of tries in the caller. I.e. pass by reference.
on_off_attempt also doesn't make much sense. Why are you making the user enter something that you don't do anything with?
I've changed back to tries++ gotten rid of the useless variables. however still when I cout tries, it says 0. I also tried changing back to a void. I don't entirely understand "pass by reference" does it mean I'm not declaring tries properly in main as an argument?

Also, on_off was part of the existing code in the lesson, I assumed I'd just add different strings later on, its just there to proceed on with line of code when compiled. Thanks for the help, you gotta start somewhere.
If whatever lesson you're following has not gone over how to pass by reference or by pointer then you should be returning the new value of tries from it_support() and then using that value in main(). Do you know how to use the return value of a function?
Without using pass by reference, consider:

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
#include <iostream>
#include <string>

// task functions
int it_support(std::string on_off, int tries, int new_try) {
	if (tries >= 3)
		std::cout << "Bring in your computer for repairs.\n";
	else {
		std::cout << "Hello. IT.\n";
		std::cout << "Have you tried turning it off and on again? (y/n): ";
		std::cin >> on_off;
	}

	return(tries + new_try);
}

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

void Roy() {
	std::cout << "You 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;
	int tries {1};
	int 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 << "tries = " << tries << " \n";
}


Note that on-off_attempt doesn't have any effect. What is it supposed to be used for?
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
Thank you Handy Andy! That rewrite made it much easier to see what went wrong. The problem is now officially fixed! This learning curve made it clear I still have much to learn regarding functions. And when I was finally given the freedom to take this code into my own hands, i bit off more than I'm able to chew. I'm just glad I can finally move on.

I'm not going to bother fixing the string however, when compiled the input expects some sort of input, as long as it receives it, (at least for me) the compiler will move on with reading the code. I know its an incredibly bizarre and lazy fix to an avoidable problem, however that part of the code was in the lesson already, so I felt the need to keep it included for whatever reason.

"Pass by", is a very new concept to me, I can almost guarantee the problem i faced was in the next lessons all along. But all of your help is greatly appreciated, it takes a person filled with passion who can take time out of their day to share valuable knowledge with others. All my respect goes out to you.
Topic archived. No new replies allowed.