简体   繁体   中英

How to give more number with cin.get() in c++?

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;
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM