Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Is It Possible

capitolccapitolc Posts: 31Member
Group

Using C++, is there a way to write code so that you can store a string of characters in a multi-dimensional array taken from the user? I recall a string copy function in C, but I can't figure out how to do this in C++.

Sounds like a simple problem, but I want to store a name and a grade within this array. The trick I'm trying to do, is make one of the dimensions open to flex to the number of characters within the given name inputting by the user. (ie. name[5][*]) Total of five names, and the length is determined by the name.

The complier is not letting me define an array like such, but they has to be a way. Any ideas?

-=The Best Has Yet To Come=-

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-8-6 3:45:47[/red][/b][hr]
    : Group
    :
    : Using C++, is there a way to write code so that you can store a string of characters in a multi-dimensional array taken from the user? I recall a string copy function in C, but I can't figure out how to do this in C++.
    :
    : Sounds like a simple problem, but I want to store a name and a grade within this array. The trick I'm trying to do, is make one of the dimensions open to flex to the number of characters within the given name inputting by the user. (ie. name[5][*]) Total of five names, and the length is determined by the name.
    :
    : The complier is not letting me define an array like such, but they has to be a way. Any ideas?
    :
    : -=The Best Has Yet To Come=-
    :


    In c++ use template class to store objects (or other kinds of objects too. You don't have to do all that messy c-style pointer stuff.

    [code]
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    vector theArray;
    string line;
    cout << "Enter some text";
    getline(line,cin);
    theArray.push_back(line);

    cout << "The text you entered was " << theArray[0] << endl;
    return 0;
    }
    [/code]



  • RIGHT_THENRIGHT_THEN Posts: 154Member
    : Group
    :
    : Using C++, is there a way to write code so that you can store a string of characters in a multi-dimensional array taken from the user? I recall a string copy function in C, but I can't figure out how to do this in C++.
    :
    : Sounds like a simple problem, but I want to store a name and a grade within this array. The trick I'm trying to do, is make one of the dimensions open to flex to the number of characters within the given name inputting by the user. (ie. name[5][*]) Total of five names, and the length is determined by the name.
    :
    : The complier is not letting me define an array like such, but they has to be a way. Any ideas?
    :
    : -=The Best Has Yet To Come=-
    :

    now i might be wrong here but it is not the rule that only
    first dimension of the array can be left open. by that i mean
    [code]
    name[][3];//is right but
    name[5][];// is wrong

    to my knowledge these are the ways to define array
    int *name;//is right
    int *name[];//is rigt
    int name[];//is right
    int name[][3];//is right
    int name[][3][3][3];//is right

    int name[10][];//worng
    int name[][20][];//wrong
    int name[][][][10];//wrong
    int name[][][][];wrong

    int name[can be left empty][must be filled][must be filled][must be filled][everydimension after me must be filled also].....;

    [/code]
  • DonotaloDonotalo Posts: 715Member
    [b][red]This message was edited by Donotalo at 2005-8-6 8:53:19[/red][/b][hr]
    : now i might be wrong here but it is not the rule that only
    : first dimension of the array can be left open. by that i mean
    : [code]
    : name[][3];//is right but [red]no, wrong[/red]
    : name[5][];// is wrong [red]ya[/red]
    :
    : to my knowledge these are the ways to define array
    : int *name;//is right
    : int *name[];//is rigt [red]no, wrong[/red]
    : int name[];//is right [red]no, wrong[/red]
    : int name[][3];//is right [red]no, wrong[/red]
    : int name[][3][3][3];//is right [red]no, wrong[/red]
    :
    : int name[10][];//worng
    : int name[][20][];//wrong
    : int name[][][][10];//wrong
    : int name[][][][];wrong
    :
    : int name[can be left empty][must be filled][must be filled][must be filled][everydimension after me must be filled also].....;
    [red]no, wrong[/red]
    :
    : [/code]
    the codes u said right is only for function protypes, not for declaration.


  • RIGHT_THENRIGHT_THEN Posts: 154Member
    : [b][red]This message was edited by Donotalo at 2005-8-6 8:53:19[/red][/b][hr]
    : : now i might be wrong here but it is not the rule that only
    : : first dimension of the array can be left open. by that i mean
    : : [code]
    : : SIR DONOTALO,
    are you suggesting below statement wrong because i did not write
    like this:-
    int name[][3]={1,2,3};//or is there some other reason

    name[][3];//is right but [red]no, wrong[/red]
    : : name[5][];// is wrong [red]ya[/red]
    : :
    : : to my knowledge these are the ways to define array
    : : int *name;//is right
    : :(1) int *name[];//is rigt [red]no, wrong[/red] //this one sure
    is wrong i agree
    : : int name[];//is right [red]no, wrong[/red]//this you are
    saying wrong because
    it is not like this
    int name[]={1,2,3};

    //if that is the reason then i just skipped that part
    //simply to tell initial syntax;but if there is some other reason
    //then kindly put me wise
    //because on my compiler all the statements that i have
    //said right are compiling properly.except fot (1)

    : : int name[][3];//is right [red]no, wrong[/red]
    : : int name[][3][3][3];//is right [red]no, wrong[/red]
    : :
    : : int name[10][];//worng
    : : int name[][20][];//wrong
    : : int name[][][][10];//wrong
    : : int name[][][][];wrong
    : :
    : : int name[can be left empty][must be filled][must be filled][must be filled][everydimension after me must be filled also].....;
    : [red]no, wrong[/red]
    : :
    : : [/code]
    : the codes u said right is only for function protypes, not for declaration.
    :
    :
    :

  • DonotaloDonotalo Posts: 715Member
    [b][red]This message was edited by Donotalo at 2005-8-6 11:38:33[/red][/b][hr]
    sorry, i didnt understand. i thought [blue]int name[][3];[/blue] IS the declaration! :-)

    : : : now i might be wrong here but it is not the rule that only
    : : : first dimension of the array can be left open. by that i mean
    : : : [code]
    : : : SIR DONOTALO,
    : are you suggesting below statement wrong because i did not write
    : like this:-
    : int name[][3]={1,2,3};//or is there some other reason
    :
    : name[][3];//is right but [red]no, wrong[/red]
    : : : name[5][];// is wrong [red]ya[/red]
    : : :
    : : : to my knowledge these are the ways to define array
    : : : int *name;//is right
    : : :(1) int *name[];//is rigt [red]no, wrong[/red] //this one sure
    : is wrong i agree
    : : : int name[];//is right [red]no, wrong[/red]//this you are
    : saying wrong because
    : it is not like this
    : int name[]={1,2,3};
    :
    : //if that is the reason then i just skipped that part
    : //simply to tell initial syntax;but if there is some other reason
    : //then kindly put me wise
    : //because on my compiler all the statements that i have
    : //said right are compiling properly.except fot (1)
    :
    : : : int name[][3];//is right [red]no, wrong[/red]
    : : : int name[][3][3][3];//is right [red]no, wrong[/red]
    : : :
    : : : int name[10][];//worng
    : : : int name[][20][];//wrong
    : : : int name[][][][10];//wrong
    : : : int name[][][][];wrong
    : : :
    : : : int name[can be left empty][must be filled][must be filled][must be filled][everydimension after me must be filled also].....;
    : : [red]no, wrong[/red]
    : : :
    : : : [/code]
    : : the codes u said right is only for function protypes, not for declaration.
    : :
    : :
    : :
    :
    :



  • LundinLundin Posts: 3,711Member
    When allocation an array staticly, you must -always- declare the width of it. No exceptions. Even if C allows this line:

    int x[] = {1,2,3};

    It is still the very same as

    int x[3] = {1,2,3};

    The 3 is just writting explicitly in the second case. The width is set at compilation-time in both cases.


    You are confusing it with function parameters. When declaring a function parameter, you can leave out the first dimention, sence the arrays passed to functions are translated to pointers. This is to prevent the programmer from passing arrays by value on the stack.
    You can write a function prototype like this:

    int func(int arr[]);
    int func(int arr[3]);

    In both cases "arr" is translated to a pointer. You have -not- allocated an array in any of the two examples.


Sign In or Register to comment.