0

I know this is probably a really dumb error on my part but I just started learning C. I wanted to make a basic calculator and I keep getting errors about the program expecting a const char * but I have a float. Also it says the last printf command (line 50 i believe) doesn't have the right syntax or correct form. Again I am really new so sorry for the incovinience. Thank you for all the help! My code is below.

#include <stdio.h>

#include <math.h>

int main()
{
    char firstnum, secondnum, answer;
    char function;

    printf("Hello and welcome to my calculator!");

    printf("Please input the function you would like to use");

    scanf("%c", &function);

    printf("Now please input the two variables.");

    scanf("%f", &firstnum);

    scanf("%f", &secondnum);

    if (function == '+')
    {
        answer = firstnum+secondnum;
    }
    else if (function == '-')
    {
        answer = firstnum-secondnum;
    }
    else if (function == '*')
    {
        answer = firstnum*secondnum;
    }
    else if (function == '/')
    {
        answer = firstnum/secondnum;
    }
    else
    {
        printf("Sorry that was an incorrect function.  The correct inputs are +, -, *, /.");
    }

    printf(answer);

    return 0;


}
Matt Price
  • 79
  • 3
  • 1
    `answer` needs to be a `float` or `double`, rather than a `char`. You are storing values that have been divided in it and a `char` can only hold integer values. Same thing with `firstnum` and `secondnum`. As it stands, `chars` can only hold values -127 - 127. You should change them to `float`s or `double`s as well if you want to be able to do calculations on numbers bigger than 127. – shanet Feb 20 '13 at 05:31
  • 1
    @shanet, you're right about not using `char` but please don't propagate the myth about ranges, `char`s can be _much_ more than that, depending on the implementation. – paxdiablo Feb 20 '13 at 05:35
  • It's also best to post the exact errors you are getting, rather than making us run your code through a compiler. This question was fairly obvious to seasoned programmers, but other errors may not be so obvious. – shanet Feb 20 '13 at 05:36
  • @paxdiablo How so? AFAIK, the size of a char in C is guaranteed by the standard to be 1 byte. How can you store more numbers than -127 to 127 in a 1 byte signed char? – shanet Feb 20 '13 at 05:38
  • @shanet: `byte` is not `octet`. C defines a byte as the minimum addressible object (sans bitfields which are something else). This is not necessarily 8 bits, it may be more. – paxdiablo Feb 20 '13 at 05:42
  • @paxdiablo I learned something new today then. Still though, where would I encounter a system that was >8 bits to a byte? – shanet Feb 20 '13 at 05:52
  • 1
    @shanet, one that implemented Unicode as UTF-16/32 rather than UTF-8 would be one place. However, I've not seen one. You may want to look at http://stackoverflow.com/questions/5516044/system-where-1-byte-8-bit. – paxdiablo Feb 20 '13 at 05:58

5 Answers5

3

Couple of things:

1)Change line, as you want to use floating point numbers

char firstnum, secondnum, answer;

should be

float firstnum, secondnum, answer;

2)Change line

printf(answer);

to

printf("Answer %f \n", answer);
Rohan
  • 52,392
  • 12
  • 90
  • 87
0

Declare your variable like so -

float firstnum, secondnum, answer;

and change last printf to -

printf("Answer is: %f", answer);
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
0

Your final printf should be

printf("%f", answer);

or the nicer

printf("Answer : %f \n", answer);

This is because the declaration of printf is as follows

int printf ( const char * format, ... );

First parameter expected is the format of the output, the rest are the variables to be inserted.

In addition to this you should fix your datatypes as others have pointed out.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
0
char firstnum, secondnum, answer;
:
scanf("%f", &firstnum);

This is not going to end well at all. You have to match the datatypes in your scanf to the format specifiers.

Those variables should be of type float although, to be honest, there's no real reason not to use doubles nowadays for the extra range and precision. In that case, make sure you use %lf for the format specifiers.

In addition, your final printf is lacking a format specifier totally which means it will attempt to use the answer variable as a specifier. Once you've fixed the data types as per above, this should be changed to:

printf ("%f\n", answer);  // %lf for double.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Change your statement to printf("%f",answer);.

Also, you have declared your variables as chars , declare them as floats.

Recommended:

As you seem to be a beginner, I have an advice for you. Try reducing your program execution time and try to make the program efficient. So , instead of so many if---else blocks, you mmay have used switch case.

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • The performance gains of converting a switch statement here are going to be on the order of unnoticeable. If anything, he should convert it to a switch statement for readability. – drew212 Feb 20 '13 at 05:47
  • @drew212 Yeah I know..but, I believe in following good practises from the start always.. – Bhushan Firake Feb 20 '13 at 05:50