Help with inputting multidimensional string arrays.... - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: General (http://qbasicnews.com/newforum/forum-6.html) +--- Forum: General/Misc (http://qbasicnews.com/newforum/forum-18.html) +---- Forum: General Programming (http://qbasicnews.com/newforum/forum-20.html) +---- Thread: Help with inputting multidimensional string arrays.... (/thread-1232.html) |
Help with inputting multidimensional string arrays.... - typosoft - 06-18-2003 Hi. I am very new to c/c++ and I need some help. Can someone show me how to make a program that has a mulidimensional array of strings so I can keep a list of names, and then input x ammount of names. So, I would have like a prompt that says "How many names do you wan't to enter: " and then you type in the names and then you go through a for loop statement and type all the names. Then it would show each name you typed in. Here's what I have so far (but I can't even get it to run because I don't know what I'm doing): Code: #include "stdafx.h" also, what is stdafx.h Help with inputting multidimensional string arrays.... - Ninkazu - 06-18-2003 just remember char arrays are "strings" so you have to have char variable[stringsNum][stringLength] Help with inputting multidimensional string arrays.... - typosoft - 06-18-2003 I don't understand what you are trying to tell me. Help with inputting multidimensional string arrays.... - Ninkazu - 06-18-2003 you char tname[100] is just one string of length 100. you make 10 strings of length 100, you would have to do this: char stringArray[10][100] Help with inputting multidimensional string arrays.... - LooseCaboose - 06-18-2003 Your almost there typosoft, just a few things wrong. First off, malloc is defined in stdlib.h, including it will remove errors about malloc returning the wrong type. You want the names array to be a two-dimensional dynamic array like names[number][100]. Because arrays must be declared to have a constant size you need to use dynamic arrays (pointers) instead. Your names declaration should look like: Code: char **names; After declaring the array and getting the value for number, you need to allocate space for your names array. First you need to allocate space for the first pointer: Code: names = malloc(number); Code: for(i = 0; i < number; i++) { Code: for(i = 0, j = 0; i < 10; i++, j++); Your name reading loop is almost correct again, except that you don't need the address (&) operator when passing tname to scanf and strcpy. Both scanf and strcpy are expecting a pointer to char, and although tname is defined as char tname[100], its first element is a pointer to the string. So change those two lines to: Code: scanf("%s", tname); For slightly more portable code you could replace: Code: while(!kbhit()) {} Code: while(!getchar()); HTH Help with inputting multidimensional string arrays.... - typosoft - 06-18-2003 Thanks for helping out, but I am still getting 2 errors: Quote:error C2440: '=' : cannot convert from 'void *' to 'char *' Quote:error C2065: 'getkey' : undeclared identifier I remember going to a place on the net that had a list of all C's commands and what library they are in, but I don't remember the url. Here's the code now that I fixed the parts you said to fix: Code: #include "stdafx.h" Help with inputting multidimensional string arrays.... - LooseCaboose - 06-18-2003 Your need two malloc statements, one for the pointer list for names and one (the loop) to malloc each of the strings. Your malloc block should look like: Code: names = malloc(number); Code: names = (char **)malloc(number); Quote:error C2065: 'getkey' : undeclared identifierWhoops, its getchar, not getkey. getchar is defined in stdio.h. It returns the next character read from stdin. Quote:I remember going to a place on the net that had a list of all C's commands and what library they are in, but I don't remember the url. Here's the code now that I fixed the parts you said to fix:Ive got the man and info pages installed (under Unix), so thats what I use, but doing a google search on "man command" should bring up the right info. The problem is that it is really only safe to do this for Standard C functions, you will need to check you specific compilers documentation for details on non-standard functions such as kbhit(). Help with inputting multidimensional string arrays.... - na_th_an - 06-18-2003 When you don't know where to find a concrete function, just google for it. You'll end in some .edu domain with complete help about it including, of course, which header file has to be included. You can also try MSDN at http://msdn.microsoft.com but beware: MSVC uses some functions differently. Help with inputting multidimensional string arrays.... - typosoft - 06-18-2003 My first program is coming along nicely now, thanks to the help, but I have four more questions: 1) how can I clear the screen? 2) is it better to use cout and cin rather than printf and scanf? 3) do I still have to free(names) using the way LooseCaboose malloced everything? 4) and why when I try to make a function do I get errors: wait for key undeclared identifier and redefinition: different type modifiers? here's the end part of the main() and the wait_for_key function (it is supposed to clear the keyboard buffer and then wait for a keypress): Code: wait_for_key(); Help with inputting multidimensional string arrays.... - na_th_an - 06-18-2003 1. It depends on the compiler and console mode. For example, in Borland console / MSDOS compiler it is "clrscr();" 2. I hate C++, so I hate cin and cout. I prefer keeping track of what I do. I'd use scanf and printf. 3. Of course you have to. 4. You have to either place your wait_for_key function before your main function (to make it visible to main) or use a prototype (like those DECLARE SUBs)... A prototype is just locating this at the beginning or in a .H file: Code: void wait_for_key(); I am too lazy and what I do is place all the functions before the main. Only your export functions have to be in the .H, so it is the best sollution for me (I hate prototypes and typing more than inteneded ). |