Thread: Why is this program exiting before waiting for character input?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176

    Why is this program exiting before waiting for character input?

    Code:
    #include <stdio.h>
    
     int number,powerraise,answer,j;
    
    long int x_to_the_n(int a,int b);
    
    int main(void)
    {
    	printf("Input the number:");
    	
    	scanf("%i",&number);
    
    	printf("\nWhat power to raise it to?");
    	scanf("%i",&powerraise);
    	
    	answer=x_to_the_n(number,powerraise);
    	
    	printf("%i raised to the power of %i is %i",number,powerraise,answer);
    	
    	
    	getchar();
    	return 0;
    	
    } 
    
    
    	
    	long int x_to_the_n(int a,int b)
    	{
    	
    		for(j=1;j<powerraise;j++)
    		{
    			a *=a;		
    		}
    			
    		return a;
    			
    	}
    Thanks for your guyses help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your getchar() just eats the newline left behind by your previous scanf call.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Quote Originally Posted by Salem View Post
    Your getchar() just eats the newline left behind by your previous scanf call.
    Thanks. I think I remember something about this before. Was there an elegant solution to this other than not using scanf? I'll try the double getchar(); for a bit just to get something up and going.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Take
    Code:
    while( !feof(stdin) && getchar()!='\n' );
    directly after every scanf.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or use fgets() with a reasonably large buffer, and then use some conversion function on the resulting buffer in memory.
    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. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  4. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  5. getting input from another program
    By adr in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2006, 03:29 AM