Hello, New on Here and i have my first question

This is a discussion on Hello, New on Here and i have my first question within the C Programming forums, part of the General Programming Boards category; i have been working on this simple c calculator program. it runs ok for me until it hits the loop ...

1. Hello, New on Here and i have my first question

i have been working on this simple c calculator program. it runs ok for me until it hits the loop and repeats the option to:

- for subtraction
x for multiplication
/ for division
Any Key to Quit

Below is the source Code

Code:
```#include <stdio.h>

/**************************************************************/
/*                  Main  Function                            */
/**************************************************************/

main ()
{

char ch;

while (ch != 'q')
{
printf ("- for subtraction\n");
printf ("x for multiplication\n");
printf ("/ for division\n");
printf ("Any Key to Quit\n\n\n");
scanf ("%c", &ch);
Decision(ch);
}

}

/**************************************************************/
/*                  Decision Function                         */
/**************************************************************/

Decision(ch)
{
float a,b,c;

if (ch == '+')
{
printf ("Please enter in the next two number in which you would like to add\n");
scanf ("%f", &a);
scanf ("%f", &b);
c = a + b;
printf ("Your Total is: %f\n", c);
}

else if (ch == '-')
{
printf ("Please enter in the next two number in which you would like to subtract\n");
scanf ("%f", &a);
scanf ("%f", &b);
c = a - b;
printf ("Your Total is: %f\n", c);
}
else if (ch == '*')
{
printf ("Please enter in the next two number in which you would like to multiply\n");
scanf ("%f", &a);
scanf ("%f", &b);
c = a * b;
printf ("Your Total is: %f\n", c);
}

else if (ch == '/')
{
printf ("Please enter in the next two number in which you would like to divide\n");
scanf ("%f", &a);
scanf ("%f", &b);
c = a / b;
printf ("Your Total is: %f\n", c);
}

}/***Decision End***/```

thanks, PO

3. 1. Learn how to indent
2. You could use a switch in place of all those "else if"'s

The standard (C89) states, it's either "int main(void)" or "int main(int argc, char * argv[])". Not "main()" Although that doesn implicitly mean "int main()" -- which is still wrong. Same goes for "Decision()", declare the functions explicitly, ie, "int Decision(void)".

Other than that, what is your question?

4. Originally Posted by zacs7
1. Learn how to indent
2. You could use a switch in place of all those "else if"'s

The standard (C89) states, it's either "int main(void)" or "int main(int argc, char * argv[])". Not "main()" Although that doesn implicitly mean "int main()" -- which is still wrong. Same goes for "Decision()", declare the functions explicitly, ie, "int Decision(void)".

Other than that, what is your question?
thanks for the info.

my problem is that when i run this program and calculate the first equation it gives the answer correctly and all but it displays

- for subtraction
x for multiplication
/ for division
Any Key to Quit

an additional time. i am just wondering why it is doing this.

i am going to mess around with switch and see if this will correct the problem.

not to sound like a noob, but can you should me what i am doing wrong with my indents? should they be smaller or bigger. i am also using vi, is there another program that i should be using besides emacs(i am kinda intimidated of it :x).

thanks, PO

5. Because there are still characters left in the stream (the newline character), http://faq.cprogramming.com/cgi-bin/...&id=1043284392. Or use fgets() and parse the line you read with sscanf().

ie the user enters, "+" the stream will then contain, "+\n" and you pull the "+" out with the scanf() and "\n" is still left in the stream, so the next scanf() reads the "\n" and returns straight away.

Choose a style: http://cpwiki.sourceforge.net/Indentation
And stick with it, it's important to be consistant (and your current style is not), you indent on some braces but not others?

6. Thanks zacs7,

a finished product! (i had been working on this stupid program and trying different loops etc for 2 weeks!!! before i found this site)

does the indenting look ok now?

Code:
```#include <stdio.h>

/***************************/
/*** main functions!!!!! ***/
/***************************/

int main (void)
{
char ch;
while(ch == '+' || '-' || '*' || '/')
{
printf("Please Choose From + - * / :");
scanf("\n&#37;c%*[^\n]%*c", &ch);
equation(ch);
}
}

/*********************************/
/*** equation function!!!!!!! ****/
/*********************************/

equation(ch)

{
float a,b,c;

switch(ch)
{
case '+' : printf("Please enter in each number followed by hitting the enter key\n");
scanf("%g", &a);
scanf("%g", &b);
c = a + b;
printf("%g + %g = %g\n", a,b,c);
break;
case '-' : printf("Please enter in each number followed by hitting the enter key\n");
scanf("%g", &a);
scanf("%g", &b);
c = a - b;
printf("%g - %g = %g\n", a,b,c);
break;
case '*' : printf("Please enter in each number followed by hitting the enter key\n");
scanf("%g", &a);
scanf("%g", &b);
c = a * b;
printf("%g * %g = %g\n", a,b,c);
break;
case '/' : printf("Please enter in each number followed by hitting the enter key\n");
scanf("%g", &a);
scanf("%g", &b);
c = a / b;
printf("%g / %g = %g\n", a,b,c);
break;

}
}/***equation function end***/```

7. Your indentation is excellent. However . . .
Code:
```equation(ch)
{```
That wouldn't even compile. You'd need to put "char ch;" before the curly brace -- assuming you were using the old K&R style of function declarations. Which you shouldn't. Consider this instead.
Code:
```void equation(char ch)
{```
Code:
`while(ch == '+' || '-' || '*' || '/')`
That won't work. You need
Code:
`while(ch == '+' || ch == '-' || ...)`
Or, if you want to be fancy, you could use
Code:
```#include <string.h>
while(strchr("+-*/", ch))```
Code:
```		case '/' : printf("Please enter in each number followed by hitting the enter key\n");
scanf("&#37;g", &a);
scanf("%g", &b);
c = a / b;
printf("%g / %g = %g\n", a,b,c);
break;```

That switch statement . . . you could put the printf() and two scanfs() before the switch statement, once, instead of repeating exactly the same code for every branch of the switch.

You might want to use doubles instead of floats. You get better precision with doubles.

8. it seemed to compile and run ok with gcc.

Are you saying it wont compile on other(most) c compilers out there?

9. It compiles fine, but with warnings.
Code:
```punch.c: In function ‘main’:
punch.c:14: warning: implicit declaration of function ‘equation’
punch.c: At top level:
punch.c:26: warning: return type defaults to ‘int’
punch.c: In function ‘equation’:
punch.c:26: warning: type of ‘ch’ defaults to ‘int’
punch.c:57: warning: control reaches end of non-void function```
These warnings are saying that you need a prototype, that main() needs to return int, that ch needs to have a type (probably char), and that equation() needs to have a type (say, void) or else return something.

10. Also, in main, ch is undefined at start, so you may never enter your while-loop.

You repeat the same piece of code several times:
Code:
```                           printf("Please enter in each number followed by hitting the enter key\n");
scanf("%g", &a);
scanf("%g", &b);```
Why not do it BEFORE you decide what to do with the numbers?

And if you are a little bit clever, you can replace the four lines like this:
Code:
`printf("%g / %g = %g\n", a,b,c);`
with a single line.

--
Mats