# Thread: Functions- newbie nedds rescue

1. ## 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("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");
scanf("%d", &x);
printf("\n ");
scanf("%d", &y);
printf("\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("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");
scanf("%d", &x);
printf("\n ");
scanf("%d", &y);
printf("\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. 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. 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 ");
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.

4. 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.
While user did not quit.
Switch based on 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
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.

5. 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. 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.

7. 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. Read my first post over again.

Quzah.

9. 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. 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.