2016-11-17 35 views
-4

我是C++的新手。我已經給出了任務,我必須計算成績並詢問用戶的輸入。如果他輸入錯誤的輸入,我必須重新開始程序。如果用戶輸入正確的輸入,我必須處理數據,並再次詢問他是否想檢查另一個計算。迄今爲止,我已經編寫了這些代碼。如果用戶輸入錯誤的輸入,並且如果成功則再次啓動程序,我不知道如何在程序中再次循環。請給我指導。謝謝。用C++循環檢查錯誤的輸入,並在用戶輸入正確的輸入時再次啓動程序

#include <cstdlib> 
#include <iostream> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    //Declaring Variable 
    char choice; 
    char input; 
    //Promptin User to Enter his/her Choice 
    cout<<"Enter C or c for Computer Science: \n" ; 
    cout<<"Enter S or s for Software Engineering: \n"; 
    cout<<"Enter T or T for Telecom Engineering: \n"; 

    cout<<"Select any option from the above Menu: "; 
    cin>>input; 



    if (input != 'c' || input != 'C'){ 

     cout<<"Invalid input! Enter the correct input option again"; 
    }else if (input != 's' || input != 'S'){ 
     cout<<"Invalid input! Enter the correct input option again"; 
    }else if (input != 't' || input != 'T'){ 
     cout<<"Invalid input! Enter the correct input option again"; 
    }else if (input == 'a' || input == 'A'){ 

    } 





    system("pause"); 
    return 0; 

} 
+2

使用'do while()'循環怎麼樣? –

+1

'input'必須同時是'c'和'C'(怎麼樣?)不能滿足'input!='c'||輸入!='C''。爲什麼這麼難? – LogicStuff

+1

@LogicStuff對於一些邏輯來說,邏輯幾乎是不合邏輯的。 :) –

回答

0

您可以使用一個簡單的循環do while做到這一點:

bool valid_input = false; 

do 
{ 
    // Code to read and validate the input... 
} while (valid_input == false); 

如果輸入的是有效的,那麼你設置valid_inputtrue循環將結束。


在一個不相關的注意事項,如果不區分約大寫或小寫,例如使用std::tolower所以你只需要比較一次這封信。例如。 std::tolower(input) != 'c'

0

這裏是提示用戶回答的代碼,只要答案是用switch語句定義的。 ans是一個變量,用於保存一個字符,1或0對應於用戶的輸入(在開關盒中定義或不定義)。如果定義了,那麼ans得到1另外明智的是它得到值0.當ans是1(在switch語句中定義)時while while循環重複。

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


int main() { 

char input; 
char ans; //correct answer, incorrect answer 


do { 
    cout<<"Enter C or c for Computer Science: \n" ; 
    cout<<"Enter S or s for Software Engineering: \n"; 
    cout<<"Enter T or T for Telecom Engineering: \n"; 
    cout<<"Select any option from the above Menu: "; 
    cin>>input; 
     switch (input){ 
     case 'S': 
      ans = 1; 
      break; 
     case 's': 
     ans = 1; 
     break; 
     case 'C': 
      ans = 1; 
      break; 
     case 'c': 
     ans = 1; 
     break; 
     case 'T': 
      ans = 1; 
      break; 
     case 't': 
     ans = 1; 
     break; 
     default: 
     ans = 0; 
} 

} while (ans); 


return 0; 
} 
+0

你可以使用'std :: tolower'或每個塊有多個case:'case'S':\ n case's':ans = 1;打破;' – stefaanv

0

用戶輸入處理非常普遍,通常可以使用類似的模式。

基本上,你重新要求輸入。您處理有效的選擇並跳出循環,並在選擇無效時顯示錯誤,並讓循環再次詢問輸入。

備註1:通過不使用開關盒,我可以立即跳出循環。我立即中斷以避免指定條件兩次或使用標誌,這也是爲什麼我使用沒有最終條件的循環。

備註2:std::flush用於在提示行輸入。它確保在等待輸入之前顯示提示。

char inp = 0; 
while (true) { 
    std::cout << "Give input (a, b): " << std::flush; 
    std::cin >> inp; 
    inp = std::tolower(inp); 
    if (inp == 'a') { 
     std::cout << 'a\n'; 
     break; 
    } 
    if (inp == 'b') { 
     std::cout << 'b\n'; 
     break; 
    } 
    std::cout << "invalid choice (" << inp << ")"; 
} 

無效的選擇處理可以做更多的普通一點點這個功能,但有效選擇的處理仍必須在本地進行:

#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 

char askInputChoice(const std::string& prompt, const std::vector<char>& valid) 
{ 
    char inp = 0; 
    while (true) { 
     std::cout << prompt << ": " << std::flush; 
     std::cin >> inp; 
     inp = std::tolower(inp); 
     if (std::find(valid.begin(), valid.end(), inp) != valid.end()) { 
      break; 
     } 
     std::cout << "invalid choice (" << inp << ")\n"; 
    } 
    return inp; 
} 

int main() 
{ 
    char inp = askInputChoice("Give input (a, b)", std::vector<char>{'a','b'}); 
    switch (inp) { 
    case 'a': 
     std::cout << "a\n"; 
     break; 
    case 'b': 
     std::cout << "b\n"; 
     break; 
    } 
} 

