Advertisement

an array YAY!!

Started by October 25, 2000 07:54 PM
3 comments, last by slightlywhacked 24 years, 2 months ago
um.... im doing a program in DOS.. I know...DOS IS DEAD!! but o well, im programming direct-x most of the time, but a friend of mine wants a small program, that i dont know how to do as a windows program... anywasy to the problem I want to set up an 2 d array that holds sentances and nnumbers.. for exsample lets say i want the adress 1 in the first array to equal cows go moo and in option[1][1] i want the value "hey whats up" and in option[1] [2] i want the value "124" ?? how do i make the array to accept multiple words?? thanx Ryan Lahay
BAHcows go moomoos go cow
make an array of arrays of strings (really an array of arrays of arrays of char)
like this:

[source]
char ***option;
[\source]
Advertisement
..hmmm.. dont exactly no what u mean.. this is how i need it

for (int index = 1; index < number_of_options; index++)
{
cout << "Please enter option" << index <<''\n'';
cin.get (???????);
}

cout << options[2];

i want it to be a 50 chracter arrra... so it can accept 50 characters.. (duh) but i want it so it can be written easily in a loop, for exsample options[1] can have heave a nice day and options[2] can read goodbye ..things like that
BAHcows go moomoos go cow
Well a 50 character array would actually only hold 49 characters, the last one needs to be NULL (\0) .

Try this:

    #include <stdio.h>#include <conio.h>#include <string.h>//...#define SOMENUMBER 10char option[SOMENUMBER][51];//...// Or do this dynamicly like this:#define SOMENUMBER 10char **option = new char*[SOMENUMBER]// Then use this to read in a string to option[0], or something like that:char *getString(void) {  char *data = new char[0];  strcpy(data,"");  char *buff;  char chunk[11] = "";  unsigned int put = 0;  int ch = 0;  bool cont = true;  for(unsigned int a=1; cont; a++) {    ch=getch();    if(ch==13) {      cont = false;    }    chunk[put] = (unsigned int) ch;    printf("%c",chunk[put]);    put++;    if(put>=10 || cont) {      unsigned int len = a+strlen(chunk);      buff = new char[len];      sprintf(buff,"%s",data);      delete [] data;      len += strlen(chunk);      data = new char[len];      sprintf(data,"%s%s",buff,chunk);      strcpy(chunk,"");      delete [] buff;      put = 0;    }  }  return data;}// That can be used like this:option[0] = getString();    


I''m kind of sleepy, please excuse errors...


Null and Void
At least I don't know COBOL...
http://www.crosswinds.net/~druidgames/
It always butchers my code!

Null and Void
At least I don't know COBOL...
http://www.crosswinds.net/~druidgames/

This topic is closed to new replies.

Advertisement