c++ primer 第五版第一章习题书店程序
2017-01-18 09:41
351 查看
#include <iostream>
#include "Sales_item.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
Sales_item item1,item2;int cnt=0;
if(std::cin>>item1)
{
cnt=1;
while(std::cin>>item2)
{
if(item1.isbn()==item2.isbn())
{
item1+=item2;
cnt++;
}
else{
std::cout<<item1<<" occours "<<cnt<<" times " <<std::endl;
item1=item2;
cnt=1;
}
}
std::cout<<item1<<" occpurs "<<cnt<<" times "<< std::endl;
}
/* int main(int argc, char** argv) {
Sales_item total;
if (std::cin >> total)
{
Sales_item trans;
int count = 1; //统计记录
while (std::cin >> trans){
if (total.isbn() == trans.isbn()){
total +=trans;
++count;
}
else{
std::cout << total << std::endl;
std::cout << count << std::endl;
total = trans;
count = 1;
}
}
std::cout << total << std::endl;
std::cout << count << std::endl;
} else {
std::cerr << "Wrong input!" << std::endl;
return -1;
} */
return 0;
}
上边的代码,第一部分是我自己写的,第二部分是是复制的别人的,即被注释掉的部分,两个代码都有相同的问题,即最后一个输入的数据是无法打印出来的,原因是无法跳出while,无法跳出while是因为,当while读到最后一个数字之后,即后面已经没有数字,while不会跳出,还在等待输入,如何才能让while在读完最后一个数字之后,能跳出while。这个是可以解决的,但是我更想明白的是为什么while在没有输入时,不会跳出,先写到这,等我解决的了,再修改这个博客。
#include "Sales_item.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
Sales_item item1,item2;int cnt=0;
if(std::cin>>item1)
{
cnt=1;
while(std::cin>>item2)
{
if(item1.isbn()==item2.isbn())
{
item1+=item2;
cnt++;
}
else{
std::cout<<item1<<" occours "<<cnt<<" times " <<std::endl;
item1=item2;
cnt=1;
}
}
std::cout<<item1<<" occpurs "<<cnt<<" times "<< std::endl;
}
/* int main(int argc, char** argv) {
Sales_item total;
if (std::cin >> total)
{
Sales_item trans;
int count = 1; //统计记录
while (std::cin >> trans){
if (total.isbn() == trans.isbn()){
total +=trans;
++count;
}
else{
std::cout << total << std::endl;
std::cout << count << std::endl;
total = trans;
count = 1;
}
}
std::cout << total << std::endl;
std::cout << count << std::endl;
} else {
std::cerr << "Wrong input!" << std::endl;
return -1;
} */
return 0;
}
上边的代码,第一部分是我自己写的,第二部分是是复制的别人的,即被注释掉的部分,两个代码都有相同的问题,即最后一个输入的数据是无法打印出来的,原因是无法跳出while,无法跳出while是因为,当while读到最后一个数字之后,即后面已经没有数字,while不会跳出,还在等待输入,如何才能让while在读完最后一个数字之后,能跳出while。这个是可以解决的,但是我更想明白的是为什么while在没有输入时,不会跳出,先写到这,等我解决的了,再修改这个博客。
相关文章推荐
- C++ Primer 第5版 第一章书店程序Sales_item问题
- 《C++ Primer》第五版课后习题解答_第一章(2)(12-25)
- c++ primer(第五版)学习笔记及习题答案代码版(第一章)
- 《C++ Primer》第五版课后习题解答_第一章(1)(01-11)
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1
- C++ primer 第五版习题答案, Stanley B. Lippman( 斯坦利 李普曼)(持续更新中)
- c++ primer 第五版习题答案9.28
- C++ primer第一章习题
- 小白初学C++ PRIMER 第五版 第一章
- C++ primer 第五版 第二部分 部分习题答案
- c++ primer 习题之 单词转换程序
- c++ primer第五版(中文)习题答案 第十章第二节第一小节-只读算法
- 《C++ primer》习题4.28程序
- 曼昆《经济学原理》(第五版)习题解答 第一章 导 言 经济学十大原理
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 2
- C++ Primer 第五版 文本查询程序
- 《C++ Primer(第四版)》的习题 4.8程序改正
- c++ primer第一章习题
- C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 3