Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 01 Aug 2014, 18:53
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to convert an Atari ST C file for Linux?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [3 Posts]  
Author Message
8-bit


Joined: 03 Apr 2007
Posts: 3357
Location: Oregon

PostPosted: 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
View user's profile Send private message 
muggins

Joined: 20 Jan 2006
Posts: 6674
Location: lisbon

PostPosted: 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:

Code:
gcc test.c -o test


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
View user's profile Send private message 
8-bit


Joined: 03 Apr 2007
Posts: 3357
Location: Oregon

PostPosted: Sun 07 Oct 2012, 21:30    Post subject:  

Thank you muggins! Smile
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
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [3 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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
[ Time: 0.0486s ][ Queries: 11 (0.0035s) ][ GZIP on ]