C++ while 循环未按预期运行

C++ while loop not functioning as intended

本文关键字:运行 while 循环 C++      更新时间:2023-10-16

我对编码很陌生,最近几天因为我的任务而感到沮丧,我应该创建一个类似超市的程序,我遇到的问题是我创建了一个 while 循环,应该在输入"f"时停止,提示它继续下一组代码, 问题是,它没有这样做。有人可以帮忙吗?谢谢。

#include <stdlib.h> //used to terminate the program
#include <string> //used for the string variable
#include <iomanip> //used for setprecision function
using namespace std;
string array;
string Data[3][10]= {{"Milk", "Bread", "Chocolatte","Towel","Toothpaste","Soap","Pen","Biscuits","Lampost","Battery"},
{"0120001", "0120002", "0120003", "0120004", "0120005", "0120006", "0120007", "0120008", "0120009", "0120010"},
{"10.50", "5.50", "8.00", "12.10", "6.75", "5.20", "2.00", "4.45", "20.50", "10.00"}}; //2D array to store and fetch the table information
float sumMoney;
int main ()
{
string currentPrice;
char customer = 'y';
while (customer == 'y')
{ //creates a while loop which resets the program if 'y' is inputted, and terminates otherwise
cout << "*****************************************************" << endl; //simple welcome greeting
cout << "*  WELCOME TO HERTS SUPERMARKET CHECKOUT SYSTEM     *" << endl;
cout << "* Scan the barcode or manually type the barcode ID  *" << endl;
cout << "*****************************************************" << endl;

bool found = false;
string ch = "n"; //char used to create the loop
cout << "Type a Barcode, or type 'f' to finish: " << endl;
cout << "n";
// loops untill input is "f"
while(ch!="f")
{
// takes input assigns it to ch variable which is being test for termination
cin >> ch;
// defines counter variable for loop
int i = 0;
//used to test if the barcode was found (changed to true when its found else its false)
//used as a break case for the loop
found = false;
//while the item isn't found and the counter isn't at the end of the array
/*
This loops through the array and compares it to the search term
has 2 break cases
1: found the search term in the array (found = true)
2: reach the end of the array (i reaches 9)
If found then price is assigned and outputted
If not found error is outputted
*/
while((found==false) && (i<9))  {
// if the given barcode is equal to the current
if(ch==Data[1][i])
{
// the price corresponding to the current i value is assigned
currentPrice = Data[2][i];
// break case for found is triggered (case 1)
found = true;
// outputs price not needed but was a debug line
cout << currentPrice << endl;
}
// if the counter reaches the end of the array without finding something
else if(i>=9){
cout << "Not found. Please input a Barcode: " << endl;
}
// increment i
i++;
}
}
float thechange();
{
float cashgiven,changegiven,sumprice;
sumprice = sumMoney;
cout << setprecision(2) << fixed << showpoint;
cout << "n";
cout <<"The total price is: " << char(156) << sumprice << endl;
while (sumprice > 0)
{
cout << endl;
cout << "Cash received: " << char(156);
cin >> cashgiven;
sumprice = sumprice - cashgiven;
if (sumprice > 0)
{
cout << "Insufficient Funds, cash needed: " << char(156) << sumprice;
}
}
if (sumprice <= 0)
{
changegiven = sumprice * -1;
cout  << "The Change Given: " << char(156) << changegiven;
}
}


cout << "Next Customer (Y/N): n";
cin >> customer;
}
return 0;
}

cin >> ch之后您没有测试ch。因此,即使输入是"f",代码也会继续进入while循环。您需要检查输入是否成功并测试输入是否为"f",然后才继续执行代码。

您将 ch 变量作为 while 循环的条件进行测试,但您在循环中读取它并对其进行操作,而无需另一个 if。

要解决此问题,您可以在 cin 操作后的 while 循环中添加一个 if,如果在if块中输入,您可以调用break 或继续