在"0"输入停止循环之前正确执行 while 循环?

Doing while loop properly until "0" input to stop the loop?

本文关键字:循环 执行 while 输入      更新时间:2023-10-16

我需要帮助。我目前正在学习C++编程,我仍然处于初学者水平。我仍在弄清楚如何使 while 循环工作。我的想法是,当插入正确的代码输入时,switch 语句选择正确的 case 语句并循环回以插入另一个输入,直到插入0以停止循环并在main((构造函数中计算最终输出。

我知道我很快就要解决一些问题,但我仍在努力弄清楚这个特定的部分。

#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
double sst = 0.06, total = 0, grandTotal, price, discount, newPrice, totalSST;
int quantity, count, code;
string name, ech;
void item001(){
name = "Rice (5kg)";
price = 11.5;
discount = 0;
}
void item002(){
name = "Rice (10kg)";
price = 25.9;
discount = 0;
}
void item003(){
name = "Sugar (1kg)";
price = 2.95;
discount = 0;
}
void item_cal(){
cout << "Please enter the quantity of the item: ";
cin >> quantity;
newPrice = (price + (discount * price)) * quantity;
cout << "nItem: " << name << "  ||  Quantity: " << quantity << "  ||  Price: RM" << newPrice << endl;
}
void input(){
cout << "Welcome SA Martn" << "Please insert the code. Press 0 to stop: ";
while (code != 0){
cin >> code;
switch (code){
case 001:
item001();
item_cal();
break;
case 002:
item002();
item_cal();
break;
case 003:
item003();
item_cal();
break;
default:
cout << "nWrong code" << endl;;
break;
total += newPrice;
}
}
}

int main(){
input();
totalSST = total * sst;
grandTotal = total + totalSST;
cout << fixed << setprecision(2);
cout << "Total: RM" << total << " ||SST: RM" << totalSST << " || Grand Total: RM" << grandTotal << endl;
return 0;
}

我在您的代码中看到的唯一功能问题是代码变量有可能初始化为 0(取决于编译器/随机性(。 如果发生这种情况,您的输入法将在进入循环之前返回。 除此之外,它看起来会起作用。当然,编程不仅仅是"让它工作"的艺术,风格和可读性也很重要。 通常,您希望将变量限制在引用它们的最小范围内。 "code"不应该是一个全局变量,它应该存在于输入法中。 至于循环,有几种方法可以实现:可以使用"while(true("循环,在这种情况下,可以在循环中定义变量;另一方面,"do while"将保证一个循环运行(也许这很适合这里(,但变量必须存在于循环之外,至少在条件检查的范围之内。 你选择的方式往往是一个风格问题。 下面,我使用"while(true("。

在编程中,可读性很重要(很多(。 我认为如果将数据分解为几个结构,也许是"比尔"和"食物",这个程序会更容易阅读。 要考虑的另一件事是如何在不引入显着复杂性的情况下扩大程序的使用范围。 例如,它可以适用于任何杂货店(任何一组食品/价格(。 这通常是确定一组适当的参数来为程序提供的问题。 要做这些事情,你可以写这样的东西:

#pragma once
#include <string>
#include <map>
using namespace std;
namespace market {
const double& sst = 0.06;
struct Bill {
double total = 0;
double totalSST = 0;
double grandTotal = 0;
};
struct Food {
const char* name;
double price;
double discount;
Food(const char* name, double price, double discount = 0)
: name(name), price(price), discount(discount) {}
double result_price() const {
return price - price * discount;
}
};
struct GroceryStore {
const char* name;
std::map<int, Food> inventory;
GroceryStore(const char* name, std::map<int, Food> inventory)
: name(name), inventory(inventory) { }
};
void shop(const GroceryStore& store, Bill& bill, bool show_menu = false, int exit_code = 0) {
// check error conditions
if (store.inventory.find(exit_code) != store.inventory.end()) {
// that's the 'exit_code' code silly!
cout << "Bad store.  Come back another time." << endl;
return;
}
cout << "Welcome to " << store.name << endl;
if (show_menu) {
cout << "The following items are available for purchase:" << endl;
for (auto p : store.inventory) {
cout << "t" << p.first << ") " << p.second.name << "(" << p.second.result_price() << endl;
}
}
cout << "Enter the product code of the item you wish to purchase:";
int code;
cin >> code;
while (true) {
auto food_it = store.inventory.find(code);
if (food_it == store.inventory.end()) {
cout << "Thanks for stopping by." << endl;;
break;
}
cout << "Please enter the quantity of the item: ";
uint32_t quantity;
cin >> quantity;
auto& food = food_it->second;
auto disc_price = food.price - (food.discount * food.price);
bill.total += disc_price * quantity;
cout << "nItem: " << food.name << "  ||  Quantity: " << quantity << "  ||  Price: RM" << disc_price << endl;
cout << "Would you like anything else?  Enter the product code, or press " << exit_code << " to proceed to check-out." << endl;
cin >> code;
}
}
void ring_up(Bill& bill) {
bill.totalSST = bill.total * sst;
bill.grandTotal = bill.total + bill.totalSST;
}
void run() {
int code = 1;
GroceryStore store("SMart", {
{ code++, Food("Rice (5kg)", 11.5, 0) },
{ code++, Food("Rice (10kg)", 25.9) },
{ code, Food("Sugar (1kg)", 2.95, 0) }
});
Bill bill;
shop(store, bill, true);
ring_up(bill);
cout << "Total: RM" << bill.total << " ||SST: RM" << bill.totalSST << " || Grand Total: RM" << bill.grandTotal << endl;
}
}

首先,当您输入 0 时输入中存在错误,那么当循环时它也不会中断,因为检查的代码包含以前的值。 例如: 输入为 3 0 但是根据您的代码,当代码第二次运行时,当条件被选中时,代码仍然包含 3 作为值,代码将再运行一次

尝试将代码初始化为某个值,例如 -1。我不太确定,但我认为对于全局 int 变量,他们将 int 变量初始化为 0。因此,您的第一个循环不会运行。或者另一种方法是使用 do while 循环而不是 while 循环。

do {
cin >> code;
switch (code){
case 001:
item001();
item_cal();
break;
case 002:
item002();
item_cal();
break;
case 003:
item003();
item_cal();
break;
default:
cout << "nWrong code" << endl;;
break;
total += newPrice;
} while (code != 0);
}

这可确保循环至少运行一次,使代码初始化。 希望对您有所帮助!祝您编程愉快!