array problem

This is a discussion on array problem within the C++ Programming forums, part of the General Programming Boards category; SOLVED!!!! im making a simple program that first asks the user for the number of items. "items" is the size ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    array problem

    SOLVED!!!!

    im making a simple program that first asks the user for the number of items. "items" is the size of an array. and then after that the program should ask the user for the value of however many items he entered
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(){
        int NumberOfItems;
        int Items[NumberOfItems];
        cout<<"enter number of items\n";
        cin>>NumberOfItems;
        cin.get();
        for (int i = 0;i < NumberOfItems;++i){
            cout<<"item"<<i = i + 1<<":" <<Items[i]<<"\n";
            cin<<Items[i];
            };
    cin.get();
    };
    thats the code

    SOLVED:i overcomplicated the code a bit.. i didnt even need <<Items[i]<< in the for statement. same thing with i = i + 1.

    heres my new code

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(){
        int NumberOfItems;
        int Items[NumberOfItems];
        cout<<"enter number of items\n";
        cin>>NumberOfItems;
        cin.get();
        for (int i = 0;i < NumberOfItems; ++i){
            cout<<"item"<<i + 1<<":\n";
            cin>>Items[i];
            cin.get();
            };
    cin.get();
    };
    still going to be doing a bit more array work
    Last edited by lilhawk2892; 11-05-2007 at 03:55 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    So what's the problem?

    Your array size is dynamic, which means you don't know what it will be until the program is already running. This isn't allowed for regular arrays in C++ (although some compilers do allow it).

    One way to solve it is to use a dynamic array. The vector class from the standard library is probably the best way to do that. Once you get the number of items from the user, you create the vector with that number of entries.

    Another solution is to have a maximum size for your array. You would make the array that size, but then only use however many entries you need for the number of items the user wants to store. This would be a common approach for beginners who aren't familiar with vector or dynamic arrays.

    Whatever solution you choose, remember that you should use the NumberOfItems variable until you fill it with the data from the user. The program has a flow and if you use a variable before it gets its value then it won't function properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

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