Thread: sooo close - need a little help

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    sooo close - need a little help

    Ohh so close to getting the program to do what is required!!
    Below is the code. I have two problems I have not been able to figure out:
    1) I need input to accept only integer input (but as is it will accept float type)
    2) On the "division" printf statement, I can get it to echo the inputted values and the x/y correctly, but cannot get the remainder to display correctly. Can anyone help with the simple solutions that I cannot seem to figure out?
    Thanks!!!

    Code:
    #include <stdio.h>
    int remainder ;
    int addint (int x, int y); /*Function Prototypes for actions*/
    int subint (int x, int y); /* to be performed on input */
    int multint (int x, int y);
    int divideint (int x, int y, int z);
    int powerint (int x, int y);
    int gcdint (int x, int y);
    int factorial (int x);
    
    int main()
    {
    
       int x=0;  /* Variable initializations */
       int y=0;
       int z=0;
       char select;
       printf("This program will perform one of the following operations \n"); /* User Menu for program */
       printf("to two numbers based on your selection from the following menu: \n \n");
       printf("1. Add two integers. \n");
       printf("2. Subtract two integers. \n");
       printf("3. Multiply two integers. \n");
       printf("4. Divide two integers. \n");
       printf("5. Calculate integer raised to a positive power. \n");
       printf("6. Compute greatest common divisor of two integers. \n");
       printf("7. Compute the factorial of an integer. \n \n");
       printf("To quit the program at any time, please press q or Q. \n \n");
       printf("Please enter two integers: \n\n"); /* Prompt user for two integers */
       fflush(stdin);
       scanf("%d", &x);
       scanf("%d", &y);
       printf("Please choose desired operation from above menu. \n "); /* Prompt user to select desired action */
       fflush(stdin);
       scanf("%c", &select);
        while (select != 'q' && select != 'Q') /* Continues running program unitl sentinel value entered */
    	{
      
    	   switch (select)  /* Invoke the proper function to perform required action */
    	   {
        	case '1':
    	    printf("\n The sum of  %d and  %d is  %d.", x, y,addint(x,y));
    	    break;
    
            case '2':
    	    printf("\n %d minus  %d is  %d", x, y, subint(x,y));
    	    break;
    
    	    case '3':
    	    printf("\n %d multiplied by  %d is  %d", x, y, multint(x,y));
    	    break;
    
    	    case '4':
    		if(y >0)
    		 printf("\n %d divided by %d is   %d and remainder is %d", x,y,divideint(x,y,z));
    		else /*Else statement to handle division by 0 */
    		 printf("\n Error,  division by 0.  \n"); 
    		break;
    
    	    case '5':
    		 if (y>=0)
    	      printf("\n %d raised to power of %d is  %d ", x, y, powerint(x,y));
    		 else /*Else statement to handle raising to a negative power */
    	      printf("\n You cannot raise a number to a negative power!\n");	  
    	    break;
    
    	    case '6':
    	    printf("\n The greatest common denominator of %d and %d is %d", x, y, gcdint(x,y));
    	    break;
    
    	    case '7':
    	     if (x<0 || y<0) /*if statement to handle a negative factorial */
    
    	      printf("\n Error, negative factorial value entered. \n");
    	     else
    	      printf("\n The value of the factorial for %d is    %d \n", x, factorial(x));
    	      printf("\n The value of the factorial for %d is    %d", y, factorial(y));
    	  
            break;
    
            default: 
             printf("Incorrect value entered, please reenter. \n \n");
            break;
    	
      }
     
       printf("\n \n This program will perform one of the following operations \n"); /* Reposts "menu" and reprompts user for input */
       printf("to two numbers based on your selection from the following menu: \n \n");
       printf("1. Add two integers. \n");
       printf("2. Subtract two integers. \n");
       printf("3. Multiply two integers. \n");
       printf("4. Divide two integers. \n");
       printf("5. Calculate integer raised to a positive power. \n");
       printf("6. Compute greatest common divisor of two integers. \n");
       printf("7. Compute the factorial of an integer. \n \n");
       printf("To quit the program, please press q or Q. \n \n");
       printf("Please enter two integers: \n\n");
       fflush(stdin);
       scanf("%d", &x);
       scanf("%d", &y);
       printf("Please choose desired operation from above menu. \n ");
       fflush(stdin);
       scanf("%c", &select);
       }
    
      printf("You have terminated the program.\n");
      return 0;
    }
        /* Function Definitions */
    	int addint (int x, int y)
    	{
    	 return x + y;	  
    	}
    
    	int subint (int x, int y)
    	{
    	  return x - y;
    	}
    
    	int multint (int x, int y)
    	{ 
    	  return x * y;
    	}
    
    	int divideint (int x, int y, int z)
    	{   
    		if (y != 0)
    		{	     
    		return x/y;
            return z=x%y;
    		}
    	}
    
    	int powerint (int x, int y)
    	{
    		if (y==0)
    		 return 1;
    		else 
    		 return (x *  powerint(x , y-1));	
    	}
    	
    	int gcdint (int x,int y)
    	{
    
            if (y!=0)
             return (gcdint(y, x%y));
            else
             return x;
    }
    
    	int factorial (int x, int y)
    	{ 
              if (x == 0)
                return 1;
               else 
    		  if (x >0)
    			return x * factorial(x-1);
              if (y == 0)
                return 1;
               else 
    		   if (y>0)
    			  return y * factorial(y-1);
            }

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Code:
    printf("\n %d divided by %d is %d and remainder is %d", x,y,divideint(x,y,z), (x%y));
    And I say again..you most certainly CAN...with great ease, raise a number to a negative power.
    Last edited by Krak; 03-05-2005 at 01:24 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    hello bnmwad,

    by looking at your code i found some few error. which i have highlighted in the your programe

    Code:
    #include <stdio.h>
    int remainder ;
    int addint (int x, int y); /*Function Prototypes for actions*/
    int subint (int x, int y); /* to be performed on input */
    int multint (int x, int y);
    int divideint (int x, int y, int z);
    int powerint (int x, int y);
    int gcdint (int x, int y);
    int factorial (int x);
    
    int main()
    {
    
       int x=0;  /* Variable initializations */
       int y=0;
       int z=0;
       char select;
       printf("This program will perform one of the following operations \n"); /* User Menu for program */
       printf("to two numbers based on your selection from the following menu: \n \n");
       printf("1. Add two integers. \n");
       printf("2. Subtract two integers. \n");
       printf("3. Multiply two integers. \n");
       printf("4. Divide two integers. \n");
       printf("5. Calculate integer raised to a positive power. \n");
       printf("6. Compute greatest common divisor of two integers. \n");
       printf("7. Compute the factorial of an integer. \n \n");
       printf("To quit the program at any time, please press q or Q. \n \n");
       printf("Please enter two integers: \n\n"); /* Prompt user for two integers */
       fflush(stdin);
       scanf("%d", &x);
       scanf("%d", &y);
       printf("Please choose desired operation from above menu. \n "); /* Prompt user to select desired action */
       fflush(stdin);
       scanf("%c", &select);
        while (select != 'q' && select != 'Q') /* Continues running program unitl sentinel value entered */
    	{
      
    	   switch (select)  /* Invoke the proper function to perform required action */
    	   {
        	case '1':
    	    printf("\n The sum of  %d and  %d is  %d.", x, y,addint(x,y));
    	    break;
    
            case '2':
    	    printf("\n %d minus  %d is  %d", x, y, subint(x,y));
    	    break;
    
    	    case '3':
    	    printf("\n %d multiplied by  %d is  %d", x, y, multint(x,y));
    	    break;
    
    	    case '4':
    		if(y >0)
    		 printf("\n %d divided by %d is   %d and remainder is %d", x,y,divideint(x,y,z));
    		else /*Else statement to handle division by 0 */
    		 printf("\n Error,  division by 0.  \n"); 
    		break;
    
    	    case '5':
    		 if (y>=0)
    	      printf("\n %d raised to power of %d is  %d ", x, y, powerint(x,y));
    		 else /*Else statement to handle raising to a negative power */
    	      printf("\n You cannot raise a number to a negative power!\n");	  
    	    break;
    
    	    case '6':
    	    printf("\n The greatest common denominator of %d and %d is %d", x, y, gcdint(x,y));
    	    break;
    
    	    case '7':
    	     if (x<0 || y<0) /*if statement to handle a negative factorial */
    
    	      printf("\n Error, negative factorial value entered. \n");
    	     else
    	      printf("\n The value of the factorial for %d is    %d \n", x, factorial(x));
    	      printf("\n The value of the factorial for %d is    %d", y, factorial(y));
    	  
            break;
    
            default: 
             printf("Incorrect value entered, please reenter. \n \n");
            break;
    	
      }
     
       printf("\n \n This program will perform one of the following operations \n"); /* Reposts "menu" and reprompts user for input */
       printf("to two numbers based on your selection from the following menu: \n \n");
       printf("1. Add two integers. \n");
       printf("2. Subtract two integers. \n");
       printf("3. Multiply two integers. \n");
       printf("4. Divide two integers. \n");
       printf("5. Calculate integer raised to a positive power. \n");
       printf("6. Compute greatest common divisor of two integers. \n");
       printf("7. Compute the factorial of an integer. \n \n");
       printf("To quit the program, please press q or Q. \n \n");
       printf("Please enter two integers: \n\n");
       fflush(stdin);
       scanf("%d", &x);
       scanf("%d", &y);
       printf("Please choose desired operation from above menu. \n ");
       fflush(stdin);
       scanf("%c", &select);
       }
    
      printf("You have terminated the program.\n");
      return 0;
    }
        /* Function Definitions */
    	int addint (int x, int y)
    	{
    	 return x + y;	  
    	}
    
    	int subint (int x, int y)
    	{
    	  return x - y;
    	}
    
    	int multint (int x, int y)
    	{ 
    	  return x * y;
    	}
    
    	int divideint (int x, int y, int z)
    	{   
    		if (y != 0)
    		{	     
    		return x/y;
            return z=x%y;		}
    	}
    
    	int powerint (int x, int y)
    	{
    		if (y==0)
    		 return 1;
    		else 
    		 return (x *  powerint(x , y-1));	
    	}
    	
    	int gcdint (int x,int y)
    	{
    
            if (y!=0)
             return (gcdint(y, x%y));
            else
             return x;
    }
    
    	int factorial (int x, int y)
    	{ 
              if (x == 0)
                return 1;
               else 
    		  if (x >0)
    			return x * factorial(x-1);
              if (y == 0)
                return 1;
               else 
    		   if (y>0)
    			  return y * factorial(y-1);
            }
    these are some of the errors which i found in your program.

    look at your divident function. the fucniotn retures two value which is not possible. you can send only one value at time. one thing you can do is dereference the x variable to *x so that you can change value in the function, which could effect main.

    here is the code which i mean

    Code:
    printf("The didident of %d an %d is %d and the remainder is %f",x,y,divident(x,y,&z));
    . 
    .
    .
    
    int divident(int x, int y, int *z)
    {
           .
           . 
           .
           *z=x/y;
           return x/y;
    }
    one more thing which i found fflush(stdin). which you shoudn't use that. there us smal artical on the FAQ about this go through it

    never use fflush(stdin);

    s.s.harish

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Thanks

    I have made the changes you suggested. Thanks for yur help!!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. how do i close one window at a time?
    By stde in forum Windows Programming
    Replies: 4
    Last Post: 08-01-2006, 12:43 PM
  3. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 10:39 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM