2012-09-30 26 views
0

Possible Duplicate:
C++ Windows Forms application unhandled exception error when textbox emptyC++ Windows窗體應用程序未處理的異常錯誤時,文本框爲空

我建立在Visual Studio中的溫度轉換應用程序的C++課程。這是一個Windows窗體應用程序,我寫的代碼如下。當然還有其他的代碼,但我不確定你需要它來幫助我。

我的問題是,當我運行的應用程序,如果我沒有任何東西進入任一txtFahrenheit或txtCelsius2文本框,我得到以下錯誤:

「類型‘System.FormatException’發生未處理的異常在mscorlib.dll中「

當兩個文本框中都輸入數字時,該應用程序現在才工作。

private: System::Void btnFtoC_Click(System::Object^ sender, System::EventArgs^ e) 
      {    
       // Convert the input in the Fahrenheit textbox to a double datatype named fahrenheit for manipulation 
       double fahrenheit = Convert::ToDouble(txtFahrenheit->Text); 
       // Set the result string to F * (5/9) -32 
       double result = fahrenheit * .5556 - 32; 
       // Set the Celsius text box to display the result string 
       txtCelsius->Text = result.ToString(); 
      } 

private: System::Void btnCtoF_Click(System::Object^ sender, System::EventArgs^ e) 
      { 
       // Convert the input in the Celsius textbox to a double datatype name celsius for manipulation 
       double celsius = Convert::ToDouble(txtCelsius2->Text); 
       // Set the result2 string to C * (9/5) + 32 
       double result2 = celsius * 1.8 + 32; 
       // Set the Fahrenheit text box to display the result2 string 
       txtFahrenheit2->Text = result2.ToString(); 
      } 
+0

改爲使用Double :: TryParse()。 –

+0

@HansPassant而不是什麼?這是我的第一個C++程序,我現在還沒有聽說過?我改成這個double fahrenheit = Double :: TryParse(txtFahrenheit-> Text);但現在我又遇到了另一個錯誤。雖然我可能實施了錯誤。 – cmorris1441

+0

避免只是隨機嘗試代碼。使用MSDN Library來查找Double.TryParse方法定義。它有兩個論點。 http://msdn.microsoft.com/en-us/library/994c0zb1.​​aspx#Y0 –

回答

2

您不能將空字符串轉換爲Double

您應該使用if語句來完成其他操作。

相關問題