关于C ++ Noob的简单问题 [英] Simple Question on C++ Noob

查看:105
本文介绍了关于C ++ Noob的简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的家伙所以我在c ++入门书中练习书店计划。这是一本书,但我不能做这个练习:/





写一个程序,读取同一个交易的几个交易/>
ISBN。写下读取的所有事务的总和。 (这是我必须要做的)



我制作了一个代码,它将两个一起添加但是如何修改它以允许无限制的输入。



Okay guys So I have an exercise to do for a book store program in c++ primer. It is a book but I can't do this exercise :/


Write a program that reads several transactions for the same
ISBN. Write the sum of all the transactions that were read. (This is what I have to do)

I made a code that will add two together but How can I modify it to allow unlimited inputs.

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2; // read a pair of transactions
std::cout << item1 + item2 << std::endl; // print their sum
return 0;
}





我想读取多个输入并允许我结束语句打印结果。这是我知道的一个菜鸟问题。任何帮助将不胜感激:)



I would like to to read multiple inputs and allow me to end statement to print result. It's a noob question I know. Any help would be appreciated :)

推荐答案

循环! ...创建一个循环,读取输入数据并将其存储到数组中。如果您不想自己进行数组大小管理,请使用某种容器,例如std :: vector(容器只是内部包含实际数据数组的帮助程序。)
Loop! ...create a loop that reads input data and store it off to an array. If you don't want to do the array size management yourself, use a container of some sort such as a std::vector (containers are just helpers that "contain" the actual data array internally).


您可以使用不确定大小的链接列表或考虑http://www.cplusplus.com/reference/vector/vector/vector/然后您需要做的就是循环直到输入结束命令,添加值到你的向量,在输入结束命令之后,获得向量的迭代器创建一个简单的变量来存储和,在迭代循环时对向量中的所有内容求和。





链接列表的概念是相同的,但是一个允许你使用stl(vector)使用另一个方法链表不必。
You could use a linked list, of indeterminate size or consider http://www.cplusplus.com/reference/vector/vector/vector/ Then all you need to do is loop until end command entered, add values to your vector, after your end command entered, get an iterator to the vector create a simple variable to store the sum, sum everything in your vector while you iterate over the loop.


The concept is the same for a linked list, however one allows you to use the stl (vector) the other approach using a linked list doesn't have to.


这篇关于关于C ++ Noob的简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