1. ## 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 rtaRes;//subtraction
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:
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. 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.

3. I expressed wrong i want to replace the (A) for the number when i enter

4. 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. 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)");`

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

7. Thats really help, thank you!

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

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

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. Originally Posted by Salem
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).

12. 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. Try
char entries[4][22]

Then try
char entries[4][24]

Can you figure out where you need to get to to be safe?

14. 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. 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?