2014-09-10 40 views
0

在我的編程類,我們一直負責編寫一個程序來的詞或短語轉換成電話號碼,評估每個字符並將其轉換成相應的數字。C++循環 - 流程錯誤?

這裏是到目前爲止的代碼:

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    char letter; 
    int noOfLetters; 
    char response; 

    cout << "Enter Y/y to convert a telephone number " 
     << "form letters to digits.\n" 
     << "Enter any other letter to terminate the program: "; 

    cin >> response; 
    cout << endl; 

    while (response == 'Y' || response == 'y') 
    { 

     cout << "Enter a telephone number using letters: "; 
     cin >> letter; 

     noOfLetters = 0; 

     cout << "The corresponding telephone number is: "; 

     while (noOfLetters != 7) 
     { 
      //cout << "[" << noOfLetters << "]"; 
      noOfLetters++; 

      switch (toupper(letter)) 
      { 
      case 'A': 
      case 'B': 
      case 'C': 
       cout << 2; 
       break; 
      case 'D': 
      case 'E': 
      case 'F': 
       cout << 3; 
       break; 
      case 'G': 
      case 'H': 
      case 'I': 
       cout << 4; 
       break; 
      case 'J': 
      case 'K': 
      case 'L': 
       cout << 5; 
       break; 
      case 'M': 
      case 'N': 
      case 'O': 
       cout << 6; 
       break; 
      case 'P': 
      case 'Q': 
      case 'R': 
      case 'S': 
       cout << 7; 
       break; 
      case 'T': 
      case 'U': 
      case 'V': 
       cout << 8; 
       break; 
      case 'W': 
      case 'X': 
      case 'Y': 
      case 'Z': 
       cout << 9; 
       break; 
      default: 
       cout << "[invalid]"; 
      } 

      if (noOfLetters == 3) 
      { 
       cout << '-'; 
      } 
      else if (noOfLetters > 7) 
      { 
       cin.ignore(); 
      } 
      cin >> letter; 

      //noOfLetters++; 
     } 
     cout << endl; 
     cin.ignore(100, '\n'); 

     cout << "\nTo process another telephone number, enter Y/y \n" 
      << "Enter any other letter to terminate the program: "; 
     cin >> response; 
     } 
} 

一切工作正常,只要我鍵入超過700個字符。問題是,如果我輸入正好7個字符,它會自動化。

說我輸入「honk honk」(測量8個字符,不包括空格)並貫穿整個程序。輸出如下:

輸入Y/y將電話號碼格式的字母轉換爲數字。輸入 任何其他字母要終止程序:Y

輸入字母使用一個電話號碼:按喇叭按喇叭相應 電話是:466-5466

要處理其他電話號碼,輸入Y/Y輸入任何其他字母 終止程序:n按任意鍵繼續。 。 。

所以,工作正常。但是,如果我輸入「七十」(正好爲7個字符),程序迫使我進入另一個角色(不包括空格和換行),然後再繼續,就像這樣:

輸入Y/Y轉換電話號碼套用信函數字。輸入 任何其他字母以終止程序:Y

使用字母輸入電話號碼:70相應的 電話號碼是:738-3689ħ

要處理另一個電話號碼,輸入Y/y輸入任何其他字母 終止程序:n按任意鍵繼續。 。 。

我想要修改程序,以便能夠在程序中輸入7個字符的短語,並使其正確運行而不會出現任何錯誤,例如當我輸入任何測量8個字符或更多的字符時。

我已經嘗試了所有我能做的,沒有運氣。有人可以提出一個建議來幫助我解決這個問題嗎?

+0

刪除'while'外'cin >>字母,並在'while'裏面移動'cin >>字母'開始解決。問題在於你正在獲得'while'外的第一個字母,並且只能在裏面加上'noOfLetters'。這意味着'noOfLetters'總是比實際少1。事物的當前順序是:cin,同時檢查,增加int。你需要改變爲cin,增加,檢查。 – wendelbsilva 2014-09-10 18:20:14

回答

0

我申請下面的補丁(其目的是最小的):

*************** 
*** 83,91 **** 
       { 
        cout << '-'; 
       } 
!    else if (noOfLetters > 7) 
       { 
!     cin.ignore(); 
       } 
       cin >> letter; 

--- 83,91 ---- 
       { 
        cout << '-'; 
       } 
!    else if (noOfLetters == 7) 
       { 
!     break; 
       } 
       cin >> letter; 

什麼,你也許是沒有意識到的是,輸入緩衝:直到你按Enter鍵程序沒有看到任何東西。它掛在循環外的第一個cin >> letter,直到您按下輸入。循環中的後續cin >> letter從緩衝區讀取,因此始終包含換行符作爲最後一個字符。

你讀取緩衝區的方式,它會跳過空白(默認),所以你永遠不會看到換行符。

循環後,你有一個cin.ignore(100, '\n');忽略所有(剩餘)uptil和包括換行符(注意這個函數不會跳過空格:100包括空格字符)。所以,最簡單的事情就是在處理完7個字符後立即離開循環。

使用原始代碼時,當您輸入正好7個字符時,所有讀取和處理都是在noOfLetters == 7,而不是> 7,因此您嘗試再讀取一個字符,因爲剩餘的新字符-line被作爲空格跳過,除此之外沒有別的東西需要讀取:至少要輸入一個非空白字符(要處理),然後按Enter鍵才能從cin >> letter;返回。

+0

我試過你的建議,現在一切正常! – EnragedTanker 2014-09-10 21:33:21

1

你的邏輯流程是有點亂了。請注意,當noOfLetters爲7時,您仍然要求在while循環結束時再寫一個字母。請考慮閱讀循環的頂部。如果它更容易,緩衝循環中的輸出。

cout << "Enter a telephone number using letters: " << flush; 
// Flush may be necessary if output is line-buffered^

noOfLetters = 0; 

// This is a buffer where we will be storing the output phone number so that we 
// don't have to deal with mixing input with output. 
// 
// You will need to "#include <sstream>". 
stringstream phoneNumber; 

// Reading becomes part of the loop condition; if the end of the input is reached, 
// we want the loop to terminate. "cin >> letter" will evaluate to false in 
// boolean context if reading failed. 
// 
// Note that && short-circuits; if the left side is false then the right side is 
// not even evaluated. So when noOfLetters == 7, the loop terminates without 
// reading another character. 
while (noOfLetters != 7 && cin >> letter) { 
    ++noOfLetters; 

    // Your switch block goes here. Replace "cout" with "phoneNumber". 

    if (noOfLetters == 3) phoneNumber << '-'; 
} 

cout << "The corresponding telephone number is: " << phoneNumber.rdbuf() << endl;