Thread: Entering size of an array

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Entering size of an array

    I am trying to write a program to allow a user to enter the order (size) of a group of objects, then name them.
    I have tried using an array but this requires a fixed size.
    I have also tried setting up new variables each time in a loop, but I cannot find a way to find a new name for the variables each time the loop goes round.
    I have also tried what is suggested on this page but the program didn't compile because it didn't like my declaration:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int order;
        cout <<"CREATE GROUP\nEnter order of G: ";
        cin>>order;
        cin.ignore();
        cout<<"order of group is "<<order<<"\nEnter name of identity element (e.g. e, 1 or 0): ";
        class *names;//HERE
        names=new class [<<order<<];
        cin>>*names[0];
        cin.ignore();
    The computer complains where I marked, 'invalid type in declaration before ';' token'

    Any suggestions?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The line "class *names;" is a declaration of a variable of named "names" of type "pointer to class".

    However, "class" is a keyword in C++, and cannot be used as a type name. Hence the error message.

    Read up on how you specify classes, before trying to create arrays.

    The example of "Dynamic Arrays" in that link you gave is about creating an array of MyClass. That whole page assumes you understand how to specify a new type. That is because, if you want to create an array of X, it is necessary (in C++ at least) that X be specified first.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    names is suppose to be a string array right?

    Code:
    std::string * names;
    names = new std::string[order];
    Last edited by codeprada; 02-12-2011 at 04:06 PM. Reason: typooooooo
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    From what I understood you are trying to make a program which asks you for the size of an array which will contain strings. In that case try with this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int o; //o stands for order
        int i;
        char names[o]; //the array "names" is the same size as "o"
        
        cout <<"Insert the order: "<<endl;
        cin >> o;
        
        for(i=0; i<o; i++)
        {
            cout <<"Insert the<<i<<element: "<<endl;
            cin >> names[i];
        }
        
        system("pause");
        return 0;
    }
    "A Computer in every desk and in every home, running Microsoft software."

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by godly 20 View Post
    From what I understood you are trying to make a program which asks you for the size of an array which will contain strings. In that case try with this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int o; //o stands for order
        int i;
        char names[o]; //the array "names" is the same size as "o"
        
        cout <<"Insert the order: "<<endl;
        cin >> o;
        
        for(i=0; i<o; i++)
        {
            cout <<"Insert the<<i<<element: "<<endl;
            cin >> names[i];
        }
        
        system("pause");
        return 0;
    }
    'o' wasn't initiaized yet, how could you use it to declare the size for names???? This sets up a world of hurt for debugging.

    Look up <vector>, use it as your container class, the size of vector and be expand or contracted by using the member function.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    and even if it was initialized you can't declare an array like that without a constant. to declare arrays with non constant variables use the new keyword
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by codeprada View Post
    and even if it was initialized you can't declare an array like that without a constant. to declare arrays with non constant variables use the new keyword
    You could if your compiler support variable size array.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by nimitzhunter View Post
    You could if your compiler support variable size array.
    none of my compilers ever did
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CatastropheNoob View Post
    I have also tried setting up new variables each time in a loop, but I cannot find a way to find a new name for the variables each time the loop goes round.
    Why would a user of the program care what a variable is named? That name doesn't exist when you compile the program, it's just a memory address.

    What do you actually want your program to do once you allocate an array of objects?

    I'd recommend using an std::vector first, then learn about pointers & memory allocation once you have a better understanding of how to program in C++. An std::vector is like an array that grows and shrinks automatically (it handles all the memory allocation internally).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    15
    I think CatastropheNoob wants a "custom sized array of named objects". It is easy to achieve with std::map:
    Code:
    std::map<std::string,std::string> mymap;
    
    mymap["name1"]="an element";
    mymap["another name"]="another element";
    The first parameter in map declaration is type of name. The second one is type of object. In my example both are strings.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Would this work?

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    int main ()
    {
    	using namespace std;
    	int iArraySize = 0;
    	cout << "Enter number of items: ";
    	cin >> iArraySize;
    	string *spaText = new string [iArraySize];
    	for (int i = 0; i < iArraySize; i++)
    	{
    		cout << endl << endl << "Enter text for item #" << i << ": ";
    		cin >> spaText [i];
    	}
    	cout << endl;
    	for (int i = 0; i < iArraySize; i++)
    	{
    		cout << "Text for item #" << i << ":\t" << spaText [i] << endl;
    	}
    	delete [] spaText;
    	getch ();
    	return 0;
    }

  12. #12
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Quote Originally Posted by nimitzhunter View Post
    You could if your compiler support variable size array.
    g++ 4.5.2 supports it here, but... VLAs are not a feature of C++. If it works, then only because you compile in non-pedantic mode with compiler specific extensions enabled - and it is indeed an extension of GCC.

    Again, VLAs are a feature of C99, not C++.
    In C++ you should use some container like vector, list, map or whatever suits the needs. If you really want, you still can manage your memory by hand, using the "new" keyword as was already said by codeprada. (remember to always delete what you newed

  13. #13
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I have always used pointers to accomplish this.... I usually deal with 2d arrays though it would be the same as a vector of vectors of class instantiations. Which I would think would be easier to change size whenever you want with my method I would *probably* end up increasing size by copying then adding so vectors would be the way you want to go if it will be completely dynamic size changinging from here to there. I would think if you just wanted to ask something like "how many inputs?" User enters some number and you work off that number which will not change during this run of the program pointers would be more efficent memory wise/cpu cycle wise as you don't have the overhead of the vector class all be it useful when you want to change things on the fly.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The vector overhead is negligible in 99% of the case, though. Don't avoid it unless you have measured a significant performance hit in it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Overhead is overhead I have programed for IC chips with only a few kb worth of storage mostly written in asm mind you, but pointers work just as well in a class as a vector would and with a proper destructor you don't run into memory leaks unless your program already has them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  2. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 08:45 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM

Tags for this Thread