[Help] calculato program

This is a discussion on [Help] calculato program within the C Programming forums, part of the General Programming Boards category; im still new to c programming, here is the question of my assignment http://i.imgur.com/jZoDc.jpg?1 someone can help me to fix ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    [Help] calculato program

    im still new to c programming, here is the question of my assignment http://i.imgur.com/jZoDc.jpg?1

    someone can help me to fix it ?
    here is my code -

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void scan_data(char *op1p, double *num1p);
    void do_next_op(char op1, double num1, double *accum);
    
    void main()
    {
        double num1;
        char op1;
        double accum;
        accum=0;
        printf("Hit enter to turn the calculator on");
        getchar();
    
    do{
    
       scan_data(&op1, &num1);
       do_next_op(op1, num1, &accum);
    
    }
    while (!(op1 == 'q'));
    return(0);
    }
    
    
    void scan_data(char *op1p, double *num1p)
    {
    
    
        printf("\nEnter an operator [^,*,/,+,-,q (quit): ");
        scanf("%c",&op1p);
        printf("\nEnter a number to calculate for: ");
        scanf("%f",&num1p);
        printf("The result so far is %c %f \n", &op1p, &num1p);
    }
    void do_next_op(char op1, double num1, double *accum)
    {
    
            switch(op1)
            {
                case '+':
                {
                    *accum = *accum + num1;
                    break;
                }
                case '-':
                {
                    *accum = *accum - num1;
                    break;
                }
                case '*':
                {
                    *accum = *accum * num1;
                    break;
                }
                case '/':
                {
                    *accum = *accum / num1;
                    break;
                }
                case '^':
                {
                    *accum = pow(*accum, num1);
                    break;
                }
                case 'q':
                case 'Q':
                {
                    break;
                }
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    printf("\nEnter an operator [^,*,/,+,-,q (quit): ");
    scanf("%c",&op1p);
    printf("\nEnter a number to calculate for: ");
    scanf("%f",&num1p);
    printf("The result so far is %c %f \n", &op1p, &num1p);

    Your parameters are pointers.

    So
    Code:
        printf("\nEnter an operator [^,*,/,+,-,q (quit): ");
        scanf("%c",op1p);
        printf("\nEnter a number to calculate for: ");
        scanf("%f",num1p);
        printf("The result so far is %c %f \n", *op1p, *num1p);
    Also, if you're using %c to read the next non-whitespace character, it's very useful to write " %c" as the format (see the space)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    thank, that part was work but the result was messed up
    http://i.imgur.com/X3mBO.png

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Post your code.

    Also, watch your formats.
    Code:
    $ gcc bar.c -lm
    bar.c: In function ‘main’:
    bar.c:23:1: warning: ‘return’ with a value, in function returning void [enabled by default]
    bar.c: In function ‘scan_data’:
    bar.c:34:5: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]
    $ gcc bar.c -lm
    bar.c: In function ‘main’:
    bar.c:23:1: warning: ‘return’ with a value, in function returning void [enabled by default]
    $ ./a.out 
    Hit enter to turn the calculator on
    
    Enter an operator [^,*,/,+,-,q (quit): +
    
    Enter a number to calculate for: 9
    The result so far is + 9.000000
    Fixing the scanf works for me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    here the code
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void scan_data(char *op1p, double *num1p);
    void do_next_op(char op1, double num1, double *accum);
    
    void main()
    {
        double num1;
        char op1;
        double accum;
        accum=0;
        printf("Hit enter to turn the calculator on");
        getchar();
    
    do{
    
       scan_data(&op1, &num1);
       do_next_op(op1, num1, &accum);
    
    }
    while (!(op1 == 'q'));
    
    }
    
    
    void scan_data(char *op1p, double *num1p)
    {
    
    
        printf("\nEnter an operator [^,*,/,+,-,q (quit)]: ");
        scanf(" %c",&op1p);
        printf("Enter a number to calculate for: ");
        scanf("%f",&num1p);
        printf("The result so far is %c %f \n", &*op1p, &*num1p);
    }
    void do_next_op(char op1, double num1, double *accum)
    {
    
            switch(op1)
            {
                case '+':
                {
                    *accum = *accum + num1;
                    break;
                }
                case '-':
                {
                    *accum = *accum - num1;
                    break;
                }
                case '*':
                {
                    *accum = *accum * num1;
                    break;
                }
                case '/':
                {
                    *accum = *accum / num1;
                    break;
                }
                case '^':
                {
                    *accum = pow(*accum, num1);
                    break;
                }
                case 'q':
                case 'Q':
                {
                    printf("The final result was %f",*accum);
                    break;
                }
        }
    }
    still dont get it , the problem just on the scanf ?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    printf("\nEnter an operator [^,*,/,+,-,q (quit)]: ");
    scanf(" %c",&op1p);
    printf("Enter a number to calculate for: ");
    scanf("%f",&num1p);
    printf("The result so far is %c %f \n", &*op1p, &*num1p);

    Read my post again!
    You managed to add a space before %c (good), but you ignored everything else.

    > the problem just on the scanf ?
    Yes, to scan a double, it's "%lf"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    sorry , didn't notice the '&', and now is how to make the result repeated to another operation
    eg.

    Enter an operator [^,*,/,+,-,q (quit): +
    Enter a number to calculate for: 9
    + 9.00
    The result so far is + 9.00

    Enter an operator [^,*,/,+,-,q (quit): +
    Enter a number to calculate for: 9
    + 9.00
    The result so far is 18.00

    latest code
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void scan_data(char *op1p, double *num1p);
    void do_next_op(char op1, double num1, double *accum);
    
    void main()
    {
        double num1;
        char op1;
        double accum;
        accum=0;
        printf("Hit enter to turn the calculator on");
        getchar();
    
    do{
    
       scan_data(&op1, &num1);
       do_next_op(op1, num1, &accum);
    
    }
    while (!(op1 == 'q'));
    
    }
    
    
    void scan_data(char *op1p, double *num1p)
    {
        printf("\nEnter an operator [^,*,/,+,-,q (quit)]: ");
        scanf(" %c",op1p);
        printf("Enter a number to calculate for: ");
        scanf("%lf",num1p);
        printf("%c %.2lf \n",*op1p,*num1p);
        printf("The result so far is %.2lf \n",*num1p);
    }
    
    void do_next_op(char op1, double num1, double *accum)
    {
            switch(op1)
            {
                case '+':
                {
                    *accum = *accum + num1;
                    break;
                }
                case '-':
                {
                    *accum = *accum - num1;
                    break;
                }
                case '*':
                {
                    *accum = *accum * num1;
                    break;
                }
                case '/':
                {
                    *accum = *accum / num1;
                    break;
                }
                case '^':
                {
                    *accum = pow(*accum, num1);
                    break;
                }
                case 'q':
                case 'Q':
                {
                    printf("The final result was %f",*accum);
                    break;
                }
        }
    }
    Last edited by spirit2244; 10-21-2012 at 12:34 PM.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    1
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    
    int main(void)
    {
       char character;
       float number=0.00, accum=0.00;
       
    	printf("Hit enter to turn the calculator on:" );
       getch();
    
    	printf("\n\nEnter an operator [^,*,/,+,-,q(quit): " );
       scanf("%c",&character);
    
      while (character!='q' && character !='Q')
       {
        printf("Enter a number to calculate for: ");
        scanf("%f",&number);
        printf("%c%.2f",character,number);
    
        	if(character=='^')
        		accum= pow(accum,number);
    
       	else if(character=='*')
        		accum= accum * number;
    
        	else if(character=='/')
        		accum= accum / number;
    
       	else if(character=='+')
        		accum= accum + number;
    
        	else if(character=='-')
        		accum= accum- number;
    
       	printf("\nThe result so far is %.2f\n",accum);
    
          fflush(stdin); // clears input stream
          fflush(stdout); // clears output stream
    
          printf("\n\nEnter an operator [^,*,/,+,-,q(quit): ");
          scanf("%c",&character);
        }
    
       printf("The result so far is %.2f",accum);
    
     getch();
     return 0;
    }
    Here is a code example. I will also take a look at your code and see if i can offer some advice

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > fflush(stdin); // clears input stream
    Mmm, quite:
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    Also, please don't dump complete alternative answers.
    We're here to help people learn how to program for themselves, not roll up and expect free homework on a plate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thank for your codes , and i dont get the flush thingy
    --------
    back to topic, the do_scan_op do nothing here

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > back to topic, the do_scan_op do nothing here
    Perhaps you could add inside the switch
    Code:
    default:
        printf("Don't know what %c does\n", op1 );
    > printf("The result so far is %.2lf \n",*num1p);
    Shouldn't you do this AFTER you've called do_next_op ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  5. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21