2016-03-01 60 views
-2

我正在製作一個程序,詢問用戶他們想要購買什麼商品以及每件商品的數量,然後計算所有商品的總金額。我想知道的是,我會如何確保爲「金額」輸入的輸入只是一個正數。我已經在這裏搜查,只發現了同類型C以外的問題語言++在C++中測試正數的輸入

#include<iostream> 
#include<string> 
#include<iomanip> 

using namespace std; 

int main() 
{ 
//Constants for menu 
const int 
    NOTEBOOK = 1,   //Represents choice for notebook 
    PEN = 2,    //Represents choice for pen 
    PENCIL = 3,   //Represents choice for pencil 
    EXIT = 4;    //Exits the program 

//Constants for Prices 
const double 
    NOTEPRICE = 1.50,  //Represents notebook price 
    PENPRICE = .30,  //Represents pen price 
    SPECPEN = .25,  //Represents pen price when coupon code is entered 
    PENCILONE = .20,  //Represents pencil price when 1-4 are purchased 
    PENCILFIVE = .15,  //Represents pencil price when 5-9 are purchased 
    PENCILTEN = .10;  //Represents pencil price what 10 or more are purchased 

int 
    choice,    //Represents what number choice the user selects 
    amount;    //Represents the amount of the item selected that the user wants to purchase 

double 
    totalPrice, 
    noteTotal = 0, 
    penTotal = 0, 
    pencilTotal = 0; 

string 
    coupon; 




do 
{ 
    //Display Menu 
    cout << "\tWelcome to Martin Office Supplies\n\n"; 
    cout << "Our inventory\n\n"; 
    cout << "1. Notebooks\n"; 
    cout << "2. Pens\n"; 
    cout << "3. Pencils\n"; 
    cout << "4. Exit\n\n"; 
    cout << "Please make a selection: "; 
    cin >> choice; 


    //Validates menu selection 
    while (choice < NOTEBOOK || choice > EXIT) 
    { 
     cout << "Sorry we dont sell that item.\n"; 
     cout << "Please make another selection.\n"; 
     cin >> choice; 
    } 


    //Responds to users menu selection 
    switch (choice) 
    { 
    case NOTEBOOK: 
     cout << "How many notebooks would you like to buy?"; 
     cin >> amount; 
     noteTotal = amount * NOTEPRICE; 
     cout << "The subtotal for the notebook(s): " << noteTotal << endl << endl; 
     break; 

    case PEN: 
     cout << "How many pens would you like to buy? "; 
     cin >> amount; 
     cout << "Please type in a coupon code or NONE "; 
     cin >> coupon; 

     if (coupon == "pen123") 
     { 
      penTotal = SPECPEN * amount; 
     } 
     else 
     { 
      penTotal = PENPRICE * amount; 
     } 

     cout << "The subtotal for the pen(s): " << penTotal << endl << endl; 
     break; 

    case PENCIL: 
     cout << "How many pencils would you like to buy? "; 
     cin >> amount; 
     if (amount <= 4) 
     { 
      pencilTotal = PENCILONE * amount; 
     } 
     else if (amount >= 5) 
     { 
      pencilTotal = PENCILFIVE * amount; 
     } 
     else if (amount >= 10) 
     { 
      pencilTotal = PENCILTEN * amount; 
     } 
     cout << "The subtotal for the pencil(s): " << pencilTotal << endl << endl; 
    } 
    totalPrice = noteTotal + penTotal + pencilTotal; 
    cout << "Thank you for your order\n\n\n"; 
    cout << "Your total is $ " << totalPrice << endl; 

} while (choice != EXIT); 
return 0; 

} 
+0

1.閱讀一些字符串2.檢查字符串是否代表正整數 – MikeCAT

+1

請將您的列表限制在與問題相關的部分代碼中。 –

+0

你也可以使用無符號類型... –

回答

0

的答案,你應該嘗試:

int amount=0; 
cout << "How many notebooks would you like to buy?"; 
cin >> amount; 
while (amount<=0){ 
    cout << "you need to enter a positive number"; 
    cout << "How many notebooks would you like to buy?"; 
    cin >> amount; 
} 
+0

0不是正數。 – MikeCAT

+0

感謝這有助於很多 – acm818

+0

@MikeCAT:從數學上講,0不是正數或負數,但傳統上在計算機(使用符號位,例如IEEE754浮點類型或甚至二進制補碼)時,數字通常被認爲是正數位未設置。 – dreamlax

0

三個選項浮現在腦海中。您也可以將這些實現更好的用戶體驗:

  • 提供的信息給他(她)應該只輸入正值/用戶 - 這可能是最不可靠的是因爲錯別字總是一個選項時輸入的東西,因此這是更多的很高興有功能
  • 限制用戶可以輸入 - 每當你不希望你的用戶做一些事情時,你總是可以選擇只是限制他/她的行爲時,交互你的申請。您可以解析輸入,並且如果該值小於您的閾值(在這種情況下 - 負值也可能爲零),則返回到之前的狀態,通知用戶他/她做錯了事,並且(非常重要!)提供積極的反饋 - 告訴他/她如何解決問題
  • 使用unsigned - 存儲任何你沒有符號的號碼。在unsigned可能成爲正數時,您實際執行一些計算時,負數和正數都會被平等對待。這種做事方式的問題是你無法向用戶提供反饋。由於-1將以與+1相同的方式處理,因此您將對應用程序的內部狀態創建一種模糊處理。這是不好的做法,因爲用戶必須控制並且知道發生了什麼。

在我看來,第二個選項是處理該問題的最佳方式,因爲它不僅可以防止繼續其工作,你知道輸入的應用程序不正確,但你也給你的用戶信息,如何解決情況。