Siemka. Otóż napisałem prosty program do obliczania BMI i w niektórych przypadkach pojawia mi się błąd.
"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Nieprawidłowy format ciągu wejściowego."
Błąd ten pojawia się gdy zostawię 2 puste textboxy, bądź pusty zostanie textbox podpisany jako "wys". Z pustym textboxem "waga" działa jak należy.
#pragma once
#include <windows.h>
#include <cstring>
#include <sstream>
namespace BMIvisual {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ wys;
protected:
private: System::Windows::Forms::Label^ Label1;
private: System::Windows::Forms::Label^ Label2;
private: System::Windows::Forms::TextBox^ waga;
private: System::Windows::Forms::GroupBox^ groupBox1;
private: System::Windows::Forms::Label^ wynik;
private: System::Windows::Forms::Label^ info;
private: System::Windows::Forms::Panel^ panel1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->wys = (gcnew System::Windows::Forms::TextBox());
this->Label1 = (gcnew System::Windows::Forms::Label());
this->Label2 = (gcnew System::Windows::Forms::Label());
this->waga = (gcnew System::Windows::Forms::TextBox());
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->info = (gcnew System::Windows::Forms::Label());
this->wynik = (gcnew System::Windows::Forms::Label());
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->groupBox1->SuspendLayout();
this->panel1->SuspendLayout();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(44, 88);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(156, 33);
this->button1->TabIndex = 0;
this->button1->Text = L"Oblicz BMI";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// wys
//
this->wys->Location = System::Drawing::Point(106, 8);
this->wys->Name = L"wys";
this->wys->Size = System::Drawing::Size(87, 20);
this->wys->TabIndex = 1;
//
// Label1
//
this->Label1->AutoSize = true;
this->Label1->Location = System::Drawing::Point(5, 11);
this->Label1->Name = L"Label1";
this->Label1->Size = System::Drawing::Size(88, 13);
this->Label1->TabIndex = 3;
this->Label1->Text = L"Wysokość w cm:";
//
// Label2
//
this->Label2->AutoSize = true;
this->Label2->Location = System::Drawing::Point(5, 41);
this->Label2->Name = L"Label2";
this->Label2->Size = System::Drawing::Size(65, 13);
this->Label2->TabIndex = 4;
this->Label2->Text = L"Waga w kg:";
//
// waga
//
this->waga->Location = System::Drawing::Point(106, 38);
this->waga->Name = L"waga";
this->waga->Size = System::Drawing::Size(87, 20);
this->waga->TabIndex = 5;
//
// groupBox1
//
this->groupBox1->Controls->Add(this->info);
this->groupBox1->Controls->Add(this->wynik);
this->groupBox1->Location = System::Drawing::Point(240, 12);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(134, 92);
this->groupBox1->TabIndex = 6;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Wynik";
//
// info
//
this->info->AutoSize = true;
this->info->Location = System::Drawing::Point(6, 57);
this->info->Name = L"info";
this->info->Size = System::Drawing::Size(0, 13);
this->info->TabIndex = 1;
//
// wynik
//
this->wynik->AutoSize = true;
this->wynik->Location = System::Drawing::Point(6, 25);
this->wynik->Name = L"wynik";
this->wynik->Size = System::Drawing::Size(0, 13);
this->wynik->TabIndex = 0;
//
// panel1
//
this->panel1->Controls->Add(this->waga);
this->panel1->Controls->Add(this->Label2);
this->panel1->Controls->Add(this->Label1);
this->panel1->Controls->Add(this->wys);
this->panel1->Location = System::Drawing::Point(7, 13);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(205, 69);
this->panel1->TabIndex = 7;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(388, 133);
this->Controls->Add(this->panel1);
this->Controls->Add(this->groupBox1);
this->Controls->Add(this->button1);
this->MaximumSize = System::Drawing::Size(404, 171);
this->MinimumSize = System::Drawing::Size(404, 171);
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"BMI";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
this->panel1->ResumeLayout(false);
this->panel1->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
float bmi, a, b;
if ( ( wys->Text == "") || ( waga->Text == "") )
MessageBox::Show("Uzupełnij dane!","Nie tak szybko!");
else
a = Double::Parse(waga->Text);
b = Double::Parse(wys->Text)/100;
bmi = (a/(b*b));
wynik->Text = ("BMI: ") + (bmi.ToString( "0.##" ));
if ( bmi <= 18.5 )
info->Text = ("Niedowaga.");
else if ( bmi <= 25 )
info->Text = ("Prawidlowa waga ciala.");
else if ( bmi <= 30 )
info->Text = ("Nadwaga bez otylosci.");
else if ( bmi <= 35 )
info->Text = ("I stopien otylosci.");
else if ( bmi <= 40 )
info->Text = ("II stopien otylosci.");
else if ( bmi > 40 )
info->Text = ("III stopien otylosci \n (ekstremalna).");
}
};
}
Używam Visual C++ 2008 Express.