# Calculative help !!

Hi

My friend has a problem with this delphi code…
She wants a set of integer numbers that people enter into boxes and results give total…Can anyone help.
This is what she has sent:

var
Number : string;
Count, Sum : integer;
begin
Count := 1;
Sum := 0;
for Count := 1 to 20 do;
Number := InputBox(‘Enter number’, ‘Number’, ‘’); //create InputBox
Count := Count + 1;
Sum := Sum + StrToInt(Number);
lblIterationNumber.Caption := ‘Iteration Number:’ + Number;
lblSum.Caption := ‘Cumulative Sum:’ + IntToStr(Sum);
end;
end.

Tx for any help

Alright, as Brandon said, this isn’t a Borland Delphi forum—here, Delphi refers to the auto parts maker Delphi, a sponsor of several FIRST teams.

As for your code, you’ve got all sorts of trouble with the use of the variable Count. First of all, Delphi (but not all versions of Pascal) will initialize any ordinal type variable (e.g. integer, etc.) to 0 when the variable is created. Furthermore, all versions of Pascal (Delphi included) will automatically set the for loop variable to the lower bound (in your case 1), when the loop begins. So you don’t have to specify Count := 1. Also, you don’t need Count := Count + 1 because it will automatically increment the for loop variable for each iteration of the loop. (I don’t remember what will happen if you do what you’re doing. If I had to guess, I think that it will allow it, but it might confuse the loop, and maybe cause it to loop infinitely, or terminate prematurely.) The bottom line is, don’t ever try to change the for loop variable during the loop.

Now that I look again, I realize that there’s a problem with the loop itself. It’s not what you mean that counts, it’s what you actually write… You’ve declared it as:

``````
for Count := 1 to 20 do;
Number := InputBox('Enter number', 'Number', ''); //create InputBox
Count := Count + 1;
Sum := Sum + StrToInt(Number);
lblIterationNumber.Caption := 'Iteration Number:' + Number;
lblSum.Caption := 'Cumulative Sum:' + IntToStr(Sum);
end;

``````

In fact, this won’t do anything substantial, because of the semicolon after the loop’s do keyword. (This means, “for Count equals 1 to 20, do nothing, then execute the rest of the statements once only”.) Furthermore, you’ve got a begin keyword missing (to match the end). You likely mean:

``````
for Count := 1 to 20 do
begin
Number := InputBox('Enter number', 'Number', ''); //create InputBox
Inc(Sum, StrToInt(Number));
lblIterationNumber.Caption := 'Iteration Number: ' + Number;
lblSum.Caption := 'Cumulative Sum: ' + IntToStr(Sum);
end;

``````

Incidentally, you can write Inc(Sum, StrToInt(Number)) instead of Sum := Sum + StrToInt(Number), if you’d like. It does the same thing, but may be more readable, depending on your own preference.

From a more basic point of view, your specification calls for a set of integer numbers, with the output being the sum of those numbers. Depending on the version of Delphi that you’re using (because the 64-bit version is possibly different), an integer is a 32-bit signed variable, meaning that it has possible values between 2 147 483 647 and -2 147 483 648. Intuitively, you should know that 20 × 2 147 483 647 > 2 147 483 647, so an integer variable can’t necessarily hold the sum of 20 integers, without overflowing. Unless this situation is impossible (e.g. if it was explicitly stated in the problem to neglect this), you must account for it by using a larger type to store the sum. If you ever write a programming contest, you’ll quickly learn that this is a common way to trip up unsuspecting competitors. (I suspect that declaring Sum as an Int64 will clear up matters, but you need to consult the help file about using Int64—it isn’t quite a normal ordinal variable. Also, you may need Int64ToStr(Sum), though I’m not positive on that.)

Edit: Two things. Now that I’m home, I checked the Delphi help file, and found that IntToStr is overloaded for Int64 input—meaning that a different version of the function (with the same name) is called if you pass it Int64 rather than integer data. This lets it handle the two different data structures (integer and Int64) in a convenient way. In other words, use IntToStr(Sum)[font=Verdana], and[/font] *don’t *use Int64ToStr(Sum), which doesn’t exist.

I also just realized something else about Int64: be careful adding integer quantities and assigning them to Int64 variables; you’re OK here, because Sum is Int64 to begin with, but if you were to add the following, you’d get bad results:

``````
Sum := 2000000000 + 147483648 //i.e. bigger than Integer, but OK for an Int64
//Returns an error, or bad data, depending on the version of Pascal, because the (implicitly) integer quantity on the RHS of the equation overflows.

``````

In fact, you need to typecast one as an Int64, so that the two integer quantities aren’t treated as integers (which leads to an overflow). Use Int64 as a function name, like this:

``````
Sum := Int64(2000000000) + 147483648 //i.e. bigger than Integer, but OK for an Int64
//Returns 2147483648 correctly.

``````

Just things to watch out for…

It would be EXTREMELY helpful if you could describe the nature of the problem she is having. I.e. What is it doing or not doing that is unexpected? Is the compile failing, or is it failing at run time? Is there an error message?

But offhand, I can see a couple problems: She need a BEGIN following the DO (with no “;” there.) This SHOULD cause a compile error, since you have an unmatched END. And she probably doesn’t want the “Count := Count + 1;”, since the FOR…DO automatically increments Count.