一个"cin"的多个输入
Multiple Inputs for one "cin"
我的作业要求输出类似于ths:
示例输出:输入产品类型和销售数量(输入 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)nn";
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;
}
正如
@Pete贝克尔指出的那样,您正在尝试在用户输入数量之前计算利润。
将您的代码更改为以下内容:
char type;
int quant;
cout << "Enter product type and quantity sold (enter -1 to stop)nn";
cin >> type >> quant;
ProfitA = (1.99) * quant;
ProfitB = (2.99) * quant;
ProfitC = (3.99) * quant;
运行程序时,在每个输入后键入 enter。然后在运行程序之前,编辑您的代码以在输入后进行利润计算
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 在C++中使用Cin,我如何在1行中输入
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- cin 的十进制输入验证?
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 从 cin 读取不会返回整个输入字符串
- CIN 仅在输入非数字值时跳过下一个 CIN
- 当输入在带有空格的单行中给出时,cin 如何在 while 循环中工作?
- C++ 限制或使用户输入不可见 (std::cin)
- 为 c++ cin 上的输入数无效生成错误消息
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 为什么 std::cin 不提示用户输入?
- C++ CIN 值返回 0 而不是输入值
- 为什么从 std::cin 读取输入后右括号字符的值有时不是")"?
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- :CIN 不断打印用户在每次循环后输入的内容
- 设置输入(CIN)的超时
- 如何将输入(CIN)字符的用户转换为ASCII代码
- 不能在一行中输入 (cin) 超过 10 个整数,以空格分隔到数组中
- 函数结构数组输入cin问题