Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

my code dosent do anything

timer c++

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 JonElias

JonElias

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 201 posts

Posted 09 March 2015 - 07:11 AM

long time no see codecall :)

 

so i am trying to make an timer AI. it is a infinate loop, and when the user hits 0 on the keyboard, 1 will be added to the object corresponding to the secound of a minute the user hit the button. So my plan was to add a part where the program guesses  when you will hit the button.

 

here is my code.

#include <iostream>

// code i copied to have a timer function
#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h>   // for nanosleep
#else
#include <unistd.h> // for usleep
#endif

using namespace std;

float chance[60]; // chance list
int totalobj = 0; // total times input have been hin
int inp = 0; // used for simple input
int inmincount = 0; // secounds in 60 format


void sleep_ms(int milliseconds) // this is the timer function, still copied
{
#ifdef WIN32
    Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
    struct timespec ts;
    ts.tv_sec = 0;
    ts.tv_nsec = milliseconds * 1000000;
    nanosleep(&ts, NULL);
#else
    usleep(milliseconds * 1000);
#endif
}


int main(){
	cout << "hello" << endl;
	while (1){
		sleep_ms(1000);
		
		inmincount += 1;
		if (inmincount == 60){ // to maintain 60 format
			inmincount = 1;
		}
		
		cin >> inp;
		cin.clear();
		
		if (inp == 0){ // if input
			totalobj += 1;
			chance[inmincount] += 1; // add one to the chance array
		}
		
	}

	
	return 0;
}

i dont get any output, even if i have an empty main loop with just "cout << "test" << endl; ". the program just quits itself


"computers are useless they only give you answers" Pablo Picasso
And yes he was alive when the first programmable computer was made

#2 JonElias

JonElias

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 201 posts

Posted 09 March 2015 - 07:46 AM

ups i forgot that the compiler line dosent give a output. but the program it makes, sorry.

 

but anyway, cin >> inp; (line 45) waits for input . and i want input if it is typed, in other words without \n


Edited by JonElias, 09 March 2015 - 07:50 AM.

"computers are useless they only give you answers" Pablo Picasso
And yes he was alive when the first programmable computer was made

#3 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 09 March 2015 - 07:54 PM

ups i forgot that the compiler line dosent give a output. but the program it makes, sorry.

 

but anyway, cin >> inp; (line 45) waits for input . and i want input if it is typed, in other words without \n

 

How you do this depends on what operating system you're using. There's no one way to do it that'll work across all operating systems unless you use a library like ncurses. For a program this simple, I think ncurses is overkill. What OS are you using?


sudo rm -rf / && echo $'Sanitize your inputs!'


#4 JonElias

JonElias

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 201 posts

Posted 09 March 2015 - 10:45 PM

Windows 64-bit

Edited by JonElias, 09 March 2015 - 10:45 PM.

"computers are useless they only give you answers" Pablo Picasso
And yes he was alive when the first programmable computer was made

#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 10 March 2015 - 03:14 PM

This should work: http://stackoverflow.com/a/421871


sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download