Thread: Functions- newbie nedds rescue

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

    Functions- newbie nedds rescue

    Help me please (I will master C yet!!) . Soon I will have 2 weeks to master the syntax and functions but now I need help.
    Right now I cannot even see how the rest of my coding is because when I execute, the program jumps right to the default case of the switch and regardless of input keep doing so. Any help with this (and any and all the error I cannot see or debug because of this) would be appreciated.

    Code:
    #include <stdio.h>
    int addint (int x, int y);
    int subint (int x, int y);
    int multint (int x, int y);
    int divideint (int x, int y);
    int powerint (int x, int y);
    int gcdint (int x, int y);
    int factorial (int x);
    
    int main()
    {
    int x=0;
    int y=0;
    char select;
    printf("This program will perform one of the following operations \n");
    printf("to two numbers based on your selection from the following menu: \n \n");
    printf("1. Add two integers. \n");
    printf("2. Subtract two intergers. \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");
    scanf("%d", &x);
    printf("\n ");
    scanf("%d", &y);
    printf("\n ");
    printf("Please choose desired operation from above menu. \n  \n");
    scanf("%c", &select);
      while (select != 'q' && select != 'Q')
      {
    	switch (select)
    	{
    	case '1':
    	  printf("The sum of  %d and  %d is  %d.", x, y,addint(x,y));
    	  break;
    
        case '2':
    	  printf("%d minus  %d is  %d", x, y, subint(x,y));
    	  break;
    
    	case '3':
    	  printf("%d multiplied by  %d is  %d", x, y, multint(x,y));
    	  break;
    
    	case '4':
    	  printf(" %d divided by %d is   %d", x, y, divideint(x,y));
    	  break;
    
    	case '5': 
    		printf(" %d raised to %d is  %d", x, y, powerint(x,y));
    		break;
    
    	case '6':
    	    printf("The greatest common denominator of %d and %d is %d", x, y, gcdint(x,y));
    		break;
    
    	case '7':
    		printf(" The value of the factorial for %d is    %d", x, factorial(x));
            break;
    
        default:
    		printf("Incorrect value entered, please reenter.\n \n ");
    		break;
    	
    	}
     
    printf("This program will perform one of the following operations \n");
    printf("to two numbers based on your selection from the following menu: \n \n");
    printf("1. Add two integers. \n");
    printf("2. Subtract two intergers. \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");
    scanf("%d", &x);
    printf("\n ");
    scanf("%d", &y);
    printf("\n ");
    printf("Please choose desired operation from above menu. \n \n");
    scanf("%c", &select);
      }
    
      printf("You have terminated the program.");
      return 0;
    }
    
    	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)
    	{
    		if (y > 0 || y<0)
    		 return x/y;
    		else 
    		printf("Error - division by O.\n");
    
    	}
    
    	int powerint (int x, int y)
    	{
    		if (y==1)
    			return 1;
    		else 
    		if (y<0)
    		 printf("You cannot raise a number to a negative power!\n");
    		else 
    		 return  x * powerint(x, y-1);
    	
    	}
    
    	int gcdint (int x, int y )
    	{  /* will add this function in */
    	}
    
    	int factorial (int x)
    	{
    		if (x >= 0)
    		 return  x * factorial (x-1);
    		else 
    		if (x<0)
    		  printf("Error, negative factorial value entered.");
    	}
    Thank you,

    Signed frustrated but will work through it !!!!

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Try to see what's the value stored in the variable select. In all probabilities it'll be a newline character.
    Have to hurry with my own code otherwise would have given you the code, for now kindly do with the hint.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is that there's a newline character in the input stream, and when you call your final scanf, it reads that as the "%c" character. You'll need to clean out the input stream before that call. (Scanf isn't that great of a function for dealing with input. Consider reading the FAQ on how to read a number from the user.)

    For starters, don't even waste time with fflush, because contrary to what many believe, you cannot use it on input streams. It's only for output streams.

    Therefore, we write our own little function, or bit of code, and call that when we need to clear out the buffer.
    Code:
    void flin( void )
    {
        int c;
        while( (c = fgetc( stdin )) != '\n' );
    }
    Now you call this right after you call scanf, and it will clear out any extra crap they have typed in.
    Code:
    printf("Please enter two integers: \n\n");
    scanf("%d", &x);
    flin();
    printf("\n ");
    scanf("%d", &y);
    flin();
    printf("\n ");
    printf("Please choose desired operation from above menu. \n  \n");
    scanf("%c", &select);
    flin();
    If you were so incliined, you could even call it at the start of the program, just to be sure:
    Code:
    printf("Press enter to begin...\n");
    flin();
    Quzah.
    Last edited by quzah; 03-03-2005 at 06:32 AM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can get rid of some extraneous code by changing the way you display your menu and ask for user input. Currently you are using a while loop:

    Code:
    Display menu.
    Ask for user input.
    While user did not quit.
        Switch based on user input.
        Display menu.
        Ask for user input.
    As you should see you are displaying and asking for user input twice. If you change to a do while loop you can cut out some of that:

    Code:
    Do
        Display menu.
        Ask for user input.
        Switch based on user input.
    While user did not quit.
    You would however need to either add an entry for the switch that simply does a break for case 'q' and case 'Q', or you would have to handle the quit condition in an if test before the switch that could then call either continue or break or skip over the switch statement entirely.
    Last edited by hk_mp5kpdw; 03-03-2005 at 08:00 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    First off, you have your int values in enclosed in ' ' marks. Secondly, I see that you have the same menu output thingy written out in code TWICE. You should never have to do this....Usually it's a better idea to create a function to do this and just go ahead and call the guy if you need to keep re-using code. Thirdly, I noticed you said at some point: "You cannot raise a number to a negative power," which made me laugh.

    You most certainly can raise a number to a negative power, with great ease. Say we raise:
    5^-3

    It would simply be: 1/(5^3), or 1/125. You can easily make your program do the float division to get the answer.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Krak
    First off, you have your int values in enclosed in ' ' marks.
    Around here, we call that a character constant. You know, like a letter, but in this case, a number. See, it's not the decimal value one. It is the character one. As in, you type 1 on your keyboard, and it enters the character one, not the decimal one. That's why it's in single quotes.

    It's ok, you can feel silly now. Really, we're laughing with you.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Still am puzzled!! added a printf to code right before switch to see what "select" had stored. It came back with nothing ??!! What am I missing or should I try to correct this??

    Thanks,

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read my first post over again.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    11
    Sorry, I should have posted the revised code with the changes I made-- I have taken out the printf's with the newlines and it is still giving me trouble. I will repost the code soon.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, the printf statements aren't the problem. When you type something into your program what do you do? You type some stuff, and then you... hit enter.

    This enter key stroke is in the input stream. Your use of scanf leaves it in there. So the next call starts with that. That's why it gets skipped. That's why you use a function like I've suggested to read everything up to, and including the newline, and discard it. That way, when you start reading again, you start past the enter key stroke, and begin with the new data.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM