Arrary with user defined size

This is a discussion on Arrary with user defined size within the C++ Programming forums, part of the General Programming Boards category; Code: void main() { const int size; cout << "Please enter a amount of lights" << endl; cin >> size; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Arrary with user defined size

    Code:
    void main()
    {
    
    	const int size;
    	cout << "Please enter a amount of lights" << endl;
    	cin >> size;
    
    	int lights[size];
    
    	for (int i = 0; i < n; i++)
    	{
    		lights[i] = 1;
    	}
    
    
    }
    How come my arrary won't take a user entered size?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Firstly, size is const. More importantly, standard C++ does not provide for variable length arrays. Use a std::vector instead, e.g.,
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<int>::size_type size;
        std::cout << "Please enter a amount of lights" << std::endl;
        std::cin >> size;
    
        std::vector<int> lights(size, 1);
    
        // No need for the init loop any more.
        // ... do other stuff ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    We have not got to vectors in class. I don't know if I can use that I will play with it anyway thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    If you cannot use the standard containers, then it implies that you should use new[] and delete[]. If you cannot use that either, then you should create a large enough array, and only use the first size elements of that array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Alright I'll see what I can do I'm trying to make a program using recursion to set all of the lights to off (0) when they are initially set to on (1).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Alright I'll see what I can do I'm trying to make a program using recursion to set all of the lights to off (0) when they are initially set to on (1).
    Write a function to print the integers from 0 to size-1, having only the current number and the size as the parameters. Then add a parameter for the array of lights and start setting the lights to off instead of printing integers.

    Alternatively, write a function to print the integers from size-1 to 0, having only the current number as the parameter. Then add a parameter for the array of lights and start setting the lights to off instead of printing integers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    try like so???

    in your lights .cpp

    Code:
    void lights::makeArray(){
    	int size;
    	cout << "Please enter a amount of lights" << endl;
    	cin >> size;
    
    	int lights[size];
    
    	for (int i = 0; i < size-1; i++)
    	{
    		lights[i] = 1;
    	}
    }

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Nope. That gets the value into size perfectly well, but it declares a variable-length array, which is apparently not standard C++.

    (BTW, the for loop should likely have this condition: "i < size".)

    laserlight has already supplied a vector example. Here's a dynamic memory allocating one.
    Code:
    // after reading int size from the user:
    int *lights = new int[size];
    // ... use the array lights ...
    delete [] lights;
    The delete statement frees the memory allocated by new, and should be called after you are done using the array. Otherwise you have a memory leak.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,765
    Also,
    http://cpwiki.sf.net/Void_main
    Get rid of the void main and use int main.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 05:20 AM
  4. need to read a User defined file during runtime
    By john_newbie in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:08 AM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM

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