Question

Im trying to write a calculator program. i have wrote the first part of it but i keep get the same error: invalid operands of types unsigned int*' andchar[80]' to binary `operator&' Please help me

 #include <stdio.h>
 #include <string.h>
 #include <math.h>
 #include <stdlib.h>
 unsigned int num1, num2, num3;
 char s[80];
 int main (){
  printf("type in an expression:   ");
  scanf(" %x %s %x\n", &num1 &s &num2);
  if(strcmp ("add", s) == 0){
    num3 = num1 + num2;
 }
 if(strcmp("subtract", s) == 0){
    num3 = num2 - num1;
 }
 printf("the answer is: %x", num3);
}   
Était-ce utile?

La solution

try:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
unsigned int num1, num2, num3;
char s[80];
int main (){
    printf("type in an expression:   ");
    scanf(" %x %s %x", &num1, s, &num2);
    if(strcmp ("add", s) == 0){
        num3 = num1 + num2;
    }
    if(strcmp("subtract", s) == 0){
        num3 = num2 - num1;
    }
    printf("the answer is: %x\n", num3);
    system("pause");
}

note: notice that I remove \n in the scanf..

Autres conseils

As Yohanes mentions, you need to have the commas in between the arguments in the scanf, otherwise the compiler is trying to do: get the address of num1 (&num1) and logically AND it with the address of the array s (address is implied here because it is an array) and logically AND that with the value contained in num2.

I would suggest that you add an else between the two if statements since they are mutually exclusive.

Furthermore, you probably want to add a \n to the printf statement

printf("the answer is: %x\n", num3);

to flush the output.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top