How to convert an Atari ST C file for Linux?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

How to convert an Atari ST C file for Linux?

#1 Post by 8-bit »

I found a small guess the number game written in C and am trying to compile it for linux/Puppy.
It was from a Analog magazine in a series called "Learning to C".

I feel like I am almost there, but evidently, the included osbind.h is not part of the linux includes.
I need a function that returns a random number and that is where I am stuck.

The code for the C file, guess_number.c, is below.
And this is my first attempt at any C compiling so if I appear dumb in this it is because I am.

Code: Select all

#include <stdio.h>
#include <osbind.h>
#define TRUE 1
#define FALSE 0

main()
{
	int num, guess, win, turns, play;
	
	play = TRUE;
	while (play) {
		turns=0; win = FALSE;
		num = get_num();
		while (!win) {
			++turns;
			guess = get_guess();
			win = check_guess(num, guess);
		}
		printf("It took you %d turns.\n\n");
		play = play_again();
	}
}

int get_num()
{
	int n;
	
	n = (int) random();
	n = abs(n) % 99 + 1;
	return(n);
}

int get_guess()
{
	int g;
	
	g = 0;
	while (g<1 || g>100) {
		printf("Enter a number from 1 to 100: ");
		scanf("%d", &g);
		printf("\n\n");
	}
	return(g);
}

int check_guess(num, guess)
int num, guess;
{
	int wn=FALSE;
	
	if (guess < num)
		printf("Too low\n\n");
	else if (guess > num)
		printf("Too high\n\n");
	else {
		printf("You guessed it!\n");
		wn = TRUE;
		}
	return(wn);
}

int play_again()
{
	int ch, p;
	
	p = -1;
	ch = getchar();
	while ( (p!=TRUE) && (p!=FALSE) ) {
		printf("Play again? ");
		if ( (ch=getchar()) == 'y' || ch == 'Y')
			p = TRUE;
		else if (ch ==  'n' || ch == 'N')
			p = FALSE;
	}
	printf("\n\n");
	return(p);
}


muggins
Posts: 6724
Joined: Fri 20 Jan 2006, 10:44
Location: hobart

#2 Post by muggins »

It will compile if you just comment out the #include <osbind.h> line.

Code: Select all

#include <stdio.h>
//#include <osbind.h>
#define TRUE 1
#define FALSE 0
I just saved the file as test.c, and compiled with:

Code: Select all

gcc test.c -o test
Edit It wasn't outputting the proper number of turns as the printf was missing the turns variable.

Code: Select all

printf("It took you %d turns.\n\n", turns);

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#3 Post by 8-bit »

Thank you muggins! :)
Now that I have had success in trying that piece of code from the article "Learning to C", I am encouraged to try some more along with reading the text involved and maybe learn something along the way.

This is a Caution!
If anyone compiles the C source code, do not try to run it just by clicking on the executable.
It is made to be run from a terminal and if just clicked on, it will be running in the background with no visible display and you will have to run "HTop to find it's program ID and kill it.

Post Reply