问题描述
我一直在努力从选择结构中获取局部变量。我通常不熟悉编程,因此在计算其值在if
函数内的项目的总购买额方面需要帮助,并且我在while
函数中遇到麻烦,因为我打算使用它来无限地接受进餐代码和数量,直到用户输入0
,但这是行不通的。如何使我的无限while
函数起作用,如何从选择结构中正确获取值?任何帮助,提示和建议都将受到赞赏!
更新:我能够通过函数内部的变量解决问题。当前的问题是我需要两次输入0
,因为第一个0
被忽略了,我也不知道为什么。
int main()
{
char mealCode;
bool noOrder = false;
int quantity = -1,subTotalA = 0,subTotalB = 0,subTotalC = 0,subTotalD = 0,subTotalE = 0,total = 0;
while (quantity != 0)
{
cin >> mealCode >> quantity;
if (quantity == 0)
break;
else
{
if (mealCode == 'A')
{
subTotalA += 45 * quantity;
cout << "Subtotal is " << subTotalA << endl;
}
else if (mealCode == 'B')
{
subTotalB += 50 * quantity;
cout << "Subtotal is " << subTotalB << endl;
}
else if (mealCode == 'C')
{
subTotalC += 55 * quantity;
cout << "Subtotal is " << subTotalC << endl;
}
else if (mealCode == 'D')
{
subTotalD += 60 * quantity;
cout << "Subtotal is " << subTotalD << endl;
}
else if (mealCode == 'E')
{
subTotalE += 75 * quantity;
cout << "Subtotal is " << subTotalE << endl;
}
}
total = subTotalA + subTotalB + subTotalC + subTotalD + subTotalE;
cout << "Total Purchase is" << total;
}
}
解决方法
如果您希望data _null_;
monyyyy = 'January 2013';
date = input(substr(strip(monyyyy),1,12),YYMMN.);;
put date=YYMMN.;
run;
循环在数量等于0之前是无限的,则需要这样写。您不需要两个相同的while
。
另外,如果要用这么多的常量进行比较,最好使用cin
控制结构。
例如,您可能想要执行以下操作:
switch
,
不确定要做什么,但有几点评论
- 如果要跟踪小计和总计,则需要将变量初始化为0,然后将每个产品的新小计求和
- 如果要使用多种产品,则需要将操作包括在while循环中(当用户不断添加数量的餐食!= 0时,您将继续计算小计)
类似
char mealCode;
int subTotalA=0,subTotalB=0,total,quantity;
while (cin >> mealCode >> quantity)
{
if (quantity <= 0) { break; }
if (mealCode == 'A')
{
subTotalA += 45 * quantity;
cout << "Subtotal is " << subTotalA << endl;
}
if (mealCode == 'B')
{
subTotalB += 50 * quantity;
cout << "Subtotal is " << subTotalB << endl;
}
total = subTotalA + subTotalB ;
cout << "Total Purchase is Php" << total;
}
,
我不喜欢if
和switch
解决方案,所以让我们来尝试一下并消除重复。
#include <iostream> //cin. cout
#include <cstring> //strchr
#include <numeric> //std::accumulate
#include <iterator> //std::begin and std::end
using namespace std;
int main()
{
char mealCode;
int quantity;
// use tables to replace the different code for different meal codes
// list valid meal codes
const char mealCodes[] = "ABCDE";
// list cost of each meal code.
const int mealCosts[] = {45,50,55,60,75};
// Sucks when you ass an item to one table and not the other
// so lets give ourselves a reminder,hmmm?
// Refuse to compile if the costs and codes don't line up
// note the -1 to ignore the null terminator in the string
static_assert(std::size(mealCodes) - 1 == std::size(mealCosts),"Meal codes and meal costs aren't the same size");
// when you have a bunch of sequentially numbered variables that's nature
// telling you you want an array. This one is forced to be the same size
// as the number of meal codes. They're all 0,so no magic is required.
int subTotal[std::size(mealCodes)] = {0};
while (cin >> mealCode >> quantity && quantity != 0) // loop until bad input
// or quantity 0
{
// verify meal code
const char * found = strchr(mealCodes,mealCode);
if (found) // found a valid meal code
{
//get index of mealCode in mealCodes
size_t index = found - mealCodes;
// Look up the cost in the table and accumulate
subTotal[index] += mealCosts[index] * quantity;
// not 100% sure about the += but not summing multiple
// runs of the same meal code run seems wrong
cout << "Subtotal is " << subTotal[index] << endl;
}
}
// sum up all of the subtotals
int total = std::accumulate(std::begin(subTotal),std::end(subTotal),0);
cout << "Total Purchase is Php" << total;
}
优点:添加新的进餐代码是代码中的两个变化:将代码添加到mealCodes
并将成本添加到mealCosts
。
没有多余的注释,该程序就很短,而且随着进餐次数的增加,它并不会变得太大。
#include <iostream>
#include <cstring>
#include <numeric>
#include <iterator>
using namespace std;
int main()
{
char mealCode;
int quantity;
const char mealCodes[] = "ABCDEFGHIJK";
const int mealCosts[] = {45,75,13,62,88,42,10,99 };
static_assert(std::size(mealCodes) - 1 == std::size(mealCosts),"Meal codes and meal costs aren't the same size");
int subTotal[std::size(mealCodes)] = {0};
while (cin >> mealCode >> quantity && quantity != 0)
{
const char * found = strchr(mealCodes,mealCode);
if (found)
{
size_t index = found - mealCodes;
subTotal[index] += mealCosts[index] * quantity;
cout << "Subtotal is " << subTotal[index] << endl;
}
}
int total = std::accumulate(std::begin(subTotal),0);
cout << "Total Purchase is Php" << total;
}