Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with inputting multidimensional string arrays....
#21
Good work, it looks as though you're starting to get the hang of pointers too. They do take a fair amount of practice before they really make sense, try writing small programs to examine how pointers, arrays and variables all work together.

The only thing I would suggest changing in your code is the system("cls"); line. C by itself doesnt have a fine grain of control over the console (because as far as C is concerned, standard in could be a magnetic tape and standard out could be a printer). Have a look for a console handling library, ncurses is the Unix one (which I believe has been ported to Windows) and I think Na_th_an may have mentioned one.

Its really odd that your compiler didn't run the ansi test, did it not compile, not run or just tell you that your compiler isn't ansi complaint. Check through the options of your compiler to see if it has a strict ansi mode (in which case you will need to change your comments from // to /* */ style).
esus saves.... Passes to Moses, shoots, he scores!
Reply
#22
when I pasted the code into a new c source file it gave me the error:
Quote:testansi.cpp(11) : fatal error C1010: unexpected end of file while looking for precompiled header directive
I then looked up strict ansi in the search msdn field and it gave me
Quote:For code that is most universal among C compilers, write strictly ANSI C and ANSI C++ code.

To write strict ANSI C/C++ code

Do not use the MFC library. Call Win32 APIs directly.

Disable Microsoft extensions.

Use the iostream library from the ANSI Standard C++ library.

Use the Standard template library from the ANSI Standard C++ library.
I can follow the first two suggestions, but the library ones I wouldn't know where to start. So apparantly I'm not using strict ansi compliant code for some reason. Thanks for all the help. But I'm sure I'll need more in a couple hours Smile
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#23
on the pch error, if you're using msvc, you have to remove the stuff in project options about pre-compiled headers. Just select the don't use pch option and clear out any related paths/filenames. That should do it.
Reply
#24
I was reading up on some c++ features and I found something about how using new and delete is easier than malloc and free. Also I saw something about easier string creation like string s; or something. How does new and delete work?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#25
If you'd want a character array of 100 chars:
Code:
char * myptr;
myptr = new char[100];
/* do what you want with it here ... */
delete[] myptr; /* or you can do it without [] - makes no difference to most compilers */
Reply
#26
but with new you can't realloc()... Sad

And string is part of STL; it's sort of like QB strings. Just say #include <string>. Then you can do stuff like:
Code:
#include <string>
#include <iostream.h>
string s1;
string s2;
string s3;

void main(void){

s1 = "This is ";
s2 = "cool.";
s3 = s1 + s2;
cout << s3 << "\n"
return 0;
}
Reply
#27
Why, when I use this code:
Code:
for(i = 0;i < number; i++) {
        printf("\nName %d: ", i + 1);
       cin << tname;
        info[i]->name = new char[strlen(tname) + 1]
      strcpy(info[i]->name, tname);        
        while(kbhit());
        printf("AGE: ");
        scanf("%d", info[i]->age);
        while(kbhit());
    }

and when I type in a name like "Mike the Man" do I skip the age input and end up only with Mike in info[i]->name and some huge random negative number for age. Am I overlapping the memory allocation for info[i]->name? Another question, the ->name is a pointer to a char, so would I use new *char instead of new char?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#28
That's because you don't give a value for age, and C doesn't initialize variables. So it shows the random value that is in memory (only the gods know what was there before Wink ).

About the char stuff, no, you don't. You are giving the size [], so C interprets it as an array (which is, in fact, a pointer). If you did char *, you'd have an array of pointers to char.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#29
If you wanted to make a dynamic string inside of an array of a struct you would use this code (using C):

Code:
typedef struct {
    int   age = 0;
    char *name;
} entry;

info = (entry *) malloc(number * sizeof(entry))
/* then you would do some form of input to a dummy string and the strcopy the dummy to the *name. */

gets(tname, 99);
info[i]->name = (char *) malloc(sizeof(char[strlen(tname)+1]));
strcpy(info[i]->name, tname);
but in C++ you would use this code:
Code:
typedef struct {
     int age = 0;
     string name;
} entry;

entry *info;
info = new entry[number]
cin << info[i]->name;
both ways would work in C++, but the second one seems easier to understand and faster to write. Do they do exactly the same thing? Are there any disadvantages to either method? Is there an even easier or more efficient way of doing things?

Na_th_an:
my problem isn't that age has some crazy number, but that if I use spaces in the cin << info[i]->name; the inputting of age is completely skipped and the following inputs for names are filled with the next word after each space. So when I run it:

Quote:how many names: 4

name 1: Bananas are very good
AGE:
name 2: AGE:
name 3: AGE:
name 4: AGE: 23
it lets me input the last age
and the display of results looks like:

Quote:Here are the 4 people you entered:

1) bananas AGE: -230593
2) are AGE: -349023
3) very AGE: -990432
4) good AGE: 23
as you can see after using spaces in the input of the name, it messes everything up.
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#30
I can't help you with cin. As I told you, I don't like C++. I use C. It is better to use scanf to keep track of stuff.

But I think that cin parses the input and stops on spaces. Try using double quotes on your info: "Bananas are very good". I dunno if it'll work, but most likely.

The other question: Just the C method vs. the C++ method. Pick which you like more Wink
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)