Thread: Menu options for a calculator

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    10

    Menu options for a calculator

    Hi guys i'm programming a calculator i have all the operations in functions but i don't know how to replace the values i entered in te menu option, so i want to do is enter the 2 values, that 2 values should be replaced in the menu and when i type for example the option of add the result should appear in the menu so i want to pretty much always clear the previuos menu
    Code:
    int main(){
        char seguir='s';
        int opcion=0;
        int num1;
        int num2;
        int rtaSum;//add
        int rtaRes;//subtraction
        float rtaDiv;//division
        int rtaMult;//multiplication
        int rtaFact;//factorial
    
    
        do
        {
            printf("1- Ingresar 1er operando (A=x"when i enter the value of the first number, x should be replaced to the value i entered in the second loop")\n");;
            printf("2- Ingresar 2do operando (B=y"same here")\n");
            printf("3- Calcular la suma (A+B)"same here for the two numbers and here i need to show the result of the add"\n");
            printf("4- Calcular la resta (A-B)\n");
            printf("5- Calcular la division (A/B)\n");
            printf("6- Calcular la multiplicacion (A*B)\n");
            printf("7- Calcular el factorial (A!)\n");
            printf("8- Calcular todas las operacione\n");
            printf("9- Salir\n");
    
    
            scanf("%d",&opcion);
    
    
            switch(opcion)
            {
                case 1:
                    printf("Ingrese el primer numero: \n");
                    scanf("%d" , &num1);
                    break;
                case 2:
                    printf("Ingrese el segundo numero: \n");
                    scanf("%d" , &num2);
                    break;
                case 3:
                    rtaSum=suma( num1 , num2);
                    printf("La suma es %d\n" , rtaSum);
                    break;
                case 4:
                    rtaRes=resta(num1 , num2);
                    printf("La resta es %d\n" , rtaRes);
                    break;
                case 5:
                    rtaDiv=division(num1 , num2);
                    printf("La division es %.2f\n" , rtaDiv);
                    break;
                case 6:
                    rtaMult=multiplicacion(num1, num2);
                    printf("La multiplicacion es %d\n" , rtaMult);
                    break;
                case 7:
                    rtaFact=factorial(num1);
                    printf("El factorial es %d\n"  , rtaFact);
                    break;
                case 8:
                    break;
                case 9:
                    seguir = 'n';
                    break;
    
    
            }
            }while(seguir=='s');
        return 0;
    
    
    }
    i hope you guys understand me sorry for my english

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,125
    So try with
    Code:
            printf("1- Ingresar 1er operando (A=%d\n",num1);
            printf("2- Ingresar 2do operando (B=%d\n",num2);
    As you choose options 1 and 2, the menu will be updated the next time you print it.

    Remember to initialise num1 and num2 to something useful.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    I expressed wrong i want to replace the (A) for the number when i enter

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Uh, why don't you write what the perfect output would be and we'll help you get there.

    Salem's suggestion should be able to do it but you say you expressed wrong.

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Would be
    Code:
    printf("Enter the first number(A)");
    And then the number i enter should appear in printf for example i enter 5
    Code:
    printf("Enter the first number (5)");
    Last edited by Xterik; 09-02-2017 at 06:39 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Well, Salem's idea would still work.

    I would just possibly make an array to make this look cleaner. You can have separate strings if you want but I recommend having one for each option.
    Code:
    char entries[9][20] = { "A", "B", "A+B", "A-B", "A/B", "A*B", "A!", "", ""};
    printf("Ingrese el primer numero (%s): \n", entries[0]);
    printf("Ingrese el secondo numero (%s): \n", entries[1]);
    You can see how I set up an array of strings, initialized to the first thing we want to see in the loop. When you have answers to display, call sprintf to change the string for the next display.
    Code:
    sprintf(entries[opcion - 1], "%d", num1); /*opcion = 1 */
    sprintf(entries[opcion - 1], "%d", num2); /* opcion = 2*/
    You can do this for any option anywhere you want to see it.

    Say the user clears the calculator or something like that, you can also copy back the initial strings into the entries to make everything look normal again.
    Last edited by whiteflags; 09-02-2017 at 08:49 PM. Reason: small fixes

  7. #7
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Thats really help, thank you!

  8. #8
    Registered User
    Join Date
    Aug 2017
    Posts
    10

    Post

    i have one more question when i have to replace 2 numbers and show the result i though this way was the correct but when i select the option 3 show me the correct result but shows me aswell the option 4

    Menu options for a calculator-d5928a05b9-png

    Code:
                case 3:                
                     rta=suma( num1 , num2);
                    sprintf(entries[opcion - 1], "%f+%f)=%f", num1, num2,rta); 
                    break;
    
                case 4:
                    rta=resta(num1 , num2);
                    sprintf(entries[opcion - 1], "%f-%f)=%f",num1, num2,rta);
    
    
                    break;

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,125
    You need to post your whole code, because this
    sprintf(entries[opcion - 1], "%f-%f)=%f",num1, num2,rta);
    won't print
    (.000000)

    Something else is going on.

    Bear in mind that "4.000000+3.000000)=7.000000" is a lot more than the 20 chars allowed in whiteflags's example.
    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.

  10. #10
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Code:
    int main(){
        char seguir='s';
        int opcion=0;
        float num1;
        float num2;
        float rta;
        char entries[5][20] = { "x", "y", "A+B", "A-B"};
    
        do
        {
    
    
            printf("1-Ingrese el primer numero (A=%s): \n", entries[0]);
            printf("2- Ingresar 2do operando (B=%s)\n", entries[1]);
            printf("3- Calcular la suma (%s)\n", entries[2]);
            printf("4- Calcular la resta (%s)\n", entries[3]);
            printf("5- Salir\n");
    
    
            scanf("%d",&opcion);
    
    
            switch(opcion)
            {
                case 1:
                    scanf("%f" , &num1);
                    sprintf(entries[opcion - 1], "%f", num1);
                    break;
                case 2:
                    scanf("%f" , &num2);
                    sprintf(entries[opcion - 1], "%f", num2);
                    break;
                case 3:
                    rta=suma( num1 , num2);
                    sprintf(entries[opcion - 1], "%f+%f)=%f", num1, num2,rta);
                    break;
                case 4:
                    rta=resta(num1 , num2);
                    sprintf(entries[opcion - 1], "%f-%f)=%f",num1, num2,rta);
                    break;
                case 5:
                    seguir = 'n';
                    break;
            }
            system("cls");
            }while(seguir=='s');
        return 0;
    
    
    }
    this is my code

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by Salem View Post
    Bear in mind that "(4.000000+3.000000)=7.000000" is a lot more than the 20 chars allowed in whiteflags's example.
    It's exactly 8 chars too long, and interestingly the last 8 chars (including the nul) are .0000000, which would be written into the next array element (assuming he's made the element 20 chars long).

    And he's totally changed his requirements. Suddenly they are all floating point instead of integers, and he's printing A, B, and the result.

    Although I generally dislike whiteflags overly-complicated solution, I don't feel like getting bogged down in this kind of "discussion" and so will refrain from offering a better one. A hint is to make a function to print the menu lines and use a special value to mean UNASSIGNED (e.g., FLT_MAX or DBL_MAX).
    Last edited by algorism; 09-03-2017 at 10:32 AM.
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  12. #12
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    Code:
    int main(){
        char seguir='s';
        int opcion=0;
        float num1;
        float num2;
        float rta;
        char entries[4][20] = { "x", "y", "A+B", "A-B"};
    
        do
        {
    
            printf("1-Ingrese el primer numero (A=%s): \n", entries[0]);
            printf("2- Ingresar 2do operando (B=%s)\n", entries[1]);
            printf("3- Calcular la suma (%s)\n", entries[2]);
            printf("4- Calcular la resta (%s)\n", entries[3]);
            printf("5- Salir\n");
    
            scanf("%d",&opcion);
    
    
            switch(opcion)
            {
                case 1:
                    scanf("%f" , &num1);
                    sprintf(entries[opcion - 1], "%f", num1);
                    break;
                case 2:
                    scanf("%f" , &num2);
                    sprintf(entries[opcion - 1], "%f", num2);
                    break;
                case 3:
                    rta=suma( num1 , num2);
                    sprintf(entries[opcion - 1], "%f+%f)=%f", num1, num2,rta);
                    break;
                case 4:
                    rta=resta(num1 , num2);
                    sprintf(entries[opcion - 1], "%f-%f)=%f",num1, num2,rta);
                    break;
                case 5:
                    seguir = 'n';
                    break;
            }
            system("cls");
            }while(seguir=='s');
        return 0;
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,125
    Try
    char entries[4][22]

    Then try
    char entries[4][24]

    Can you figure out where you need to get to to be safe?
    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.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by algorism
    .

    Although I generally dislike whiteflags overly-complicated solution, I don't feel like getting bogged down in this kind of "discussion" and so will refrain from offering a better one. A hint is to make a function to print the menu lines and use a special value to mean UNASSIGNED (e.g., FLT_MAX or DBL_MAX).
    I think using a numerical value as a sentinel in a calculator like this would cause more difficult problems than the imaginary one you are solving with it.

    It's really too bad you're too good to bother with us mere mortals.

  15. #15
    Registered User
    Join Date
    Aug 2017
    Posts
    10
    ok i could do it but in the case 8 i have to do all the operations at once i tried to copy all the cases into the case 8 but doesn't work any ideas?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming menu options
    By levitylek in forum C Programming
    Replies: 2
    Last Post: 11-17-2010, 06:04 PM
  2. Menu to select 1 of 2 options
    By gold2040 in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2006, 03:50 PM
  3. Menu Options Verification?
    By wbeasl in forum C++ Programming
    Replies: 6
    Last Post: 09-13-2004, 09:28 PM

Tags for this Thread