2017-04-09 146 views
1

我想創建一個程序,當用戶輸入一些我沒有定義的內容時,程序會再次提示他。如果用戶輸入無效循環

我用if語句做了它,但它只循環1次,不再做。我嘗試了循環,但每當輸入爲false時,它就會打破條件並拒絕所有輸入。在C++中。

任何幫助,非常感謝。

#include <iostream> 
#include <string> 
using namespace std; 


void xD(){string x; 
    do{cout << "Retry\n"; 
    cin >> x;}while(true);} 
//declaring a function to make the shop 
void shop(){ 
    string x; 
    float coins = 500; 
    float bow_cost = 200; 

cout << "welcome to the shop\n"; 

cout << "Bow(bow)costs 150 coins.\n"; 

     cin >> x; 
// if u chose bow you get this and get to choose again 
     if (x == "bow"){ 
     cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;} 

/*now the problem that whenever I excute the code and type something other than bow it gives me the cin only once more and then fails even if I type bow in the 2nd attempt*/ 



//in my desperate 5k attempt, I tried creating a function for it.. no use. 
//i want it o keep prompting me for input till i type "bow" and the other block excutes. but it never happens. 
    else{xD();} 

} 
int main(){ 
    string name; 
    string i; 

    cout << "if you wish to visit the shop type \"shop\"\n"; 


    cin >> i; 


     if(i == "shop"){shop();} 
     else{cin >> i;} 
     return 0; 
    } 

回答

0

問題就出在條件在這個循環塊

void xD(){ 
    string x; 
    do{ 
    cout << "Retry\n"; 
    cin >> x; 
    }while(true); 
} 

while(true)條件使得它不管輸入的永遠循環。要解決這個問題,您可以更改條件:

void xD(){ 
    string x; 
    do{ 
    cout << "Retry\n"; 
    cin >> x; 
    }while(x!="bow"); 
cout << "you bought the bow. and some other messages"<<endl; 
} 

這應該有效。但是,這對我來說依然太複雜。這可以簡化爲下面的代碼片段:

void shop(){ 
    string x; 
    float coins = 500; 
    float bow_cost = 200; 

    cout << "welcome to the shop\n"; 

    cout << "Bow(bow)costs 150 coins.\n"; 

    cin >> x; 
    while (x!="bow"){ 
    cout << "Retry\n"; 
    cin>>x; 
    } 
    cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x; 

} 
+0

非常感謝你,它像一個魅力工作! –

+0

現在每當我輸入一個無效值然後輸入一個有效的值時,程序意外結束。 –

0

而不是做這種做法(這是檢查狀態只有一次):

if (x == "bow"){ 
     cout << "you bought the bow.\n you now have " <<coins - bow_cost << " 
    coins." << endl; cin >> x; 
} else{ 
    xD(); 
} 

這實際上是一個RECURSIVEinvocation的方法的xD()

你應該做一個do-while循環,

例如:

while (x.compare("bow") != 0) 
{ 
    cout << "sorry, wrong input, try again..."; 
    cin >> x; 
} 

注意使用比較方法,而不是==操作符

here更多關於它的文檔中的

0

可以使用CIN的返回值>> [您輸入對象]在這裏檢查狀態或istream的方法fail()。只要輸入流不能解析整個或部分流,它就會失敗並保持在故障狀態,直到您清除它爲止。未分析的輸入被保留下來(所以你可以嘗試以不同的方式解析它)?所以如果你再次嘗試>>到相同類型的對象,你會得到同樣的失敗。要忽略稱輸入N個字符,有方法

istream::ignore(streamsize amount, int delim = EOF) 

例子:

int getInt() 
{ 
    while (1) // Loop until user enters a valid input 
    { 
     std::cout << "Enter an int value: "; 
     long long x; // if we'll use char, cin would assume it is character 
     // other integral types are fine 
     std::cin >> x; 

     // if (! (std::cin >> x)) 
     if (std::cin.fail()) // has a previous extraction failed? 
     { 
      // yep, so let's handle the failure, or next >> will try parse same input 
      std::cout << "Invalid input from user.\n"; 
      std::cin.clear(); // put us back in 'normal' operation mode 
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // and remove the bad input 
     } 
     // Thechnically you may do only the above part, but then you can't distingusih invalid format from out of range 
     else if((x > std::numeric_limits<int>::max()) || 
      (x < std::numeric_limits<int>::min())) 
     { 
      std::cout << "Invalid value.\n"; 
     } 
     else // nope, so return our good x 
      return x; 
    } 
} 

對於字符串解析幾乎總是成功的,但你需要字符串比較的一些機制,你有另外一個是允許。嘗試尋找使用std::find()以及某些容納允許選項的容器,例如以pair<int,string>的形式,並在switch()語句中使用int索引(或者在您給它的函數中使用find_ifswitch())。

0

考慮到if()語句是one_direction道路,它會檢查條件,如果條件成立它關係到它的支架和做等等等等,如果有任何條件編譯器的問題通過if並跳轉到編譯其他代碼。

每次開始編譯代碼時,它都會從int main()函數開始。在ifelse報表中再次發生了錯誤 下面是正確的代碼。我做了必要的更改。

#include "stdafx.h" 
#include <iostream> 
#include <string> 

using std::string; 
using std::cin; 
using std::cout; 

#define coins 500 ; 
#define bow_cost 200 ; 

int shop(string x) 
{ 
    //There is no need to allocate extra memory for 500 and 200 while they are constant.`` 

    cout << "welcome to the shop\n"; 
    cout << "Bow(bow)costs 150 coins.\n"; 


    do 
    { 
     cout << "Input another :\n"; 
     cin >> x; 
     if (x == "bow") 
     { 
      return (coins - bow_cost); //return to function as integer 
     } 
    } while (true); 

} 

int main() 
{ 
    string name, i; 

    cout << "if you wish to visit the shop type \"shop\"\n"; 
    cin >> i; 

    if (i == "shop") 
    { 
     cout << "Input :\n"; 
     cin >> name; 
     cout << shop(name) << "you bought the bow.\n you now have " << " coins." << "\n"; 
    } 

    //argument passed to shop funnction parameters. 

    system("pause"); 
    return 0; 
} 
相關問題