简体   繁体   English

未在此范围内声明错误

[英]was not declared in this scope errors

I want this program to compute gross salary, but it's outputting errors. 我希望该程序计算总工资,但是输出错误。

#include<stdio.h>

int main() {
    float base-salary = 1500.00;
    float bonus-rate = 200.00;
    float commission-rate = quantity * 2/100;
    int quantity;
    float Price;
    float Gross-salary;

    printf("Enter quantity of computers sold\n");
    scanf("%d",&quantity);
    printf("Enter Price of computers sold\n");
    scanf("%f",&Price);

    Gross-salary = base-salary + (quantity * bonus-rate) + (quantity * Price) * commission-rate;
    printf("Gross salary equals :%f",Gross-salary);


    //Gross salary = base salary + (quantity * bonus rate) + (quantity * Price) * commission rate


}

It spits out these errors. 它吐出这些错误。

5 13 Documents\\Untitled10.cpp [Error] expected initializer before '-' token 16 2 Documents\\Untitled10.cpp [Error] 'Gross' was not declared in this scope 16 2 Documents\\Untitled10.cpp [Error] 'base' was not declared in this scope 16 2 Documents\\Untitled10.cpp [Error] 'salary' was not declared in this scope 16 2 Documents\\Untitled10.cpp [Error] 'bonus' was not declared in this scope 16 2 Documents\\Untitled10.cpp [Error] 'rate' was not declared in this scope 16 2 Documents\\Untitled10.cpp [Error] 'commission' was not declared in this scope 5 13 Documents \\ Untitled10.cpp [错误]预期在'-'令牌之前的初始化程序16 2 Documents \\ Untitled10.cpp [错误]在此范围内未声明'Gross'16 2 Documents \\ Untitled10.cpp [错误]'基本'在此范围内未声明16 2 Documents \\ Untitled10.cpp [错误]在此范围内未声明'salary'16 2 Documents \\ Untitled10.cpp [错误]在此范围内未声明'bonus'16 2 Documents \\ Untitled10.cpp [错误]未在此范围内声明“ rate” 16 2 Documents \\ Untitled10.cpp [错误]未在此范围内声明“ commission”

You can't use - dash in C++ identifier names. 不能使用-破折号在C ++标识符名称。 Switch to underscore. 切换到下划线。

To quote http://en.cppreference.com/w/cpp/language/identifiers : 引用http://en.cppreference.com/w/cpp/language/identifiers

An identifier is an arbitrarily long sequence of digits, underscores, lowercase and uppercase Latin letters, and most Unicode characters (disallowed are control characters and characters in the basic source character set). 标识符是由数字,下划线,小写和大写拉丁字母以及大多数Unicode字符组成的任意长序列(不允许使用控制字符和基本源字符集中的字符)。 A valid identifier must begin with a non-digit character (Latin letter, underscore, or Unicode non-digit character). 有效标识符必须以非数字字符(拉丁字母,下划线或Unicode非数字字符)开头。 Identifiers are case-sensitive (lowercase and uppercase letters are distinct), and every character is significant. 标识符区分大小写(小写字母和大写字母是不同的),并且每个字符都很重要。

Besides, use the floating division, not integer, in your calculations. 此外,在计算中使用浮动除法,而不是整数。

float commission-rate = quantity * 2/100;

Since quantity is int , integer division will be performed, and you will only get the truncated part of the result. 由于数量为int ,将执行整数除法,并且您只会得到结果的截断部分。 Do so: 这样做:

float commission_rate = quantity * 2/100.f;

You have another error with using a variable before declaring it: 在声明变量之前,使用变量存在另一个错误:

float commission_rate = quantity * 2/100;
int quantity;

Quantity is undeclared so can't be used in line 1. Swap these statements. 数量未声明,因此不能在第1行中使用。交换这些语句。 Also, note that C++ is not a symbolic math language. 另外,请注意,C ++ 不是符号数学语言。 Variable values are taken at the moment when an expression is evaluated. 评估表达式时会获取变量值。 This is a common beginner mistake to first declare a formula, then initialize the variables it contains. 首先声明一个公式,然后初始化其包含的变量,这是一个常见的初学者错误。 Thus, line 1 must be moved below the input section. 因此,必须将第1行移到输入部分的下方。

Your fixed code might look like this: 您的固定代码可能如下所示:

const float base_salary = 1500.f;
const float bonus_rate = 200.f;
int quantity;
float price;
float gross_salary;

printf("Enter quantity of computers sold\n");
scanf("%d",&quantity);
printf("Enter Price of computers sold\n");
scanf("%f",&price);

const float commission_rate = quantity * 2/100.f;
const gross_salary = base_salary + quantity * bonus_rate + \
                     quantity * price * commission_rate;
printf("Gross salary equals :%f",gross_salary);

You can't use the minus sign (-) in variable name declarations. 您不能在变量名称声明中使用减号(-)。 Use either underscore notation 使用下划线符号

float base_salary = 100; or camelCase (which I prefer): float baseSalary = 100; 或camelCase(我更喜欢): float baseSalary = 100;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM