C 编程错误|未申报的标识符

C++ Programming Error | Undeclared Identifier

本文关键字:标识符 编程 错误      更新时间:2023-10-16

所以我正在尝试制作一个简单的程序,并且我不断获得'未宣布的标识符错误',用我的名字,作者,价格,价格,ISBN,数量,数量,第一个结果,第二个结果,在控制台中。如果已经问过这种类型的话,我深表歉意,但我无法修复它。

这是我的程序:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name, author,
        double isbn,
        float price,
        int quantity,
        float firstresult,
        float secondresult,
        const float tax (0.07);
    cout << "What is the name of the book?";
    cin >> name;
    cout << "What is the authors name?";
    cin >> author;
    cout << "What is the ISBN number?";
    cin >> isbn;
    cout << "What is the price?";
    cin >> price;
    cout << "How many books did you purchase?";
    cin >> quantity;
    firstresult = price*tax;
    secondresult = price + firstresult;
    if (quantity > 5) {
        secondresult += (quantity - 5) * 2.00;
    }
    cout << "------------------------" << endl;
    cout << "Invoice of Order:" << endl;
    cout << name << endl;
    cout << author << endl;
    cout << isbn << endl;
    cout << price << endl;
    cout << quantity << endl;
    cout << "Total Cost: " << secondresult << endl;
    cout << "------------------------" << endl;
    return 0;
}

您试图通过用comma ,分离错误来声明不同类型的多个局部变量,这是错误的。使用单独的语句来声明您的变量,然后使用分号;。分号标志着陈述的结尾:

string name, author; // you are defining multiple variables of the same type which is fine
double isbn;
float price;
int quantity;
float firstresult;
float secondresult;
const float tax (0.07);

这些不是函数参数,在这种情况下,它们将被逗号分隔。也就是说,在接受标准输入的字符串时,您应该使用std :: getline:

std::getline(std::cin, author);