Thread: Failed assertion error

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    Failed assertion error

    Hi

    i created a program but I have an error about an assertion failure. Any ideas on how to correct this?

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cassert>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	int n; // number that the user enters
    	int i; // used to iterate through an array
    	int number[10]; // array to store the 10 command line inputs
    	int index = -1; // index at which the integer is found
    	bool found = false; // flag that tells whether the search was successful
    	
    	//Check to see if the command line input was correct
    	if( argc != 11 )
    	{
    		cerr << "Usage: n1 n2 n3 n4 n5 n6 n7 n8 n9 n10\n" ;
    		exit(1) ;
    	}
    	
    	//Store the 10 command line integers
    	for ( i = 0; i < 10; i++ )
    	{
    		number[i] = atoi( argv[i+1] ) ;
    		assert( 1 <= number[i] && number[i] <= 1000 ) ;
    	}
    	
    	//User enters integer to be searched
    	cout << "Enter Number: " << endl ;
    	cin >> n;
    	
    	//Loop through the array and look for the integer
    	for ( i = 0; i < 10; i++ )
    	{
    		if ( n == number[i] )
    		{
    			index = i;
    			found = true;
    			
    			break; 
    		}
    	}
    	
    	//Display result
    	if ( found )
    		cout << "\nFound at index: " << index << endl;
    	else
    		cout << "\nNOT FOUND" << endl;
    
    	return 0;
    }
    I get the error failed assertion `1 <= number[i] && number[i] <= 1000'
    Abort

    Any ideas to correct this would be great.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what numbers are you actually giving on the command line?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    So, what numbers are you actually giving on the command line?

    --
    Mats
    4 39 5 3 0 34 9 99 66 44

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ programming forum.

    Quote Originally Posted by rushhour
    4 39 5 3 0 34 9 99 66 44
    I notice that 0 is an invalid input. Incidentally, instead of using an assertion, you should print out an error message and possibly end the program immediately. Only after you have tested that user input is correct do you use assertions to check that pre-conditions are met.
    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
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    Moved to C++ programming forum.


    I notice that 0 is an invalid input. Incidentally, instead of using an assertion, you should print out an error message and possibly end the program immediately. Only after you have tested that user input is correct do you use assertions to check that pre-conditions are met.
    I need a zero though, so dont I just change

    Code:
    	assert( 1 <= number[i] && number[i] <= 1000 )
    to

    Code:
    	assert( 0 <= number[i] && number[i] <= 1000 )
    Is this allowed? I'm just a little unsure on this. All other numbers work correctly, it's just the zero isn't it?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rushhour
    Is this allowed?
    Of course, the expression that you assert is entirely up to you. It is a question of deciding what command input are you going to accept as valid. Nonetheless, I still think that you should not be using an assertion at this point. That the user enters invalid input is not a violation of some internal pre-condition. You might throw an exception instead, but the most consistent way of handling it is to print an error message, just like you did for the case where the user enters an invalid number of command line arguments.
    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

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    Of course, the expression that you assert is entirely up to you. It is a question of deciding what command input are you going to accept as valid. Nonetheless, I still think that you should not be using an assertion at this point. That the user enters invalid input is not a violation of some internal pre-condition. You might throw an exception instead, but the most consistent way of handling it is to print an error message, just like you did for the case where the user enters an invalid number of command line arguments.
    Ok thanks for the advice.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    assert is a debugging tool. If NDEBUG is defined, the assert will be completely removed from the code. But you still want to validate user input, whether you have a debug version or not.

    So use assert to find programming errors. User giving incorrect arguments to the program is not a programming error - however responding inadequatly - or not at all - is.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM