0

我正在介绍编程,我有一位教授参与度不高。不幸的是,班里全是像我这样的新手,所以没有帮助。我必须只使用输入/输出、赋值语句和算术来创建税收计算器。我的朋友实际上帮助我修复了代码并在将其发送给我之前对其进行了编译。我没有改变任何东西,但它对我不起作用。

#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");

}

这是输出

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 ==========
4

1 回答 1

11

在 C 中,小于 C99 的变量必须在函数顶部声明(在其他非变量初始化语句之前),所以

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

然后你可以使用它们:

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

在 C++ 和 C99 中,您可以在代码中声明变量。

Visual C++ 不完全支持 C99(仅部分内容,例如阅读https://stackoverflow.com/a/146419/613130

Visual C++ 根据文件扩展名选择您使用的语言。您的朋友可能将程序命名为 likemyprogram.cpp并在他的计算机上编译为 C++,而您将文件命名为myprogram.c并在您的计算机上编译为 C。

对于警告...您可以忽略它们。几年前,微软开始对 C 基础库的某些部分(处理字符串和内存块的部分)进行讨伐,宣布它们“不安全”,因为它们很容易被滥用。它引入了具有更多参数的新函数。这些功能不是标准的和 Microsoft 特定的。请参阅http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx

你可以使用一个

#define _CRT_SECURE_NO_WARNINGS

在文件的开头(在 之前#include)删除警告。如果在 GCC 上编译,它不会有副作用。

于 2013-09-09T10:06:44.890 回答