2014-05-08 145 views
-1

我讓程序順利運行,然後在對它進行註釋並添加了一些最終接觸之後,它停止了對我的工作。我有問題的功能是使用其他地方定義的幾個對象/函數,所以我只是想知道是否有人可以肯定我的邏輯是正確的,並且無限循環不是語法錯誤的產物。感謝您的時間,這裏是我遇到的問題:陷入無限循環

如果收銀員開始新訂單並想要結束訂單,則輸入T。但是,嘗試退出訂單並循環回到while(moreCustomers)開始,沒有任何事情發生。我試圖通過設置moreItems = false;來退出while(moreItems)循環,但是在完成之後,它會卡在while(moreItems)循環中,並且不會返回到while(moreCustomers)。語法是否有意義,我應該通過設置moreItems = false;來打破循環?

bool moreCustomers = true;  
while (moreCustomers) 
{ 
    // get input to start new order or close register 
    drawInstruct("Enter N to start a new order or E to\n close the register."); 
    char* setFmt = "@"; // the input must be a letter 
    char input[7];  // char array that stores input from cashier 
    s.GetStr(xLeftCoord + 1, yTopCoord + 1, input, 1, setFmt, true); 

    for(int x = 1; x < 10; x++) // clear the input field 
    { 
     s.ClearScreenPos(x, 1); 
    }   

    if (input[0] == 'N') // if a new order is requested 
    { 
     bool moreItems = true; 

     while (moreItems) 
     {     
      getInput(input);   

      if(input[1]) // if input is not a single char 
      { 
       if (input[0] == 'M') // get the desired number of multiples for the current item and update the tape and display area accordingly 
       {      
        custTape.handleMultiples(atoi(input)); // adds multiples to tape 
        curVal = isUPC->price * (atoi(input)); // updates the current item price        
        drawDisplayArea(curVal);    // updates the display area 
       } 
       else // invalid number of multiples, prompt for new multiple 
       { 
        drawInstruct("Invalid command. Please try again."); 
        s.Delay(); 
       }      
      } 
      else if (input[0] == 'T') // close the order 
      { 

       drawInstruct("Order cancelled."); 
       s.Delay();               
       moreItems = false; // customer order is complete, exit loop      
      } 
      else // invalid command, get new input from the cashier 
      { 
       drawInstruct("Invalid command. Please try again."); 
       s.Delay(); 
      } 
     } 
    } 
    else if (input[0] == 'E') // close the register 
    {    
     moreCustomers = false; // no more customers, exit the program 
    } 
    else // invalid command, get new input from the cashier 
    { 
     drawInstruct("Invalid Command. Please try again."); 
     s.Delay(); 
    } 
} 

我不能退出else if(input[0] == 'T'),任何命令我以後正確moreItems = false;工作介入;

+9

這種代碼是一個很好的例子,爲什麼分解出是一個偉大的工具。我不願意在此代碼中查找問題。 – harper

+0

你是什麼意思分解?我可以理解,當沒有解釋正在使用的代碼時,是否很難提供幫助,但是需要了解大量代碼。我只是想弄清楚我使用的moreItems布爾值是否應該在技術上退出循環,並不認爲額外的代碼會對此有所幫助 – cjw

+1

首先逐步完成程序,並觀察每次通過循環,但尤其要注意看*什麼*不會*改變(導致離開循環的事物)。 – crashmstr

回答

1

我會在第一個moreItems設置一個斷點= false;看看它是否被擊中。我的猜測是,事實並非如此。您已經標記了與Visual Studio的問題,所以如果這是你用什麼看這個鏈接瞭解如何設置斷點:

http://msdn.microsoft.com/en-us/library/vstudio/k80ex6de%28v=vs.100%29.aspx

基本上是一個斷點使你的程序在該行停止。也可以嘗試在這一行上設置一個斷點:

if (input[0] == 'N') 

運行程序,按下一個鍵,並等待斷點被命中。然後使用Debug菜單上的「Step Over」選項。這可以逐行運行你的程序,每次你按下「Step Over」(F10也會這樣做,速度更快)。繼續查看通過代碼執行的執行路徑。您也可以將鼠標懸停在變量上以查看其值。關於使用Visual Studio調試淨

即使世界荷載,但如果你掌握了上面你會得好遠

+0

按照之前給出的建議,我設置了一個斷點,並能夠最終看到我的變量中的值。 'moreItems = false'被擊中,所有我的值在被調用後都是正確的,但是當我跳出'if(input [0] =='N')'語句時,我的input []字段是錯誤的。所以我認爲我的語法是正確的,而且問題在於別處。我會嘗試看起來非常有用的「Step Over」選項。 – cjw

+0

「我的輸入[]字段錯誤」是什麼意思?請注意,如果將鼠標懸停在數組的名稱上,則只會看到第一個元素的地址,並可能試圖顯示整個字符串。將鼠標懸停在sqaure括號內以獲取該特定元素的值 – rcs

+0

我看到輸入[]的每個元素中都有哪些字符,並且這不是我所期望的 – cjw