here is the logic that its suppose to be doing:
there are pretty much 3 text boxes... first box sets the starting point of the for loop and that number is put int num1.
second box is the ending point of the for loop and the number is stored in num2.
the third textbox stores the number in which the variable i will be incremented by.
if statement logic:
if num2 > num1 then num3 cannot be negative
if num2 < num1 then num3 cannot be positive
the problem is that if i enter the following
num1 = 8, num2 = 5, num3 = -1... the result will be zero, instead of 26 like i need it to be. In this case if num3 is any negative number i think it will get the same result. The rest of it seems to work fine.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int num1 = 0, num2 = 0, sum = 0, temp = 0, num3 = 0;
Int32::TryParse(txtFirst->Text, num1);
Int32::TryParse(txtLast->Text, num2);
Int32::TryParse(txtStep->Text, num3);
if (((num2 > num1) && (num3 < 0)) || ((num1 > num2) && (num3 > 0)))
{
txtSum->Text = "Not Allowed";
}
else
{
for (int i = num1; i <= num2; i+=num3)
{
sum = sum + i;
}
txtSum->Text = sum.ToString();
}
}

New Topic/Question
Reply



MultiQuote



|