Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » array of string problem

Credits go to Fladimir da Gorf for helping out!
This thread is locked; no one can reply to it. rss feed Print
array of string problem
chin jiunn gai
Member #6,753
January 2006

i use c language.
i have one array of string and one char variable and i want to compare they value is same or not ,so wat should i do .
math_q1 is the question arrays.
The math_a1 store the answers of the question example 2 + 3 =
the multiple choice is 1 : 7, 2 : 6, 3 : 5 the 4 th row store the right answers position.

get the key input 1 or 2 or 3 from user
compare the value key in with the 4 th row of array;
char *math_q1 [5] = {"2 + 3 = ","6 + 7 = ","2 + 8 = ","7 + 10 = ","9 + 12 = "}
char *math_a1 [4][5] = {
{"7","15","8","17","21"},
{"6","14","10","15","22"},
{"5","13","13","13","23"},
{"3","3","2","1","1"}
}
char answers = ureadkey(NULL);
if(answers == math_a1[4][1])
{......}
but problem is answers is char and math_al is array of string
i am doing a quiz game ,is this good way to do this ?

Fladimir da Gorf
Member #1,565
October 2001
avatar

I'd store integers in the table and use atoi to convert the user input to an interger.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Pedro Avelar Gontijo
Member #5,372
January 2005
avatar

I really didn't understand what you want to do... but in any case Fladimir's tip should do it :)
BTW:

Fladimir da Gorf's sig said:

The final testing of OpenLayer 2.0 is underway! (Yet another Framebuffer issue solved)

Considering you're changing your sig very instensely, I guess OpenLayer 2.0 is just getting the last hits on the anvil? :)

----------
兩隻老虎,兩隻老虎,跑得快!

chin jiunn gai
Member #6,753
January 2006

my question not only integer but string ,example which is the longest rivers
1 :xx
2 :xx
3 :xx

Fladimir da Gorf
Member #1,565
October 2001
avatar

Then use strcmp instead of the == operator to test the equality of strings. If you want to get really fancy you could also ignore case and strip the trailing and leading spaces from the input etc.

Quote:

Considering you're changing your sig very instensely, I guess OpenLayer 2.0 is just getting the last hits on the anvil?

Maybe ;) I'll just have to solve a color issue with the framebuffers (probably caused by the way that AllegroGL uploads the textures) and a few other things.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

chin jiunn gai
Member #6,753
January 2006

so how what code i should write to convert my input to string ? i am use the allegro for the input .and my input either 1,2 or 3 only
or u comment me step how to do quiz game ?

Fladimir da Gorf
Member #1,565
October 2001
avatar

The best way would be to use a real GUI. But if you want to do it by yourself here's a quick article about it.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

chin jiunn gai
Member #6,753
January 2006

that article just for text input .
i can get input from user and i am using gui , i just dont write the code inside here .i just show u the problem part .
as u say use strcmp but the input is char type
so how to convert the answers to string
sorry for this ,i really newbiz in programming .

and i want to ask is this the way to do the quiz game .

store the question and answers in array then compare the user input with store answers ... is that something i omit

Fladimir da Gorf
Member #1,565
October 2001
avatar

You're reading only one key, not a string. But as you have predefined choises to choose from, that's enough. To convert a char to a string you could use the age old trick:

char *answer[2];
answer[0] = ureadkey(NULL);
answer[1] = '\0';

// Or if you want you could also use sprintf:
sprintf( answer, "%c", ureadkey( NULL ));

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Go to: