0

I want to get a very large number from the user and put the each individual digits of that number in rows of an array, respectively. That's why I wrote this code in c++. But when I running code and copy that big number and paste in windows Cmd it only receives 4094 digits and does not allow to write more numbers. How to fix this?

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    int size=0; 
    int *a=NULL;
    int *b=NULL;
    int count=0;
    char x='0';

    a=new int[size];
    x=cin.get();  //input by user

    while(isdigit(x)!=0)
        {
            if(count>=size)
            {   
                 b=new int[size+1];
                 for(int i=0;i<size;i++)
                 {
                     b[i]=a[i];
                 }
                 delete []a;
                 a=b;
                 size++;
            }
            a[count++]=x-'0';
            x=cin.get();  //input by user
        }

         cout<<size;
    }

1 Answers1

2

Experimentation has shown me that the Windows cmd.exe has a maximum command line length of approximately 4094 * 2. On my Windows 10 64bit machine I am able to enter a maximum of 8189 characters before it stops allowing me to enter more. This means when I enter a sequence of digits separated by spaces, the most I can possibly enter in a single prompt is 4095 individual digits.

Here's the official Microsoft documentation on the subject:

Command prompt (Cmd. exe) command-line string limitation

Which states:

On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters. On computers running Microsoft Windows 2000 or Windows NT 4.0, the maximum length of the string that you can use at the command prompt is 2047 characters.

This limitation applies to the command line, individual environment variables (such as the PATH variable) that are inherited by other processes, and all environment variable expansions. If you use Command Prompt to run batch files, this limitation also applies to batch file processing.

Microsoft even offers some guidance on how to work around this.

Modify programs that require long command lines so that they use a file that contains the parameter information, and then include the name of the file in the command line.

In your case you're using cin, but the same limitation seems to hold.

What this indicates is your problem lies in the method of entry to the particular prompt. There's a limit to how much can be entered at once.


Related question: Maximum Length of Command Line String

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
sbseltzer
  • 164
  • 7
  • Is it possible to upgrade this range from 4094 to more? – ali Hosseinnezhad Jun 11 '19 at 22:11
  • @aliHosseinnezhad No, it is not. – Remy Lebeau Jun 11 '19 at 22:25
  • It would be very peculiar for such a limit to impact [the Windows equivalent of] STDIN, and none of these quotes say that it does. Yet the OP is using `cin`. Something's not quite right here, sorry. – Lightness Races in Orbit Jun 12 '19 at 01:10
  • That being said, Windows is full of strange and undocumented limits. The [.NET Open File dialog](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog?view=netframework-4.8)'s search functionality breaks in Windows 10 if your selected extension filter has any wildcard list of more than (count 'em) 88 extensions. Why 88? Who the hell knows. – Lightness Races in Orbit Jun 12 '19 at 01:13
  • Note that I say "at once". This doesn't mean stdin itself has a total limit, it means that any single input to cmd.exe has a limit. Each long entry to cmd.exe will truncate the input, the final character in the sequence will be a newline ('\n' is not a digit), and the loop will terminate with a lower count than expected. If they chunk the input into segments, they can continue to enter them by modifying their code to handle the newline, but from what I can tell there's no way to expand the cmd.exe input buffer, which is what they are asking. Maybe this answer should be updated to clarify that? – sbseltzer Jun 12 '19 at 14:33
  • I thought my code had a bug. But with your descriptions, I understand the problem. Thank you for answers – ali Hosseinnezhad Jun 13 '19 at 20:13
  • You're welcome! If this was helpful would you mind setting this as the chosen answer? – sbseltzer Jun 14 '19 at 14:54