NEED help with a Error checking

This is a discussion on NEED help with a Error checking within the C Programming forums, part of the General Programming Boards category; i have made a program that allows u to enter a number, if the number is less than or equal ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    NEED help with a Error checking

    i have made a program that allows u to enter a number, if the number is less than or equal to zero then it will goto the start and ask to enter numbers again, im using miracle c - using the scanf function.

    when i run the program and enter a char into the int variable it jsut keeps looping continuasly

    thankx for your help

    Paul
    Shizzle Ma 'C' Whizzle's

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    what if scanf() read EOF? Then getchar() will never return '\n'
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Error Checking

    cheers, al hav a go, wat duz it accually do?
    Shizzle Ma 'C' Whizzle's

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    Code:
    void surfacearea()
    {
    	
    	redraw:
    	pcra=0;
    	pcrb=0;
    	pcrc=0;
    	pcrs=0;
    	pcrarea=0;
    	pcrr=0;
    	pcrh=0;
    	pcrR=0;
    	pcrr=0;
    	pcrv=0;
    	
    	clrscr();
    	printf("\n\n\t\t  ********************************************");
    	printf("\n\t\t |  The surface area of an enclosed cylinder  |\n");	
    	printf("\t\t  ********************************************");
    
    	printf("\n\n\t\t Please enter the Radius (an integer number): ");
    	if ( scanf( "%d", &pcrr ) != 1 ) 
    	{
    	  
    	} 
    	//while ( getchar() != '\n' );
    	//{
    		
    
    //		scanf("%d",&pcrr);
    		printf("\n\t\t Please enter the Height (an integer number): ");
    		scanf("%d",&pcrh);
    		printf("*%d*",pcrr);
    		hold();
    		
    		/*if (pcrr <= 0 || pcrh <= 0)
    		{
    			printf("\n\n\t\t...You have entered an incorrect value...");
    			hold();
    			goto redraw;	
    		}*/
    		
    		pcrarea = 2 * pcrpi * pcrr * (pcrr + pcrh);
    		printf("\n\t\t The answer is: %0.2f",pcrarea);
    		hold();
    	//}
    
    
    }
    i have tried it a few diff ways round, but it keeps waiting for key presses,



    Code Tags added by Kermi3
    Shizzle Ma 'C' Whizzle's

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    SORTED!!! THANKX PEOPLE, WHAT DOES THAT CODE DO ? I NEED COMMENTS IF POSS

    Code:
    void surfacearea()
    {
    	
    	pcrr=0;
    	pcrh=0;
    		
    	clrscr();
    	printf("\n\n\t\t  ********************************************");
    	printf("\n\t\t |  The surface area of an enclosed cylinder  |\n");	
    	printf("\t\t  ********************************************");
    
    /**/	printf("\n\n\t\t Please enter the Radius (an integer number): ");
    	if ( scanf( "%d", &pcrr ) != 1 ) {
    		// throw away unrecognisable chars
    	while ( getchar() != '\n' );
    	} else {
    		// do stuff with valid input in pcrr
    		printf("\n\t\t Please enter the Height (an integer number): ");
    		if ( scanf( "%d", &pcrh ) != 1 ) {
    			// throw away unrecognisable chars
    		while ( getchar() != '\n' );
    		} else {
    			// do stuff with valid input in pcrh
    			pcrarea = 2 * pcrpi * pcrr * (pcrr + pcrh);
    			printf("\n\t\t The answer is: %0.2f",pcrarea);
    			
    		}
    	}
    	hold();
    }


    &#91;code]&#91;/code]tagged by Salem
    Shizzle Ma 'C' Whizzle's

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    One comment.... read this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by Salem
    You need to use scanf like this
    I was surprised by a recommendation for scanf. So I thought I'd throw some comments on scanf out here.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Dave_Sinkula
    I was surprised by a recommendation for scanf. So I thought I'd throw some comments on scanf out here.
    It wasn't a recommendation, imo. It was answer to a specific problem (paulroseby was asking how to use scanf() ).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21