重新啓動程序,把它放在一個while循環,添加一個選項以退出('q')並在給出該選項時中斷。

0

謝謝你的支持。其實這是我在C++中的第一個程序,並對不起,我已經使用了指導。其實我已經編譯成功了。請檢查我的程序,我知道你不需要,但我想知道如果我可以添加更多,以改善它。

#include <cstdlib> 
#include <iostream> 
#include <conio.h> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    //Declaring Variable 
    char choice; 
    char input; 
    int addTest = 0, matricMarks = 0 , interMarks = 0 , result = 0; 
    start: //Label 
    system("cls"); // Clear the Screen 


    //Prompting User to Enter his/her Choice 
    cout<<"Please Select the Degree Programme in which you are interested in to take Addmission: "; 
    cout<<"\nEnter C or c for Computer Science: "<<endl ; 
    cout<<"Enter S or s for Software Engineering: "<<endl; 
    cout<<"Enter T or t for Telecom Engineering: \n"<<endl; 

    cout<<"\nSelect any option from the above Menu: "; 
    cin>>input; 

    //Switch Statement Started 
    switch(input){ 
     //Case C Started 
     case 'c': 
     case 'C': 

       { 
        cout<<"Enter your Marks in Addmission Test: "; 
        cin>>addTest; 
        cout<<"Enter your Marks in Matric Degree: "; 
        cin>>matricMarks; 
        cout<<"Enter your Marks in Intermediate Degree: "; 
        cin>>interMarks; 

        result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50); 

        if (result >= 70) 
        { 
         cout<<"\nCongratulations! You are eligible for the Computer Science degree program :)"<<endl; 

        } 
        else 
        { 
         cout<<"Sorry you Do not qualify for Computer Science Degree Programme: "<<endl; 
         system("pause"); 
        } 
        break; 
       }//Case C Closeed 

    //Case s Started 
     case 's': 
     case 'S': 
      { 
        cout<<"Enter your Marks in Addmission Test: "; 
        cin>>addTest; 
        cout<<"Enter your Marks in Matric Degree: "; 
        cin>>matricMarks; 
        cout<<"Enter your Marks in Intermediate Degree: "; 
        cin>>interMarks; 

        result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50); 

        if (result >= 85) 
        { 
         cout<<"\nCongratulations! You are eligible for the Software Enginnering degree program :)"<<endl; 

        } 
        else 
        { 
         cout<<"\nSorry! you Do not Qualify for Software Engineering Degree: "<<endl; 
         system("pause"); 
        } 
        break; 

      }//Case S Closed 

    //Case t Started 
     case 't': 
     case 'T': 
      {  
        cout<<"Enter your Marks in Addmission Test: "; 
        cin>>addTest; 
        cout<<"Enter your Marks in Matric Degree: "; 
        cin>>matricMarks; 
        cout<<"Enter your Marks in Intermediate Degree: "; 
        cin>>interMarks; 

        result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50); 

        if (result >= 80) 
        { 
         cout<<"\nCongratulations! You are eligible for the Telecom Engineering degree program :)"<<endl; 

        } 
        else 
        { 
         cout<<"Sorry you Do not Qualify for Telecom Enginnering Degree Programme: "<<endl; 
         system("pause"); 
        } 
        break; 
      }//Case t Closed 

    //Default Case Started 

     default: 
      { 
        cout<<"\nInvalid input! Enter the correct option again: \n"; 
        system("pause"); 
        goto start;     //Jump To Label Start  
      }//Deafult Case Closed 

    }// Switch Statement Close 

    //do while Loop Started 
    do{ 

     cout<<"\nDo you want to check your eligibility in some other degree program y/n? : "; 
     cin>>choice; 
     if (choice=='Y'||choice=='y') 
     { 
      goto start;         //jump to Label start: 
     } 
     else if (choice=='N'||choice=='n') 
     { 
      break; 
     } 
     }while(choice == 'y' || choice == 'Y'); 
    //Do while Loop Closed 





    system("pause"); 
    return 0; 

} 
+0

很高興看到一些反饋。不幸的是,這還不夠。然而,更常見的反饋方式是上/下調,接受和評論問題/答案(upvoting也是一種鼓勵人們繼續幫助你的方式)。代碼審查,我指導你到http://codereview.stackexchange.com。請注意,他們也有規則和慣例。 – stefaanv

+0

我注意到儘管所有的答案都向你建議了一個while循環,但你使用了goto。 Goto's可能會在那裏使用,在你的練習中,它仍然很簡單。然而,使用goto's(over)可能會相當快地產生問題,我希望你不必在完成任務的那一晚就陷入困境。他們幾乎從未使用過,因爲3 goto's的組合已經很難遵循了。 – stefaanv

+0

在這種情況下,goto start可以用'bool restart = true替換;在'choice =='n''分支中執行{...} while(restart)'loop和'restart = false;'也要注意重啓問題的有效性檢查是錯誤的:用調試器檢查它並一些不同的輸入) – stefaanv

相關問題