我正在从 C 入门中学习 C++。我被困在这个问题的最后一部分(1.6(第 25 页))
练习第 2.6.2 节练习 2.41:使用您的 Sales_data 类重写第 1.5.1 节(第 22 页)、第 1.5.2 节 >(第 24 页)和第 1.6 节(第 25 页)中的练习。现在,您应该在同一个文件中定义您的 Sales_data 类 > 作为您的主函数。
#include <iostream>
#include <string>
struct Sales_data
{
std::string Book_Name;
unsigned Units_Sold = 0;
double Revenue = 0.0;
};
int main()
{
double price;
Sales_data total; // variable to hold data for the next transaction // read the first transaction and ensure that there are data to process
if (std::cin >> total.Book_Name >> total.Units_Sold >> price)
{
total.Revenue = total.Units_Sold * price;
Sales_data trans; // variable to hold the running sum // read and process the remaining transactions
while (std::cin >> trans.Book_Name >> trans.Units_Sold >> price)
{
trans.Revenue = trans.Units_Sold*price;
// if we're still processing the same book
if (total.Book_Name == trans.Book_Name)
{
total.Units_Sold += trans.Units_Sold; // update the running
total.Revenue += trans.Revenue; // update the running
}
else
{
std::cout << total.Book_Name << total.Units_Sold << total.Revenue;
**total.Book_Name = trans.Book_Name;**
total.Units_Sold = trans.Units_Sold;
**total.Revenue = trans.Revenue;**
}
**std::cout << total.Book_Name << total.Units_Sold << price << std::endl;** //print the last transaction
}
}
else
{
// no input! warn the user
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
return 0;
}
哪里有** Xcode一直告诉我预期的表达..我不知道出了什么问题请帮助...