2 Replies - 740 Views - Last Post: 13 February 2010 - 07:45 PM Rate Topic: -----

#1 woppix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 127
  • Joined: 13-September 09

if statement issues

Posted 13 February 2010 - 05:08 PM

This program i am working on is a fairly simple summation program. the for loop starts at the number stored in num1, and stops at num2.. all the numbers between those two are added together. If there is a number in the txtStep textbox, it is a increment value for the numbers that will be added. You can probally tell the rest from the code but if needed i'll explain more.

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();
					 }
				 
			 }



Is This A Good Question/Topic? 0
  • +

Replies To: if statement issues

#2 seeP+   User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: if statement issues

Posted 13 February 2010 - 06:24 PM

Using the values that you just stated will not have the for loop execute.
 for (int i = num1; i <= num2; i+=num3)

i is 8 and num2 is 5.
Was This Post Helpful? 0
  • +
  • -

#3 woppix   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 127
  • Joined: 13-September 09

Re: if statement issues

Posted 13 February 2010 - 07:45 PM

ok so i went and modified my code.. i didn't see how to make it work with 1 for loop so i made two and it seems to work fine like this. I was wondering though if there was a better way, or should i just stick with it like this.

	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
					 {
						 if (num1 > num2)
						 {
							 temp = num1;
							 num1 = num2;
							 num2 = temp;
						 
							 for (int i = num1; i <= num2; i-=num3)
							 {
								 sum = sum + i;
							 }
						 }
						 else if (num1 < num2)
						 {
							 for (int i = num1; i <= num2; i+=num3)
							 {
								 sum = sum + i;
							 }
						 }

						 
					 txtSum->Text = sum.ToString();
					 }
				 
			 }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1