C++ while 循环未按预期运行
C++ while loop not functioning as intended
我对编码很陌生,最近几天因为我的任务而感到沮丧,我应该创建一个类似超市的程序,我遇到的问题是我创建了一个 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 或继续
相关文章:
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 重新启动后,线程无法在 while 循环中再次运行
- 为什么我的 Do-while 循环无法在C++中运行菜单
- 如果语句不会在 do - while 循环中运行
- C++ while 循环未按预期运行
- 第二个 while 循环未运行,将值设置为 "nan"
- 在运行 while 循环中从 QLineSeries 更新 QChart
- Qt qml - 在没有任何条件的情况下运行一行(while(true))
- 跳过整个循环而不运行 while
- 如何运行 while 循环直到到达新行?
- 查找涉及 while 循环的运行时函数
- while 循环是否在设置标志的情况下运行多次?
- 当循环应该只运行一次时,执行两次while循环(C++)
- 使用 delta 变量将 while 循环限制为 30 "FPS"运行C++
- while 循环运行,无论输入是否正确
- C++ - 使用 cin 循环运行 while 后,程序不会再次接受输入
- 不停止While循环运行而未获得预期结果
- 当带有while循环的进程正在运行时,缺少对话框内容
- 在while循环中运行Qt程序
- 运行while的第一个实例