开发者社区> 问答> 正文

C++而循环不按预期工作

我对编码非常陌生,最近几天因为我的任务而感到沮丧,我应该创建一个类似超市的程序,我的问题是,我创建了一个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;
}

展开
收起
aqal5zs3gkqgc 2019-12-19 20:19:34 2991 0
1 条回答
写回答
取消 提交回答
  • 你没有测试ch后cin >> ch...因此,即使输入是“f”,代码也将继续输入while循环。您需要检查输入是否成功,并测试输入是否为“f”,然后再继续进行代码。

    2019-12-19 20:20:05
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载