6 Replies - 772 Views - Last Post: 08 November 2016 - 01:08 PM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

When Using Timer in Visual C++, Result Ignores Increment

Posted 29 October 2016 - 05:16 PM

Hey all, I'm currently coding a visual C++ program and this whole problem is centering around a way to make text fluidly change colors. I'm trying to use a string to determine which color the text is displayed in (which is probably the wrong way to do it) and I keep running into a problem when I try to use VC++ ... when I compile as a console application I'm not having this problem. The problem is that whenever I try to use a loop (for) or use the timer the output always goes straight to the last (if) statement. I don't understand why this is because I thought that both were incremental and not necessarily sequential. I'm sorry if this seems vague but here's what I'm working with:

	private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
		 {
			 int a;
			 a + 1;
			 string increasing;
			 if (a = 1)
			 { increasing = "Timer = 1";
			 }
			 if ( a = 2 )
			 {
			 increasing = "Timer = 2";
			 }
			 if ( a = 3 )
			 {
			increasing = "Timer = 3";
			 }
			 if ( a = 4 )
			 {
			 increasing = "Timer = 4";
			 }

			 textBox1->Text = gcnew String(increasing.c_str());

		}


So I'm using a button to change the timer enabled state to true... that part works fine as it's incredibly easy to code and also straightforward, but when I compile and push the button the textbox immediatly displays "Timer = 4". The timer is set to 1000ms increments so since it only adds 1 to a every pass I don't understand why it's going straight to a=4 .. can anyone help me with the logic? Thanks

also, is there any way to edit a post?

This post has been edited by Skydiver: 29 October 2016 - 06:53 PM
Reason for edit:: Fixed code tags. Thanks for trying. You'll get edit right eventually.


Is This A Good Question/Topic? 0
  • +

Replies To: When Using Timer in Visual C++, Result Ignores Increment

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 19,817
  • Joined: 05-May 12

Re: When Using Timer in Visual C++, Result Ignores Increment

Posted 29 October 2016 - 06:57 PM

Moving to C++/CLI forum since this is not standard C++.

If you look closely, the compiler would have been warning that you were assigning the values using the = operator instead of doing comparisons using the == operator on lines 6, 9, 13, and 17.

Also your line 4 is effectively doing nothing since you are not storing the result of the addition anywhere.
Was This Post Helpful? 1
  • +
  • -

#3 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: When Using Timer in Visual C++, Result Ignores Increment

Posted 29 October 2016 - 07:38 PM

Oh the compiler was definitely warning me... i just had no idea what the fuck that means lol.... but thanks!! ill go from here and appreciate you editing it, i forgot to wrap the tag
Was This Post Helpful? 0
  • +
  • -

#4 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: When Using Timer in Visual C++, Result Ignores Increment

Posted 30 October 2016 - 09:04 AM

Hey Skydiver, thanks again! I see where I messed up there. I have another question:

So I put a second integer (B)/>/>/>/> into my timer to address the logical issue that you pointed out to me, but now my problem is that I don't know where to declare the integer because if I declare it inside the timer it just writes to 0 every time which defeats the purpose of the second statement. If I try and declare the integer anywhere else (probably because it's a forms application) it says that a is undefined. I tried making the button's click event that turns the timer on declare the integer but it also says undefined.

				int a;
				int b;

				a = 0;
					
				b = a + 1;
				a = b;
				string increasing;
				if ( b == 1 )
				{
					increasing = " Timer = 1 ";
				}
				if ( b == 2 )
				{
					increasing = " Timer = 2 ";
				}
				      textBox1->Text = gcnew String(increasing.c_str());
			 
			 
			 


Do you know where I can assign a = 0; or if I should be using a conditional to ask if the integer is already assigned? I'm not sure how to check if an integer is assigned. I know how to say: is a == 1 but I don't know how to say is 'a' not yet assigned to a value. If I did I could probably make a bypass for the assignment if it's already set to a value.

Also, sorry to be needy, I tried to delete my post due to the profanity but couldn't find a delete button.

I really appreciate your help sir! Have a great day.
Was This Post Helpful? 0
  • +
  • -

#5 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: When Using Timer in Visual C++, Result Ignores Increment

Posted 30 October 2016 - 10:06 AM

So I'm not sure if this is right but I added this line:

public ref class Form1 : public System::Windows::Forms::Form
	{
		 
		static int a = 0;

}




There's more obviously but I cut the rest this is just to show at what point I declared and assigned a. Seems to be working now. Thanks again Skydiver, I'm going to see how far I can get. Sorry this is such a n00b question I'm just getting started programming, appreciate the patience :)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 19,817
  • Joined: 05-May 12

Re: When Using Timer in Visual C++, Result Ignores Increment

Posted 01 November 2016 - 05:54 AM

When you use static, that variable is shared by all instances of your class. So if you create two Form1's, they will be stomping on each others value of a. Think of it as a private global variable. In general, you should avoid global variables, so don't use static.
Was This Post Helpful? 1
  • +
  • -

#7 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: When Using Timer in Visual C++, Result Ignores Increment

Posted 08 November 2016 - 01:08 PM

Excellent, thanks Skydiver!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1