Skip to content

Instantly share code, notes, and snippets.

@dionyziz
Created June 12, 2011 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dionyziz/1021669 to your computer and use it in GitHub Desktop.
Save dionyziz/1021669 to your computer and use it in GitHub Desktop.
A simple time machine in C++
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int main( int argc, char *argv[] ) {
int year;
int now;
char c;
FILE* fi;
FILE* fo;
if ( argc != 2 ) {
printf( "Usage: timemachine <year>\n" );
return 1;
}
year = atoi( argv[ 1 ] );
fi = fopen( "year.txt", "r" );
fscanf( fi, "%i", &now );
fclose( fi );
if ( year > now ) {
printf( "This time machine doesn't yet support going to the future. Sorry.\n" );
return 1;
}
else if ( year == now ) {
printf( "You are already in %i.\n", year );
return 1;
}
else if ( year < 1900 ) {
printf( "For security reasons, you cannot go back beyond 1900.\n" );
return 1;
}
printf( "Warning: This program did not yet exist in %i. You may not be able to travel back.\n", year );
printf( "Are you sure you want to continue? (Y/N) " );
scanf( "%c", &c );
if ( c != 'y' && c != 'Y' ) {
printf( "Operation aborted.\n" );
return 1;
}
printf( "Taking you back to year %i...\nThis may take a few moments.\n", year );
sleep( 5 ); // TODO
fo = fopen( "year.txt", "w" );
fprintf( fo, "%i\n", year );
fclose( fo );
printf( "You are now in year %i.\n", year );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment