The time now is Thu 23 May 2013, 00:35
All times are UTC - 4 |
| Author |
Message |
8-bit

Joined: 03 Apr 2007 Posts: 3012 Location: Oregon
|
Posted: Sun 07 Oct 2012, 18:46 Post subject:
How to convert an Atari ST C file for Linux? Subject description: Replacing osbind.h with an equvilent. |
|
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: |
#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);
}
|
|
|
Back to top
|
|
 |
muggins
Joined: 20 Jan 2006 Posts: 6660 Location: lisbon
|
Posted: Sun 07 Oct 2012, 19:25 Post subject:
|
|
It will compile if you just comment out the #include <osbind.h> line.
| Code: | #include <stdio.h>
//#include <osbind.h>
#define TRUE 1
#define FALSE 0 |
I just saved the file as test.c, and compiled with:
Edit It wasn't outputting the proper number of turns as the printf was missing the turns variable.
| Code: | | printf("It took you %d turns.\n\n", turns); |
|
|
Back to top
|
|
 |
8-bit

Joined: 03 Apr 2007 Posts: 3012 Location: Oregon
|
Posted: Sun 07 Oct 2012, 21:30 Post subject:
|
|
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.
|
|
Back to top
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|