A little help here

This is a discussion on A little help here within the C++ Programming forums, part of the General Programming Boards category; I'm having problems figuring out what's wrong with this line of code. #include <iostream.h> #include <iomanip.h> void print(char **, char ...

  1. #1
    DVF
    DVF is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    A little help here

    I'm having problems figuring out what's wrong with this line of code.


    #include <iostream.h>
    #include <iomanip.h>

    void print(char **, char **, int**);
    void enter(char**, char **, int**);


    void main (void)

    {

    char car [10][5];
    char model[10][5];
    int year[5][1];

    enter((char**)car,(char**)model, (int**)year);
    print((char**)car,(char**)model, (int**)year);


    }

    void enter(char**c, char **m, int** y)
    {
    for (int I = 0; I<5; I++)
    {
    cout<<"Enter car make\n";
    cin>>c[I];

    cout<<"Enter car model\n";
    cin>>m[I] ;
    cout<<"Enter car year\n";
    cin>>y[1][I];
    }

    }

    void print(char **Car, char **Model, int** Year)

    {
    for (int I = 0; I<5; I++)
    {
    cout<<Car[I]<<setw(5)<<Model[I]
    <<setw(5)<<Year[0][I]<<endl;
    }

    }

    This is just a little thing I was experimenting with on passing two dim arrays as pointer parameters, but all I get is junk. What am I doing wrong?

    Also, I want to implement a function that allows the user to search for a car make, and then it prints out the car the model and the year or error message if it doesn't find one. I was thinking of using strcmp, but can't quite figure out yet. Any help will be much apriciated.

  2. #2
    Unregistered
    Guest
    not 100% on this, but remeber that a char** is a pointer to a pointer. For a line like this:
    Code:
    cin>>c[I];
    try this instead
    Code:
    cin >> *c[I];

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in your function calls it would just be:

    enter(car, model, year);
    print(car, model, year);

    I know you are using multidimensional arrays for practice, but using a [5][1] array is unnecessarily complex. You can accomplish all you want with

    int year[5];


    Most people would say that

    char car [10][5];

    declares an array of 10 strings the longest of which is 4 char (the fifth being the terminating null char). The way you are using the loop would also indicate the first subscript is the number of strings, and the second is the size. Therefore, assuming you want 5 strings the longest of which could be 9 char, I would suggest you switch the subscripts around.

    If you insist on using a 2D array for year, then to keep things lined up correctly I would change the following line

    cin>>y[1][I];

    to

    cin >> y[I];

    just like the others input lines.

  4. #4
    DVF
    DVF is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    Ops accidentally I put char make [10][5] instead of char [5][10]. So you mean instead of using a two dimensional int I should use a char I guess? BTW how should I serch for a record? Let's say the user enters the make and the program then prints the model and the year?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21