我对编码非常陌生,最近几天因为我的任务而感到沮丧,我应该创建一个类似超市的程序,我的问题是,我创建了一个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;
}
你没有测试ch后cin >> ch...因此,即使输入是“f”,代码也将继续输入while循环。您需要检查输入是否成功,并测试输入是否为“f”,然后再继续进行代码。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。