Thread: Hello, New on Here and i have my first question

  1. #1
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16

    Thumbs up 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:

    Please Enter in:

    + for addition
    - 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 ("\n\nPlease Enter in:\n\n");
    printf ("+ for addition\n");
    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***/

  2. #2
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    if there are any other comments about style etc please let me know.

    thanks, PO

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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?
    Last edited by zacs7; 07-07-2008 at 09:47 PM.

  4. #4
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    Quote Originally Posted by zacs7 View Post
    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

    Please Enter in:

    + for addition
    - 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
    Last edited by PunchOut; 07-07-2008 at 10:02 PM.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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. #6
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    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. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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;
    What about division by zero?

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

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16
    it seemed to compile and run ok with gcc.

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

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed