Nouman Arshad Nouman Arshad - 3 months ago 7
C++ Question

Label text doesn't update on Windows form after adding 0 to it by type casting C++

I'm making a simple calculator in windows form through C++.. I have a condition in which when i click 0 button after being in decimal part it should update label text on screen... but it doesn't .. forexample:

i have on screen
334.5
i click 0 button, it doesn't update on label
but when i click other button lets say 8 then it updates like this

334.508

I'm type casting from double type to string and vice versa...

here is a piece of code ...

int totalLength = 0;
bool decimalPressed = false;
int decimalPart = 1;
bool isNegative = false;

void AddNewNum(double num){
if (totalLength < 12){
if (decimalPressed == false){
if (isNegative == false){
double prevDisplay = Convert::ToDouble(label1->Text);
prevDisplay = (prevDisplay * 10) + num;
label1->Text = Convert::ToString(prevDisplay);
}
else{
double prevDisplay = -1 * Convert::ToDouble(label1->Text);
prevDisplay = (prevDisplay * 10) + num;
label1->Text = Convert::ToString(-1 * prevDisplay);
}
}
else{// for adding digits in decimal part in this section error occurs .. only problem is display everything is okay
if (isNegative == false){
double prevDisplay = Convert::ToDouble(label1->Text);
if (num != 0)
prevDisplay = ((prevDisplay * (pow(10, decimalPart)) + num) / pow(10, decimalPart));
else{
prevDisplay = ((prevDisplay * (pow(10, decimalPart))) / pow(10, decimalPart));
}
label1->Text = Convert::ToString(prevDisplay);
}
else{
double prevDisplay = -1 * Convert::ToDouble(label1->Text);
prevDisplay = ((prevDisplay * (pow(10, decimalPart)) + num) / pow(10, decimalPart));
if (num != 0)
prevDisplay = ((prevDisplay * (pow(10, decimalPart)) + num) / pow(10, decimalPart));
else{
prevDisplay = ((prevDisplay * (pow(10, decimalPart))) / pow(10, decimalPart));
}
label1->Text = Convert::ToString(-1 * prevDisplay);
}
decimalPart++;
}
totalLength++;
}
else maskedTextBox1->Text = "LIMIT REACHED";
}

Answer

334.5, 334.50, 334.50000000000, these are all the same number to a double. You need to specify how many decimal places to display when formatting a double as a string with trailing zeros.

You should not be converting between double and string at all when you are just appending digits. Use simple string concatenation instead:

label1->Text = label1->Text + Convert::ToString(num);

Convert between double and string only when performing actual arithmetic operations, or if you want to validate the new string before assigning it, eg:

string s = label1->Text + Convert::ToString(num);
Convert::ToDouble(s); // check for errors
label1->Text = s;

Either way, there is no reason to keep track of any decimalPart value manually at all. If the user enters a decimal, you can detect that so you can insert a . character when a subsequent digit is entered.

bool decimal = false;

...

// user enters decimal point...
decimal = true;

...

// user enters a digit...
string s = label1->Text;
if (decimal) {
    decimal = false;
    s = s + ".";
}
s = s + Convert::ToString(num);
Convert::ToDouble(s); // check for errors
label1->Text = s;