我希望有人能帮助我。以下是我目前的代码。请温和一点,这是我第一次使用C++编写的程序,距离我最后一次碰触C已经有一年多了。是的,这是为了作业〜我已经使用这个页面足以知道它被问了很多;)带用户输入的C++文本数组,然后打印输入
我有一个问题,并努力寻找一些适度的帮助,是如何创建一个数组来存储用户输入文本?
从代码流程中可以看出:我询问用户想要购买多少物品......然后决定循环,询问用户购买的物品名称,每件物品的成本以及总数(量。数学部分我很好〜我有购买的总项目和运行小计相当准确地打印出来。但是,我想要做的是按顺序打印购买的商品的名称。
目前代码输出:
How many items do you want to enter? 3
What is the item name? Honey
What is the unit price for Honey? 5.99
How many purchased? 3
What is the item name? Milk
What is the unit price for Milk? 2.79
How many purchased? 2
What is the item name? chocolate
What is the unit price for chocolate? 1.97
How many purchased? 5
Bill Date:
Items Purchased: 10
Subtotal: 33.4
在“比尔日期”和“购买的商品”之间我想列出,一行行的(3)购买的物品:蜂蜜,牛奶和巧克力。这是项目名称的存储和增加,我非常坚持。如果有人能指引我正确的方向,我将不胜感激。而且,请更详细地解释如何以及为什么,对我来说更好。文本/字符数组和我仅仅是熟人,而数字int数组和我正在喝酒伙伴。
谢谢! :d
所需的代码输出:
Bill Date:
Honey
Milk
chocolate
Items Purchased: 10
Subtotal: 33.4
我的代码:?
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <time.h>
using namespace std;
int main()
{
int itemCount = 0, i, itemQty;
int numOfItems = 0;
char itemName[25];
double itemCost;
double itemSub;
double subtotal = 0;
cout << "How many items do you want to enter? ";
cin >> itemCount;
for(i = 0; i < itemCount; i++)
{
cout << "What is the item name? ";
cin >> itemName;
cout << "What is the unit price for " << itemName << "? ";
cin >> itemCost;
cout << "How many purchased? ";
cin >> itemQty;
numOfItems = numOfItems + itemQty;
itemSub = itemQty * itemCost;
subtotal = subtotal + itemSub;
}
cout << "\n\tItems Purchased: " << numOfItems;
cout << "\n\tSubtotal: " << subtotal << "\n";
}
我会建议使用成员'itemCost'' itemName'和'itemQty'结构。使'itemName'成为'std :: string'。将项目数组更改为上述结构的“std :: vector”,并在打印其元素之前使用std :: sort对向量进行排序 –