我不知道为什么我不能循环整个代码?当我按下"下一个客户"

时间:2015-09-30 14:30:02

标签: c++ loops

我想循环整个代码如果我按下Y"下一个客户"?并像第一次尝试一样重置程序。只需在项目代码上键入0101和0307,我只会输入一些代码。定义gotoxy只在on" next customer"我认为是错误?

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define g gotoxy
main()

{
  char back_, back;
  float change, p0101 = 25.5, p0307=11,subtotal=0,total=0;
  int y=9, quantity, itemcode, cash,discount;
  clrscr();
    do{
      do{
        g(1,8);
        cout<<"Itemcode";
        g(15,8);
        cout<<"Quantity";
        g(41,8);
        cout<<"Price";
        g(51,8);
        cout<<"Subtotal";
        g(62,8);
        cout<<"Total";

        g(1,y);
        cin>>itemcode;
        g(15,y);
        cin>>quantity;
        {
          g(25,y);
          if (itemcode == 0101){
            cout<<"Yogurt(12 oz)";
            subtotal = quantity * p0101;
          }
          else if (itemcode == 0307){
            cout<<"Pumpkin (1000g)";
            subtotal = quantity * p0307;
          }
        }
        {
          g(41,y);
          if(itemcode == 0101){
            cout<<"25.50";
          }
          else if (itemcode == 0307){
           cout<<"79.75";
          }
        }
        g(51,y);
        cout<<subtotal;
        total=total+subtotal;
        g(62,y);
        cout<<total;
        subtotal=0;
        g(72,8);
        cout<<"Add item?";
        g(72,y);
        cin>>back;
        y++;
        if(back=='n'||back=='N'){
          {
            g(1,y+2);
            cout<<"CASH: ";
            cin>>cash;
          }
          if(cash>total){
            {
              g(1,y+3);
              cout<<"[R]-REGULAR\t[D] - WITH DISCOUNT    :";
            }
            cin>>discount;
            g(1,y+4);
            if(discount=='d'||discount=='D'){
              (change=cash-(.8*total));
              cout<<"Change: "<<change;
            }
            else{
              change=cash-total;
              cout<<"Change: "<<change;
            }
          }
          else if(cash<total){
            cout<<"Insufficient amount";
          }
          {
            g(18,y+8);
            cout<<"EXCHANGE OF ITEM TO ANOTHER TYPE AND SIZE IS ALLOWED,";
          }
          {
            g(18,y+9);
            cout<<"SUBJECT TO STANDARD PROVISIONS AND PRODUCT WARRANTY";
          }
          {
            g(18,y+10);
            cout<<"          PLEASE PRESENT THIS RECEIPT";
          }
          {
            g(15,y+12);
            cout<<"Next Customer?";
            cin>>back;
          }
        }
      } while(back_=='Y'||back_=='y');
    } while(back=='Y'||back=='y');
  enter code here
  getchar();
  return 0;
}

2 个答案:

答案 0 :(得分:1)

我无法看到您输入back_的位置,但您有cin>>back;两次。这可能是你的错误吗?你的控制结构很难理解。

基本上,你有

do {
  do {
    // input some stuff for an item
    cout<<"Add item?";
    cin>>back;
    if(back == 'n' || back == 'N')
    {
      // some code to finalize the transaction
      cout<<"Next Customer?";
      cin>>back;
    }
  } while (back_=='y' || back_=='Y')
} while (back=='y' || back == 'Y')

您定义了back_,但从未对其进行初始化或在您的代码中使用它,因此当while (back_=='y' || back_=='Y')出现时,即使您按下&#39; y,#循环也不会循环。

如果您要将代码更改为:

do {
  do {
    // input some stuff for an item
    cout<<"Add item?";
    cin>>back;
    if(back == 'n' || back == 'N')
    {
      // some code to finalize the transaction
      cout<<"Next Customer?";
      cin>>back_;
    }
  } while (back_=='y' || back_=='Y')
} while (back=='y' || back == 'Y')

如果按下&#39; y&#39;程序肯定会再次循环。但是当你按下“&#39; n”时,你会得到一些有趣的行为。 "Add item?"和&#39; y&#39;为"Next Customer?"。我预计该计划将提前结束。最好有:

