0

I'm taking intro to programming and i have a professor who isn't very involved. Unfortunately the class is full of novices like myself, so no help there. I have to create a tax calculator only using input/output, assignment statements, and arithmetic. My friend actually helped me fix the code up a bit and compiled it before sending it to me. I didn't change anything, yet it won't work for me.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    const double TAX_RATE = 0.075;
    double item1 = 0, item2 = 0, item3 = 0;

    printf("Enter the price of the first item: \n");
    scanf("%lf", &item1);

    printf("You entered %.2lf \n", item1);
    printf("Enter the price of the second item: \n");
    scanf("%lf", &item2);

    printf("You entered %.2lf \n", item2);
    printf("Enter the price of the third item: \n");
    scanf("%lf", &item3);

    double total = item1 + item2 + item3;
    double tax = total * TAX_RATE;
    double totalWithTax = total + tax;

    printf("You entered %.2lf \n", item3);
    printf("The total of your items is %.2lf \n", total);
    printf("The tax is %.2lf \n", tax);
    printf("The total with tax is %.2lf \n", totalWithTax);

    system("pause");

}

Here's the output

1>------ Build started: Project: assign2, Configuration: Debug Win32 ------
1>  program.c
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(10): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(14): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(18): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(20): error C2143: syntax error : missing ';' before 'type'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(21): error C2143: syntax error : missing ';' before 'type'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(22): error C2143: syntax error : missing ';' before 'type'
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(25): error C2065: 'total' : undeclared identifier
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(26): error C2065: 'tax' : undeclared identifier
1>c:\users\chris\documents\visual studio 2010\projects\assign2\assign2\program.c(27): error C2065: 'totalWithTax' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
xdry
  • 57
  • 1
  • 1
  • 2
  • 2
    How about fixing those errors, which are listed in output? Seems like your code is also incomplete. – Rito Sep 09 '13 at 10:03
  • 1
    Did you check out the online helps mentioned in the error messages? Also, the error messages seem to be self-explanatory. – Powerslave Sep 09 '13 at 10:03
  • 3
    Declaration need to be before any statements. – leppie Sep 09 '13 at 10:04
  • This code when compiled with gcc compiles just fine. – Manoj Awasthi Sep 09 '13 at 10:06
  • The warnings against `scanf` are bull****. `scanf` is as unsafe as `int *`. If they think that's unsafe, they shouldn't write in C. That said, microsoft doesn't like C. If you want to learn C, do yourself a favor and use gcc. While you're at it, also switch to Linux. – Shahbaz Sep 09 '13 at 10:16
  • Note that depending on the compiler, there could be problems with the `%.2lf` used in `printf` (and only in `printf`, not in `scanf`). You should use `%.2f`. In C the use of `%lf` in `printf` was added only in C99. – xanatos Sep 09 '13 at 10:27
  • 5
    This question appears to be off-topic because it is asking help in getting code to compile. It seems that solutions were readily available in the documentation and the questioner hasn't done very much before posting here. Question is unlikely to be of use to others. – marko Sep 09 '13 at 10:36

1 Answers1

11

In C lesser than C99 variables must be declared at the top of the function (before other non-variable-initializing statements), so

const double TAX_RATE = 0.075;
double item1 = 0, item2 = 0, item3 = 0;
double total, tax, totalWithTax;

and then you can use them:

total = item1 + item2 + item3;
tax = total * TAX_RATE;
totalWithTax = total + tax;

In C++ and in C99 you can declare variables inside the code.

Visual C++ doesn't fully support C99 (only some pieces, read for example https://stackoverflow.com/a/146419/613130)

Visual C++ choose which language you are using based on file extension. Your friend probably named the program like myprogram.cpp and it compiled on his computer as C++, while you named the file myprogram.c and it compiled on your computer as C.

For the warnings... You can ignore them. Microsoft some years ago began a crusade against parts of the base library of C (the ones that handle strings and blocks of memory), declaring them "unsafe" because it's very easy to misuse them. It introduced new functions that have some more parameters. These functions are not-standard and Microsoft specific. See http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx .

You can use a

#define _CRT_SECURE_NO_WARNINGS

at the beginning of the file (before the #include) to remove the warnings. It won't have side-effects if you compile on GCC.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280