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 Wed 26 Nov 2014, 04:59
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 Posts_count  
Author Message
8-bit


Joined: 03 Apr 2007
Posts: 3393
Location: Oregon

PostPosted: Sun 07 Oct 2012, 18:46    Post_subject:  How to convert an Atari ST C file for Linux?
Sub_title: 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: 6690
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: 3393
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:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0639s ][ Queries: 12 (0.0182s) ][ GZIP on ]