我的作业要求输出类似于:
示例输出: 输入产品类型和销售数量(输入Z停止):A 2
产品A的总销售额= $ 5.97
字母代表对象的价格,后面的数字代表对象的数量。那么我如何得到我的cin以获得对象和数量? 我目前有:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double ProfitA, ProfitB, ProfitC;
double TotalA = 0;
char type;
int quant;
ProfitA = (1.99) * quant;
ProfitB = (2.99) * quant;
ProfitC = (3.99) * quant;
cout << "Enter product type and quantity sold (enter -1 to stop)\n\n";
cin >> type >> quant;
switch (type)
{
case 'A':cout << "Total sales of product A =" << ProfitA << "\n";
break;
case 'B':cout << "Total sales of product B =" << ProfitB << "\n";
break;
case 'C':cout << "Total sales of product C =" << ProfitC << "\n";
break;
}
答案 0 :(得分:0)
运行程序时,在每次输入后输入enter。 然后在运行程序之前,编辑代码以在输入之后进行利润计算
答案 1 :(得分:0)
正如@Pete Becker指出的那样,您试图在用户输入数量之前计算利润。
将您的代码更改为:
char type;
int quant;
cout << "Enter product type and quantity sold (enter -1 to stop)\n\n";
cin >> type >> quant;
ProfitA = (1.99) * quant;
ProfitB = (2.99) * quant;
ProfitC = (3.99) * quant;