- Online Community Forums » Programming Questions » Newbie Question--Inputing Data

This thread is locked; no one can reply to it. rss feed Print
Newbie Question--Inputing Data
Matt Ward
Member #8,046
November 2006

Hi there,
I'm new to these forums and Allegro itself.
I am creating a program in which I am using Allegro to graph basic math functions. I am accomplishing this by a simple loop of putpixel()'s in which I step the x value up each loop. My y value for putpixel() is obtained through the formula y=(a+(x*b)+(x*x*c)+(x*x*x*d)+(x*x*x*x*e)) where a, b, c, d, and e are doubles. I would really like to allow the user to enter values for these doubles so that they can choose the shape of the graph, but I can't seem to figure out how to that. I know the C++ reserved word cin does not work in Allegro. Is there something similar that I could use in Allegro? I have looked through a bunch of tutorials and FAQ's and I haven't found anything. Could someone either show me what command I should use or point me in the right direction.

Member #7,263
May 2006

You'd have to use either key[KEY_whatever] or readkey() (most likely readkey) to obtain user input key by key then take that data and use it for something. Sorry I can't help more than that; I haven't actually done this myself, just how to do it.:'(

Matt Ward
Member #8,046
November 2006

Thanks for your help spork.
Looking at both key[] and readkey(), it appears they both only accept input from one key. If I wanted to type a number like 1.34 (or any other double), wouldn't it just accept the first key pressed? Is there a way to tie multiple "pressings"? (excuse my non-C++ jargon :-[)

Another thought, in my college C++ class, we learned to import data from other programs using #include <fstream>. Although that doesn't work, is there away for Allegro to access data from other programs?

Thanks Again!

Member #5,313
December 2004


If I wanted to type a number like 1.34 (or any other double), wouldn't it just accept the first key pressed? Is there a way to tie multiple "pressings"?

Read the input key by key and save it in a buffer. Then when the user has hit ENTER (or whtever you want to use to terminate input), convert the contents of the buffer to a numeric format.

Member #2,407
June 2002

Or use the Allegro GUI routines, most notably d_edit_proc()

sig used to be here

Member #7,733
September 2006

Matt Ward said:

...import data from other programs using #include <fstream>. Although that doesn't work...

What's not working with fstream? Using it in your code concurrently with Allegro, or fstream "doesn't work at all"?

1#include <iostream>
2#include <fstream>
4using namespace std;
6int main(void)
8 int input;
9 int result;
11 // ifstream means input from file
12 ifstream fin;
13 // ostream means output to file
14 ofstream fout;
19 // Grab data until the end of file (EOF) is reached
20 fin >> input;
21 while (!fin.eof())
22 {
23 if (input < 100)
24 result = input + 100;
25 else
26 result = input;
28 fout << result;
29 fin >> input;
30 }
31 fin.close();
32 fout.close();
34 return 0;


Member #8,013
November 2006

i had a couple of functions that did exactly what your talking about....

...but i just spent 15 minutes looking through my dev files and i think it may be deleted.(check,oh gods, check before delete!)...

someone here should know it, since i got it from the code gallery, although the link seems to be broken...

..there was a couple of lines of hexidecimal magic, to take the actual ascii value of the keypress and put it into a string, inside a loop that went till enter.

...try the code gallery, theres only 3 files should recognize the one...:)

never play leapfrog with a unicorn....

Three Harris
Member #6,226
September 2005

I am not sure but I think this is the snipit you want.

1#include <allegro.h>
3#define BUFFERSIZE 128
4#define WHITE makecol(255, 255, 255)
5#define BLACK makecol(0,0,0)
7int main()
9 /* typical Allegro initialization */
10 allegro_init();
11 install_keyboard();
12 set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
14 char edittext[BUFFERSIZE];
15 int caret = 0;
16 BITMAP *buffer = create_bitmap(200,20);
17 //clean out string
18 int i;
19 for(i=0;i<BUFFERSIZE;i++)
20 edittext<i> = '\0';
21 //text loop
22 do {
23 if(keypressed()) {
24 int newkey = readkey();
25 char ASCII = newkey & 0xff;
26 char scancode = newkey >> 8;
27 /* a character key was pressed; add it to the string */
28 if(ASCII >= 32 && ASCII <= 126) {
29 if(caret < BUFFERSIZE - 1) {
30 edittext[caret] = ASCII;
31 caret++;
32 edittext[caret] = '\0';
33 }
34 }
35 else if(scancode == KEY_BACKSPACE) {
36 if (caret > 0) caret--;
37 edittext[caret] = '\0';
38 }
39 }
40 /* all drawing goes here */
41 clear_to_color(buffer,WHITE);
42 textout_ex(buffer,font,edittext,5,(buffer->h / 2) - 4,BLACK,-1);
43 vline(buffer,(caret*8) + 6,(buffer->h / 2) - 6,(buffer->h / 2) + 5,BLACK);
44 vline(buffer,(caret*8) + 7,(buffer->h / 2) - 6,(buffer->h / 2) + 5,BLACK);
45 blit(buffer,screen,0,0,25,15,300,200);
46 } while(!key[KEY_ENTER]);
48 destroy_bitmap(buffer);
49 allegro_exit();
50 return 0;

Neil Walker
Member #210
April 2000

Why code when the allegro wiki is there for you :)

there's a c++ version as well.

MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Three Harris
Member #6,226
September 2005


Why code

That is basicly identical code.

[edit] But I didn't have that link so thanks.

Go to: