visual c++... what did I do wrong?
I'm teaching myself Visual C++ by way of a textbook and Express Ed. 2005.
The textbook has me making this lame little number adder (not a full calculator) and it seems simple enough. I was able to write the program with the book's guidance and the program works if you enter numbers and not other strange characters. I have my input variables set to "float" and all is well...
... next lesson the book wants you to add code to test your input for whether characters other than numbers have been input into the input boxes (using the TryParse method and if/else phrases... and then a subroutine to tie it all together).
And I understand what I'm supposed to be doing... I type everything right (I think) and with the new code, the program still runs when only regular numbers are input. But the program crashes when I put in letters... and it should just return 0 to my answer box.
here's the block of code I wrote:
Code:
private: System::Void calcbutton_Click(System::Object^ sender, System::EventArgs^ e) {
float valueone; //define 3 variables, could use int, but float will let decimal numbers work too
float valuetwo;
float answer;
valueone = InputCheck(inputbox1->Text, valueone);
valuetwo = InputCheck(inputbox2->Text, valuetwo);
//test input1
//valueone = float::Parse(inputbox1->Text);//read text to variables... float::Parse() lets data be read from a string to a float
//test input2
// valuetwo = float::Parse(inputbox2->Text);//read text to variables... float::Parse() lets data be read from a string to a float
//process variables
answer = valueone + valuetwo;// add the variables into the answer variable
answerbox->Text = answer.ToString();//display answer. answer.ToString() reads variable and prints to a string for display.
}
private: float InputCheck(System::String^ textValue, float% value)
{
if (float::TryParse(textValue, value) == true)
{
value = float::Parse(textValue);
}
else
{
value = 0;
}
return value;
}
just ignore all my notes... and notice that the original input to Parse lines have been commented out (that was the original code and it works fine...) then I tried adding the InputCheck subroutine and it doesn't work... and I don't know why... Individual If/else statements for each of the variables "valueone" and "valuetwo" don't work either... WHY!? Did I type something wrong that I'm not seeing?
Build/Debug returns no errors... Only when I run the program through Visual C++ and enter letters for my variables then a window pops up saying roughly this(translated from Japanese):
An exception not handled by 'System.FormatException' was displayed by mscorlib.dll
included information: the form of an entered line is incorrect.
(I'll run the program on my computer at home and get the English version of the error message later)
I assume that I must have typed something wrong... but what?
Should the subroutine be nested within the same "calcbutton_Click" method like all the other processes? I tried that though and it didn't solve the problem....