Thread: Basic Troubleshooting

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    19

    Basic Troubleshooting

    I currently have an assignment to troubleshoot a simple C++ program. According to the problem, it has three separate errors that cause infinite loops. I have been trying to identify them, but I've only come up with braces for the while statement. Here is the original with all the errors:
    Code:
    #include <iostream>
    using namespace std;
    
    
    void increment(int);
    
    
    int main()
    {
        int count = 1;
        
        while(count < 10)
        cout << " The number after " << count;
        increment(count);
        cout << " is " << count << endl;
        
        return 0;
    }
    
    
    void increment (int nextNumber)
    {
        nextNumber++;
    }
    Personally, I wouldn't even use the function for something this basic, but they want me to find errors, not make the program more efficient.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay, now show how you modified the program to "fix" the while.

    Personally, I wouldn't even use the function for something this basic, but they want me to find errors, not make the program more efficient.
    That's nice, but part of the problem may well be because of the use/improper use of the function so it's good that you're leaving the function as part of the program.

    I suggest you break out your debugger and single step through the program watching the values of the variables as you step.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Here's the program with the added braces:
    Code:
    #include <iostream>
    using namespace std;
    
    
    void increment(int);
    
    
    int main()
    {
    	int count = 1;
    	
    	while(count < 10)
    	{
    		cout << " The number after " << count;
    		increment(count);
    		cout << " is " << count << endl;
    	}
    	
    	return 0;
    }
    
    
    void increment (int nextNumber)
    {
    	nextNumber++;
    }
    I also had a thought that maybe the function should return an integer value, but I'm not sure if that's the right track.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pianoman\
    I also had a thought that maybe the function should return an integer value, but I'm not sure if that's the right track.
    It is a right track. Recall that unless the parameter is a reference, changes to the value of the parameter only affect the local copy of the argument, not the argument from the caller. Hence, if you want changes to the value of the parameter to be reflected in the caller, typically you would either return a value and leave it up to the caller to use the return value, or you would make the parameter a reference (i.e., an "in/out" or "input/output" parameter, because you are both using its value as input, and using it as output to the caller), or provide another reference parameter (i.e., an "out" or "output" parameter).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    So I've implemented braces on the while loop, changed the return data type on increment() from void to int, and returned a value to main. However, it's still in a loop, and I can't seem to find a solution without scrapping the function.
    Code:
    #include <iostream>
    using namespace std;
    
    
    int increment(int);
    
    
    int main()
    {
    	int count = 1;
    	
    	while(count < 10)
    	{
    		cout << " The number after " << count;
    		increment(count);
    		cout << " is " << count << endl;
    	}
    	
    	return 0;
    }
    
    
    int increment (int nextNumber)
    {
    	nextNumber++;
    	return nextNumber;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ah, but you're not really "returning" the value to main(), you're just returning the value but never actually using that returned value in main().

    By the way I really suggest you pass the variable by reference instead of by value, then you won't need to actually "return" the value to the calling function.

    I suggest you don't just get rid of the function. Part of this assignment is about learning how to properly pass variables to and from functions.

  7. #7
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Okay, I've revised it to pass by reference and it produces the correct output now. I'm still a little confused though because the problem said that it had "three separate errors, each of which would cause an infinite loop." I've only found two errors which would cause an infinite loop: the passing by value and the missing braces on the while loop. Is there another one that I fixed inadvertently? Here is the final correct code:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int increment(int&);
    
    
    int main()
    {
        int count = 1;
        
        while(count < 10)
        {
            cout << " The number after " << count;
            increment(count);
            cout << " is " << count << endl;
        }
        
        return 0;
    }
    
    
    int increment (int& nextNumber)
    {
        nextNumber++;
    }
    Last edited by pianoman\; 05-04-2018 at 12:55 AM. Reason: Typos

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you remove the first cout, you can fix the code just by adding a reference without adding braces.

    The braces mean you can add the cout without causing the code to lock up as an unintended consequence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help troubleshooting
    By blackfox_1109 in forum C Programming
    Replies: 2
    Last Post: 03-03-2014, 04:10 PM
  2. SIGTRAP troubleshooting
    By ~Kyo~ in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2011, 03:26 PM
  3. Troubleshooting Please
    By jsking09 in forum C Programming
    Replies: 3
    Last Post: 03-03-2010, 02:37 AM
  4. Troubleshooting
    By hockeydrummer88 in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 02:17 PM
  5. Troubleshooting DDE
    By musicafterhours in forum Windows Programming
    Replies: 3
    Last Post: 11-18-2001, 08:26 AM

Tags for this Thread