因此,我尝试编写一个简单的程序,并在控制台中不断收到'未声明的标识符错误',其中包含我的姓名,作者,价格,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;
}
最佳答案
您试图通过用逗号,
分隔它们来声明多个不同类型的局部变量,这是错误的。使用单独的语句声明变量,并改用分号;
。分号标志着语句的结束:
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);
关于c++ - C++编程错误|未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46107791/