gpt4 book ai didi

c++ - 从 C 入门学习。预期的表达式错误 Xcode

转载 作者:行者123 更新时间:2023-11-28 03:06:55 25 4
gpt4 key购买 nike

我正在通过 C 入门学习 C++。我卡在这个问题的最后一部分 (1.6(p. 25))

Exercises Section 2.6.2 Exercise 2.41: Use your Sales_data class to rewrite the exercises in § 1.5.1 (p. 22), § 1.5.2 >(p. 24), and § 1.6 (p. 25). For now, you should define your Sales_data class in the same file >as your main function.

#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 一直告诉我预期的表达式..我不知道哪里出了问题请帮忙...

最佳答案

根据您对我的评论的回复,您在从电子书中复制一些代码时引入了一些奇怪的字符。当我用 gcc 复制和编译程序时,我收到如下错误 ( live example here ):

error: stray ‘\357’ in program

\357 是一个八进制转义序列。删除这些字符后,程序可以正常编译。

关于c++ - 从 C 入门学习。预期的表达式错误 Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com