2014-10-11 23 views
1

好吧,我非常新 - 嘗試除外。我不知道我想要做什麼是可能的,但我想我會要求輸入。使用異常來將用戶輸入存儲爲雙精度或字符串

我的程序應該平均x的用戶輸入量,直到他/她輸入q。

這是給我最多問題的功能。

矢量USER_INPUT(){

vector<double> scores; 
    string user_command; 
    do{ 
     double user_input; 
     cout << "Enter the scores to be averaged (range 0-100) or enter q to quit: " << endl; 
     cin >> user_command; 
     if (is_number(user_command)) 
     { 
      user_input = atof(user_command.c_str()); 
      if (user_input < 0 || user_input > 100) 
       cout << "This is not within range!" << endl; 
      else{ 
       scores.push_back(user_input);} 
     } 

    } 
    while (user_command != "q" && user_command != "Q"); 
    return scores; 

}

我需要這個節目爲什麼不會編譯一些見解。任何幫助,將不勝感激

+0

您需要先解決您的語法問題。你在第一個'cout <<'行的結尾缺少';',你錯過了一些大括號。 – Barmar 2014-10-11 01:52:02

+0

您也錯過了'try'語句的'catch'塊。 – Barmar 2014-10-11 01:53:00

+0

這個例子有大約一百萬個錯誤,直到例子修復。 – 2014-10-11 01:56:08

回答

0

你沒有明確指定你的要求,這使得很難知道如何回答這個問題。我假設你想是這樣的:

#include <string> 
#include <vector> 
#include <iostream> 
#include <sstream> 

typedef double score_type; //so we don't need to write 'double' everywhere, makes it easy to change the type 

void user_input(std::vector<score_type>& scores) 
{ 
    std::string command; 

    for (;;) 
    { 
     score_type score; 

     std::cout << "Enter the scores to be averaged (range 0-100) or enter q to quit: " << std::endl; 

     std::cin >> command; 

     if (command == "q" || command == "Q") 
     { 
      //works better than a do-while in this case 
      break; 
     } 

     try 
     { 
      //stod to throw std::invalid_argument and std::out_of_range, check documentation (http://en.cppreference.com/w/cpp/string/basic_string/stof) 
      score = std::stod(command.c_str()); 
     } 
     catch (std::exception e) 
     { 
      //build exception string 
      std::ostringstream oss; 
      oss << "bad input: " << command; 

      //throw new exception to be handled elsewhere 
      throw std::exception(oss.str().c_str()); 
     } 

     if (score < 0 || score > 100) 
     { 
      std::cerr << "Input is not within range (0-100)!" << std::endl; 
     } 

     scores.push_back(score); 
    } 
} 

int main() 
{ 
    for (;;) 
    { 
     std::vector<score_type> scores; 

     try 
     { 
      user_input(scores); 
     } 
     catch (std::exception e) 
     { 
      std::cout << "input exception: " << e.what() << std::endl; 
     } 

     score_type score_sum = 0; 

     for (auto score : scores) 
     { 
      score_sum += score; 
     } 

     score_type average_score = 0; 

     if (!scores.empty()) 
     { 
      average_score = score_sum/scores.size(); 
     } 

     std::cout << "average score: " << average_score << std::endl; 
     std::cout << "go again? (y/n): " << std::endl; 

     std::string command; 

     std::cin >> command; 

     if (command == "y") 
     { 
      continue; 
     } 
     else if (command == "n") 
     { 
      break; 
     } 
    } 
} 

在未來,確保所有在你未來的問題張貼代碼可以編譯和任何人跑了,尤其是像這樣簡單的東西。在發佈之前,您可以使用http://ideone.com/來測試您的代碼示例。

相關問題