char new_customer, add_item
do {

  //---This is the outer loop---//

  do {

    //---This is the inner loop---//

    // input some stuff for an item
    cout<<"Add another item?";
    cin>>add_item;
    if(add_item == 'n' || add_item == 'N')
    {
      // some code to finalize the transaction
      cout<<"Next Customer?";
      cin>>new_customer;
    }

    //---End of the inner loop---//

  } while (add_item=='y' || add_item=='Y')

  //---End of the outer loop---//

} while (new_customer=='y' || new_customer=='Y')

这样,当您按下&#39; n&#39;在"Add Item?"处,当您按下&#39; n&#39;时外部循环中断在"Next Customer?",有效地结束了该计划。按下&#39; y&#39;在任何一个将继续他们各自的循环。

其他奇怪的行为可能来自

  1. 按下&#39; y&#39;以外的任何内容或者&#39; n&#39;决定点
  2. 将数字以外的任何内容输入到用于计算价格的变量中
  3. 不将您的输出格式化为$或具有两位小数位的价格字段
  4. 前两个可以通过以字符串形式读取所有内容然后逐个字符地解析来检查有效输入,然后使用内置C ++函数将相应的字符串转换为数字来修复。可以使用%(模数)运算符对数据进行一些花哨的计算来修复最后一个,以查看值是否以零结尾。只是一些想法。

    看起来它主要起作用,除了那个主要错误。

答案 1 :(得分:1)

你在程序中有一些错误。

  1. 取折扣类型int但用作char

  2. 如果您需要integer value,之后需要char,那么在这些之间需要使用getchar(),因为在获取integer时,按enter,所以需要阅读。

  3. 当您阅读char时,请检查您之前是否接受了输入(如果需要),然后按getchar()

  4. 读取换行符

    检查代码

    #include<iostream>
    #include<conio.h>
    #include<stdio.h>
    using namespace std;
    
    int main()
    {
        char newCustomer, addItem,discount;
        float change, p0101 = 25.5, p0307=11,subtotal=0,total=0;
        int y=9, quantity, itemcode, cash;
        do{
                subtotal=0,total=0;
            do{
                cout<<"Itemcode: ";
                cin>>itemcode;
                cout<<"Quantity: ";
                cin>>quantity;
                {
                if (itemcode == 101){cout<<"Yogurt(12 oz)";
                    subtotal = quantity * p0101;
                }else if (itemcode == 307){cout<<"Pumpkin (1000g)";
                    subtotal = quantity * p0307;
                }}
                {
                 cout<<"Price: ";
                if(itemcode == 101){
                    cout<<"25.50\n";
                }else if (itemcode == 307){
                    cout<<"79.75\n";
                }}
                cout<<"Subtotal: ";
                cout<<subtotal<<"\n";
                total=total+subtotal;
                cout<<"Total: ";
                cout<<total<<"\n";
                subtotal=0;
                cout<<"Add item?";
                cin>>addItem;
                getchar();
                if(addItem=='n'||addItem=='N'){
                    {
                    cout<<"CASH: ";
                    cin>>cash;}
                    getchar();
                    if(cash>total){
                        {
                        cout<<"[R]-REGULAR\t[D] - WITH DISCOUNT    :";}
                        cin>>discount;
                        char c = getchar();
                        if(discount=='d'||discount=='D'){(change=cash-(.8*total));
                        cout<<"Change: "<<change<<"\n";}
                        else{change=cash-total;cout<<"Change: "<<change;}
                    }
                    else if(cash<total)
                    {cout<<"Insufficient amount";}
                        {
                            cout<<"EXCHANGE OF ITEM TO ANOTHER TYPE AND SIZE IS ALLOWED,";
                        }
                        {
                            cout<<"SUBJECT TO STANDARD PROVISIONS AND PRODUCT WARRANTY";}
                        {
                            cout<<"          PLEASE PRESENT THIS RECEIPT\n";} {
                            cout<<"Next Customer?";cin>>newCustomer;char c =  getchar();}
                }
            }while(addItem=='Y'||addItem=='y');
        }while(newCustomer=='Y'||newCustomer=='y');
        return 0;
    }