2011-09-08 22 views
3

這是我第一次使用Stackoverflow。 我正在製作一個程序來找出汽車的MPG。我想知道如何讓cin語句只接受正整數?而且,如果您確實輸入了無效輸入,您是否可以重置它?我不確定這是否合理。我不必爲課堂做這件事。我只是好奇如何去做。這是代碼。C++ cin正整數只有

#include <iostream> 
using namespace std; 

int main() 
{ 
double tank, miles, mpg; 

cout << "Hello. This is a program that calculates the MPG (Miles Per Gallon) for  your\n" ; 
cout << "vehicle\n" << endl; 
cout << "Please enter how many gallons your vehicle can hold\n" << endl; 
cin >> tank; 
cout << endl; 
cout << "Please enter how many miles that have been driven on a full tank\n" <<endl; 
cin >> miles; 
cout << endl; 

mpg = (miles)/(tank); 
cout << "Your vehicle recieves " << mpg << " miles per gallon\n" << endl; 
system ("pause"); 
return 0; 
} 
+3

不是你的問題的答案,但如果你想要整數你不應該要求雙打。 –

+0

首先要求整數 - 然後處理它不是正數的事實。更好的是隻讀字符串,然後解析它們以確保是必需的/如果有人輸入了一些不需要的東西並給予他們另一個機會,則標記錯誤。 –

+0

是的,你不能從鍵盤上物理刪除鍵。人們會輸入錯誤。 – MSalters

回答

4

iostreams不是構建複雜UI的工具包。除非你想編寫你自己的相當複雜的流來包裝普通的流,否則你不可能(a)只接受正整數,或者(b)有禮貌地與其他類型的用戶進行交互。

你應該只是從cin讀取行,然後打印自己的錯誤提示等,看看你得到什麼。

+0

我認爲最後一句描述了這個問題。他沒有要求任何幻想。 – Potatoswatter

-3

而不是

cin >> miles; 

嘗試

while ((cin >> miles) < 0) 
cout << "Please enter how many gallons your vehicle can hold\n" << endl; 

這將重複問題,直到輸入爲正。你也可以爲其餘的問題做到這一點。

請注意,輸入流不適用於輸入過濾。你必須爲此提供你自己的邏輯。

+6

'cin >>英里'表達式返回'cin',所以我不知道這應該如何工作。 – user786653

+0

這也不會對非數字輸出做任何錯誤檢查。 –

1
cout << "Hello. This is a program that calculates the MPG (Miles Per Gallon) for  your\n" ; 
cout << "vehicle\n" << endl; 
do 
{ 
    cout << "Please enter how many gallons your vehicle can hold\n" << endl; 
    cin >> tank; 
    cout << endl; 
} while (tank <= 0 && ((int)tank != tank)); 
do 
{ 
    cout << "Please enter how many miles that have been driven on a full tank\n" <<endl; 
    cin >> miles; 
    cout << endl; 
} while (miles <= 0 && ((int)miles != miles)); 

如果在運行報表後這樣做會重新運行它們,如果答案是0或更低或不是整數。如果你讓變量ints而不是double,那麼你可以刪除while語句的「& &((int)miles == miles)」部分。

+0

我看到你明白我的意思,但是當我把它放在我的代碼中時,它只是不斷重複相同的事情。它一直在說請輸入你的車可以容納多少加侖。而且我確實將它改爲int而不是double。 – Sujay

+0

哦,我的!我似乎已經鍵入==當我的意思是!= 我編輯了它現在應該工作的帖子 – annirun

0

不過,在命令行環境中有幾種標準的方法可以實現。

您可以將cin語句捕獲到循環中,直到輸入有效輸入纔會釋放。這是驗證CLI輸入的「標準」方式,而不僅僅是有符號數字。

do 
{ 
    cout << "\nPlease enter..."; 
    cin >> tank; 
} 
while (tank < 0) 

while語句中的條件是驗證數據的地方。您也可以通過if語句來解釋輸入無效的原因。

