Conditional If statement does not seem to be working as designed

has to do with GetTickCount()

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1785 Views - Last Post: 18 January 2009 - 01:10 PM Rate Topic: -----

#1 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 10:21 PM

The program runs but for some reason it ignores the if statement and goes above 200 ( i even tried changing it to 250 but still didn't work)
Compiler being used: Dev-C++

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
   int start_time = GetTickCount(); //i want this to happen once
   int end_time, total_time; 


   while(true)
   {
	 end_time = GetTickCount();
	 total_time = (end_time - start_time)/ 10;
	 
		   cout << 200 << endl
		   << total_time << endl; 
	 if(total_time == 200)	//for some reason, this if statement gets ignored?
	 {
	   cout << "Perfect" <<endl;  
	   system("PAUSE");
	   return 0;  
	 }
	 
	 else if(total_time > 200)
	 {
				   
		 cout << "WHAT THE F-" << endl;
		 Sleep(10000);
		 return 0;
	 }
	 system("cls");
   }

   system("PAUSE");
   return 0;

}
 

This post has been edited by Pat234: 17 January 2009 - 10:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Conditional If statement does not seem to be working as designed

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 10:30 PM

Try the following, & see what it's seeing.

total_time=0;
while(total_time < 200) {
     end_time = GetTickCount();
     total_time = (end_time - start_time)/ 10;
     
     cout << total_time << endl
     if(total_time == 200) {
     ...
     }
     
     else if(total_time > 200) {
     ...
     }
}



** Renamed title to be more descriptive of topic **
Was This Post Helpful? 0
  • +
  • -

#3 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 10:43 PM

View Postno2pencil, on 17 Jan, 2009 - 09:30 PM, said:

Try the following, & see what it's seeing.


i don't get it? :blink:
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 10:49 PM

View PostPat234, on 17 Jan, 2009 - 11:43 PM, said:

View Postno2pencil, on 17 Jan, 2009 - 09:30 PM, said:

Try the following, & see what it's seeing.


i don't get it? :blink:

Looking at your code you don't see why the conditional statements are not reacting the way you think they should.

So output the value & see what the conditional statements are actually comparing. Check the values visually & make the correct change to the code.
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 10:53 PM

It worked for me, the way I thought it should, according to what I see in the code.
Was This Post Helpful? 0
  • +
  • -

#6 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:06 PM

View Postno2pencil, on 17 Jan, 2009 - 09:49 PM, said:

View PostPat234, on 17 Jan, 2009 - 11:43 PM, said:

View Postno2pencil, on 17 Jan, 2009 - 09:30 PM, said:

Try the following, & see what it's seeing.


i don't get it? :blink:

Looking at your code you don't see why the conditional statements are not reacting the way you think they should.

So output the value & see what the conditional statements are actually comparing. Check the values visually & make the correct change to the code.

i did by using
cout << 200 << endl
		   << total_time << endl; 

but i still don't understand why its doing that

View PostOliveOyl3471, on 17 Jan, 2009 - 09:53 PM, said:

It worked for me, the way I thought it should, according to what I see in the code.

yeah, i read the code now and it looks like it makes sense, but when i compile & run it, it does not go as planned
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:09 PM

Try the following :

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
   int start_time = GetTickCount(); //i want this to happen once
   int end_time, total_time;


   while(true)
   {
     end_time = GetTickCount();
     total_time = (end_time - start_time)/ 10;
     
           cout << 200 << endl
           << total_time << endl;
  
     if(total_time > 200)
     {
                   
         cout << "WHAT THE F-" << endl;
         Sleep(10000);
         return 0;
     }
     if(total_time == 200)    //for some reason, this if statement gets ignored?
     {
       cout << "Perfect" <<endl;  
       system("PAUSE");
       return 0;  
     }
     system("cls");
   }

   system("PAUSE");
   return 0;

}


I think it's in your else statement.
Was This Post Helpful? 0
  • +
  • -

#8 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:16 PM

i tried that as well with no sucess :(
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:18 PM

So what values are being displayed when you run this?
Was This Post Helpful? 0
  • +
  • -

#10 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:29 PM

View PostPat234, on 18 Jan, 2009 - 12:06 AM, said:

yeah, i read the code now and it looks like it makes sense, but when i compile & run it, it does not go as planned

So, what do you expect it to output?

This post has been edited by OliveOyl3471: 17 January 2009 - 11:29 PM

Was This Post Helpful? 0
  • +
  • -

#11 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:29 PM

it will count up to 201, then my 2nd if statement occurs printing text and pausing the screen showing:
200
201
Was This Post Helpful? 0
  • +
  • -

#12 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:32 PM

View Postno2pencil, on 18 Jan, 2009 - 12:09 AM, said:

I think it's in your else statement.

That's what I thought, too. The else will never run.

View PostPat234, on 18 Jan, 2009 - 12:29 AM, said:

it will count up to 201, then my 2nd if statement occurs printing text and pausing the screen showing:
200
201

That's funny, for me it outputs the 200 on top like yours, but counts up to 200 and stops, for the 2nd line and outputs Perfect on the third line.

This post has been edited by OliveOyl3471: 17 January 2009 - 11:33 PM

Was This Post Helpful? 0
  • +
  • -

#13 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:32 PM

The code solution that I provided works perfectly for me, 100% of the time. I've ran it like 30 times & not had it pass 200 once.

Quote

198
198
200
Perfect

Was This Post Helpful? 0
  • +
  • -

#14 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Conditional If statement does not seem to be working as designed

Posted 17 January 2009 - 11:39 PM

i'll check it out try it again 2morrow
NEEEEED SLEEEEEP
and thanks for the help, cant for 2morrow
Was This Post Helpful? 0
  • +
  • -

#15 Pat234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 02-November 08

Re: Conditional If statement does not seem to be working as designed

Posted 18 January 2009 - 11:24 AM

okay so i ran...
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
   int start_time = GetTickCount(); //i want this to happen once
   int end_time, total_time;


   while(true)
   {
	 end_time = GetTickCount();
	 total_time = (end_time - start_time)/ 10;
	 
		   cout << 200 << endl
		   << total_time << endl;
  
	 if(total_time > 200)
	 {
				   
		 cout << "WHAT THE F-" << endl;
		 Sleep(10000);
		 return 0;
	 }
	 if(total_time == 200)	//for some reason, this if statement gets ignored?
	 {
	   cout << "Perfect" <<endl;  
	   system("PAUSE");
	   return 0;  
	 }
	 system("cls");
   }

   system("PAUSE");
   return 0;

}
 


and it still is not working for some odd reason
it still prints out :
200
201
WHAT THE F-

could it be vista? lol, im out of ideas
i'll have to test this on my friend's laptop then
Glad to see it works, even tho its working on other people's computers and not mine

This post has been edited by Pat234: 18 January 2009 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2