0

I've been working on this problem for hours now, but can't seem to be able to figure it out. The while loop is running infinitely because the increment function is not working properly. Would really appreciate any pointers.

void Increment(int); int main() { int count = 1; while(count < 10){ cout << “ The number after “ << count; /* Function Increment adds 1 to count */ Increment(count); cout << “ is “ << count << endl; } return 0; } void Increment (int nextNumber) // Increment the parameter by 1 { nextNumber++; } 
3
  • 1
    change void Increment (int nextNumber) to void Increment (int & nextNumber) Commented Sep 6, 2018 at 4:05
  • any pointers is exactly the right thing to look for: the parameter nextNumber of your Increment function needs to be passed by reference (i.e. as a pointer). Commented Sep 6, 2018 at 4:06
  • 1
    the Increment function as you have placed it makes a copy of the value of nextNumber and increases it, so being a copy of the value will not change to count. Commented Sep 6, 2018 at 4:06

4 Answers 4

4

It's not working because when you are passing count to the function Increment a separate copy is created and that value is updated, not the original one. If you want the original value to be updated pass by reference or pointer.

void Increment (int &nextNumber) // Increment the parameter by 1 { nextNumber++; } 

Also, I don't think it's necessary to create a separate function for incrementing, you can just do count++ on the main function.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting this error when I try that: /var/tmp/cciksf00.o: In function main': /home/main.cpp:20: undefined reference to Increment(int)' collect2: error: ld returned 1 exit status
change the prototype signature as well. void Increment(int&);
That did it! Thank you!
0

void Increment(int) will not change the variable after the method is called. You must add & to the medhod: void Increment(int &).

Then your code will look like:

void Increment(int &); int main() { int count = 1; while(count < 10){ cout << “ The number after “ << count; /* Function Increment adds 1 to count */ Increment(count); cout << “ is “ << count << endl; } return 0; } void Increment (int & nextNumber) // Increment the parameter by 1 { nextNumber++; } 

Comments

0

The increment is only happening to the object created in the "Increment" method. Outside the method, the "nextNumber" does not exist as there is no link to the main function. The solution is to pass the addrress of the "count" variable, store the address in the pointer in the "Increment" method and do the operation. The pointer operation will affect the memory of the variable "count" as the memory reference of "count" is passed to "nextNumber"

void Increment(int*); int main() { int count = 1; while(count < 10){ std::cout << " The number after " << count; /* Function Increment adds 1 to count */ Increment(&count); std::cout << " is " << count << std::endl; } return 0; } void Increment (int *nextNumber) // Increment the parameter by 1 { *nextNumber=*nextNumber+1; } 

Comments

0
#include <iostream> using namespace::std; void Increment(int*); int main() { int count = 1; while(count < 10){ cout << "The number after " << count << endl; Increment(&count); cout << "is " << count << endl; } return 0; } void Increment (int *nextNumber) // Increment the parameter by 1 { (*nextNumber)++; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.