0
我需要你的帮助家伙我在1个练习堆栈女巫在第2章结尾!我的这个练习的问题是,我无法弄清楚如何让逻辑循环问几次输入!我写了代码巫婆问两次输入!以前用书提供的标题我做这个任务容易,但这种方式不再工作了。所以生病给你的锻炼和代码,并希望你能帮助我。很遗憾我ENGLSIHC++ Primer练习2.41
练习
写程序女巫将有一类在同一个地方,你main
功能。
编写代码女巫将读取具有相同书号的多个交易,并使用该书号统计每笔交易。
我的代码
#include <iostream>
#include <string>
using namespace std;
//Data structure Code
struct Sales_Data
{
std::string bookNo;
unsigned unit_sold;
double revenue;
};
int main()
{
Sales_Data data1,data2; //Data wich will hold input
double price; //Price per book used to calculate total revenue
// Checking if there was data input of book number units sold and price
if (std::cin>>data1.bookNo>>data1.unit_sold>>price)
{
int cnt=1; //Start Counter
data1.revenue=data1.unit_sold*price;// data1 calculating total revenue from price and unit_sold
while (std::cin>>data2.bookNo>>data2.unit_sold>>price)
{
data2.revenue=data2.revenue*price;
//checking if book name is same
if (data1.bookNo == data2.bookNo)
{
++cnt; //Incrementing counter if they same
unsigned totalCnt=data1.unit_sold+data2.unit_sold;
double totalRevenue=data1.revenue+data2.revenue;
//Print out result
std::cout<<cnt<<data1.bookNo<<" "<<totalCnt<<" "<<totalRevenue<<" ";
getchar();
getchar();
getchar();
if (totalCnt != 0)
std::cout<<totalCnt/totalRevenue;
else
std::cout<<"(No Sales)"<<std::endl;
return 0;
}else{
std::cerr<<"Book numbers isn't same"<<std::endl;
return -1;
}
}
}
return 0;
}
而且素来也是知道为什么,但在收入给我的垃圾数量。 谢谢你的时间。
yes在'struct' – AlexGreat 2013-04-24 02:14:13
不,它是声明,不是定义...你可以在这里尝试'std :: cout << data2.revenue << std :: endl',看看输出是什么。它应该是垃圾。 – gongzhitaao 2013-04-24 02:14:46
对不起!不,它告诉我,数据成员不允许初始化 – AlexGreat 2013-04-24 02:15:40