Thread: My C++ program keeps crashing

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    My C++ program keeps crashing

    For a programming assignment, I have to ask the user for a number and determine if it is an abundant number or not and report that back to the user.

    For some reason, when I enter a number to test this program, the program abruptly crashes after entering the number.

    Code:
    bool abundantTest(int n){					// boolean function to test if a number is abundant
    	bool isAbundant = false;				// boolean flag to determine if the number is abundant
    	int sum=0;
    
    	for (int i = 0; i < n; i++){			// loop to check for the proper divisors of the number 'n'
    
    		if (n % i == 0){
    			sum = sum + i;
    		}
    
    	}
    
    	if (sum > n){
    		return true;
    	}
    	else{
    		return false;
    	}
    
    }
    int main(){
    	int num;
    	std::cout << "Enter a number to test if it is an abundant number: \n";
    	cin >> num;
    	abundantTest(num);
    	if (abundantTest(num) == true){
    		std::cout << "The number " << num << " is an abundant number.\n";
    	}
    	else if(abundantTest(num) == false){
    		std::cout << "The number " << num << " is NOT an abundant number.\n";
    	}
    	else{
    
    	}
    	
    
    
    }
    I'm not sure why it is crashing. If anyone has any insight into this issue, your help would be greatly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,160
    On the very first iteration of the loop, you compute: n % 0, effectively trying to divide by 0.

    By the way:
    • You don't make use of isAbundant
    • You call abundantTest up to three times when you only need to call it once.
    • abundantTest either returns true or false. There is no other possibility, so you have an else branch that will never be reached.
    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

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Oh! Haha! I should have caught that one! That's one of those "so stupid that I can't believe I didn't catch it" kind of errors. Thanks for noticing! And I also don't know why I had that third else branch in there. Thanks again!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,160
    Quote Originally Posted by Tech2011
    And I also don't know why I had that third else branch in there.
    As a matter of style, I would suggest:
    Code:
    int main() {
        std::cout << "Enter a number to test if it is an abundant number: \n";
        int num;
        if (cin >> num) {
            if (abundantTest(num)) {
                std::cout << "The number " << num << " is an abundant number.\n";
            }
            else {
                std::cout << "The number " << num << " is NOT an abundant number.\n";
            }
        }
        else {
            // report an input error to the user
            // ...
        }
    }
    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
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Don't you also have a error here where you use n instead of num? if (n % i == 0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing
    By AlexD in forum C Programming
    Replies: 2
    Last Post: 03-13-2012, 04:56 PM
  2. Program keeps crashing
    By o.fithcheallaig in forum C Programming
    Replies: 25
    Last Post: 10-20-2011, 03:57 AM
  3. Program crashing
    By krinl in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 10:07 AM
  4. Program crashing
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2010, 09:55 AM
  5. Program crashing when I use IO
    By legit in forum C++ Programming
    Replies: 9
    Last Post: 05-31-2009, 07:54 AM