Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8029509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:27:34+00:00 2026-06-05T00:27:34+00:00

In C++ can you allocate an array without telling it how big the array

  • 0

In C++ can you allocate an array without telling it how big the array is going to be? I am simulating a grocery checkout program and I was going to use a multidimensional to achieve this because the input needs to be as following:
“Please enter product number and quantity” 110111 4 (first is product number, second is quantity).

This below throws an error because of the second value

#include <iostream>
#include <fstream>
#include "checkout.h"
using namespace std;

int main()
{
    Checkout check;
    int choice;
    int array_size = 0;
    int max = 10;
    int* product_info = new int[max][1];

    do{
        cout << "Please Enter Item Number and Quantity: ";
        cin >> product_info[array_size][array_size];
    }while(!choice == 0);

    system("pause");
    return 0;
}

EDIT * I realize the array cin is messed up in this example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:27:36+00:00Added an answer on June 5, 2026 at 12:27 am

    A two dimensional array does not appear to be very efficient for this task, the array would be very sparse.

    You could dynamically grow it as needed, but only in one dimension (that you can choose as coding time), the other one would have to be permanently fixed, which is at best a half solution.

    In your program you use one dimension that is declared to have bound 1. This means that allowed indexes would range only from 0 to 0 and thus that dimension would not really exist.

    So you simply need something like a one dimensional array (indexed by product ID; the quantity will be placed inside the array rather than as a second dimension to it); and in your application, a vector in place of the array (as indicated in comments) will handle the dynamic aspects (reallocation) for you so that you will not even have to care how big the vector is at the moment.

    The following is how you can declare the structure, except that the POS code (the ID of the item) would normally be too large to fit into an int in a real application, and the quantity might not always be genuinely integral.

    struct transaction_line
    {
        int poscode;
        int quantity;
    };
    
    std::vector<transaction_line> transaction;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you allocate an array using new [] , why can't you find out
In C, I know I can dynamically allocate a two-dimensional array on the heap
I tried to allocate an array of structs in this way: struct T {
According to my class notes, you can allocate an array in C++ like int
I have following static declaration of memory: void* array[5000]; How I can allocate the
I have a server application that, in rare occasions, can allocate large chunks of
can I make my own headers in HTTP request ? e.g. This is normal
Can this be done? It seems like this should be possible. In general, I
You can allocate a std::vector which allocates aligned heap memory by defining your own
How do you allocate an array so it starts at certain place in memory?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.