how can i determine the size?

This is a discussion on how can i determine the size? within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> #include <cctype> using namespace std; struct inventory_items { int code,quantity,reorder_level; char name[20], item[3]; double price; }; inventory_items record[2000]; ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    Angry how can i determine the size?

    #include <iostream>
    #include <cctype>
    using namespace std;

    struct inventory_items
    {
    int code,quantity,reorder_level;
    char name[20], item[3];
    double price;
    };

    inventory_items record[2000];

    Question: What should I do to allow the user to enter the size of the array for inventory_items record himself instead of me specifying the size? Thanks!

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: how can i determine the size?

    Originally posted by Mingzhi

    Question: What should I do to allow the user to enter the size of the array for inventory_items record himself instead of me specifying the size? Thanks!
    use std::vector
    Code:
    #include <vector>
    ...
    std::vector<inventory_items> itemVec;

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i would suggest you creata a pointer inside the struct...

    Code:
    
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    struct inventory_items
    {
    int code,quantity,reorder_level;
    char name[20], item[3];
    double price;
    };
    
    
    int main()
    {
    inventory_items *record;
    int num=100;    \\ this can be accepted fro the user..
    record=new inventory_items[num]; \\will create an array of 100
    return 0;
    
    }


    But make sure you clear up the memory by using delete..



    edited
    --------

    ohh boy.. i thought i was the first to answer this and 2 guys beat me to it..

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    Thanks

    A big thanks to all of u

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  2. Having a problem with templates and classes
    By bowluswj in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 12:55 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Determine array size
    By eth0 in forum C++ Programming
    Replies: 16
    Last Post: 01-08-2004, 09:26 AM

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