C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Is It Possible Posted by capitolc on 6 Aug 2005 at 2:54 AM
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=-
Report
Re: Is It Possible Posted by stober on 6 Aug 2005 at 3:44 AM
This message was edited by stober at 2005-8-6 3:45:47

: 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 <vector> template class to store <string> objects (or other kinds of objects too. You don't have to do all that messy c-style pointer stuff.

#include <vector>
#include <string>
#include <iostream>
using namespace std;

int main()
{
   vector<string> 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;
}  




Report
Re: Is It Possible Posted by RIGHT_THEN on 6 Aug 2005 at 7:59 AM
: 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
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].....;


Report
Re: Is It Possible Posted by Donotalo on 6 Aug 2005 at 8:49 AM
This message was edited by Donotalo at 2005-8-6 8:53:19

: 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
:
: name[][3];//is right but no, wrong
: name[5][];// is wrong    ya
: 
: to my knowledge these are the ways to define array
: int *name;//is right
: int *name[];//is rigt     no, wrong
: int name[];//is right     no, wrong
: int name[][3];//is right   no, wrong
: int name[][3][3][3];//is right  no, wrong
: 
: 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].....;
no, wrong
: 
: 

the codes u said right is only for function protypes, not for declaration.


Report
Re: Is It Possible Posted by RIGHT_THEN on 6 Aug 2005 at 9:58 AM
: This message was edited by Donotalo at 2005-8-6 8:53:19

: : 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
: :
: : 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 no, wrong
: : name[5][];// is wrong    ya
: : 
: : to my knowledge these are the ways to define array
: :    int *name;//is right
: :(1) int *name[];//is rigt   no, wrong //this one sure 
                                                     is wrong i agree
: :    int name[];//is right  no, wrong//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   no, wrong
: : int name[][3][3][3];//is right  no, wrong
: : 
: : 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].....;
: no, wrong
: : 
: : 

: the codes u said right is only for function protypes, not for declaration.
:
:
:

Report
Re: Is It Possible Posted by Donotalo on 6 Aug 2005 at 11:36 AM
This message was edited by Donotalo at 2005-8-6 11:38:33

sorry, i didnt understand. i thought int name[][3]; 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
: : :
: : : 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 no, wrong
: : : name[5][];// is wrong    ya
: : : 
: : : to my knowledge these are the ways to define array
: : :    int *name;//is right
: : :(1) int *name[];//is rigt   no, wrong //this one sure 
:                                                      is wrong i agree
: : :    int name[];//is right  no, wrong//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   no, wrong
: : : int name[][3][3][3];//is right  no, wrong
: : : 
: : : 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].....;
: : no, wrong
: : : 
: : : 

: : the codes u said right is only for function protypes, not for declaration.
: :
: :
: :
:
:



Report
Re: Is It Possible Posted by Lundin on 8 Aug 2005 at 5:44 AM
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.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.