另一種方法是通過簡單地去tank = fabs(tank);來簡單地強制該值爲正值,該值取tank變量的絕對值(即正值)。

+2

如果輸入一個字母,這將進入無限循環。 – Potatoswatter

+0

實際上它是未定義的行爲,因爲'tank'是未初始化的,因此不能保證在第一次達到條件時被初始化。 –

+0

真的嗎? Potatoswatter,如果輸入一個字母,不應該只是認爲這個條件是假的,循環並做一個新的cin?卡爾:坦克可以是未初始化的,因爲路徑將首先通過cin。這是做...的好處,而不是一會兒。 –

0
So this is my code for an infinite loop 
    1: So main will call the "Get_number()" function 
    2: Get number will accept an int from the user 
    3(A): If int is greater than 0, go into loop 
    3(B): Else, display to user "Invalid Input" and then call the function 
      "Get_number()" again creating an infinite loop until the user 
      enters a value greater than 0 



    #include <iostream> // Access the input output stream library 
    #include <fstream> // Access to the fstream library (used to read and write to files) 
    #include <chrono> // Needed to access "std::chrono_literals" 
    #include <thread> // Needed to access "namespace std::this_thread" 

    using std::fstream; // this will allow us to use the fstream (we'll be able to read and write to files) 
    using std::ios;  // needed for iostream (used to be able to tell fstream to read and/or write to a file and that it's reading/writing a binary file) 
    using std::cout; // need this statment to access cout (to display info to user) 
    using std::cin;  // need this statment to access cin (to gather info from user) 
    using std::endl; // need this statment to access endl (will end the line) 
    using namespace std::this_thread; // This will allow me to use "Sleep_For" or "Sleep_Until" 
    using namespace std::chrono_literals; // This will allow the use of measurements of time such as ns, us, s, h, etc. 

    //Prototypes*************************************************************************************************** 
    void shellSort(int read[], int readLength); //Making Prototype (Declaring our function) so that compiler knows not to worry about it 
    void Get_number(); 
    void Write_to_file(int user_input_of_how_many_random_numbers_to_generate); //Making Prototype (Declaring our function) so that compiler knows not to worry about it 
    void Read_from_file(int user_input_of_how_many_random_numbers_to_generate);//Making Prototype (Declaring our function) so that compiler knows not to worry about it 
    //************************************************************************************************************* 

    void main() 
    { 
     Get_number(); 

     system("pause>>void"); // will let the console pause untill user presses any button to continue 

    } 

    /************************************************************************************************************** 
    * Purpose: This function will gather a positive integer from the user and use it to generate that many 
    *   random numbers! 
    * 
    * Precondition: None 
    * 
    * 
    * Postcondition: 
    *   Would've gathered the number of random numbers the user wanted to generate and then gone into the 
    *   Write_to_file and Read_from_file function 
    * 
    **************************************************************************************************************/ 

    void Get_number() 
    { 
     int user_input_of_how_many_random_numbers_to_generate = 0; //make variable that will accept the int value the user wants to generate random numbers 
     cout << "Please Enter A Number Greater Than Zero:" << endl; // displays to user to enter a number greater than zero 
     cin >> user_input_of_how_many_random_numbers_to_generate; // will accept the value the user inputted and place it in the "user_input_of_how_many_random_numbers_to_generate" variable 
     system("cls"); // Will clear the screen 
     if (user_input_of_how_many_random_numbers_to_generate > 0) // if user input is greater than zero, enter this 
     { 

      Write_to_file(user_input_of_how_many_random_numbers_to_generate); // will bring up the "Write_to_file" function 
      Read_from_file(user_input_of_how_many_random_numbers_to_generate); // will bring up the "Read_from_file" function 
     } 
     else // else enter this 
     { 
      cout << "invalid input!" << endl; // display to user "invalid input" 
      sleep_for(2s); // system will pause for 2 seconds allowing the user to read the message of "invalid input" 
      system("cls"); // console will be cleared 
      Get_number(); // Get_number function will be entered creating an infinate loop untill the user's input is valid! 
     } 
    }