如何在C ++中使用cin.get()给更多的数字? [英] How to give more number with cin.get() in c++?

查看:40
本文介绍了如何在C ++中使用cin.get()给更多的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从用户那里得到一个非常大的数字,并将该数字的各个数字分别放入数组的行中.这就是为什么我用c ++编写此代码的原因.但是,当我运行代码并复制那个大数字并将其粘贴到Windows Cmd中时,它仅接收4094位数字,并且不允许写入更多数字.该如何解决?

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

推荐答案

实验表明,Windows cmd.exe的最大命令行长度约为4094 *2.在Windows 10 64位计算机上,我可以输入最多8189个字符,然后它才能停止输入.这意味着当我输入由空格分隔的数字序列时,一次提示中最多可以输入4095个独立数字.

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.

这是有关此主题的Microsoft官方文档:

Here's the official Microsoft documentation on the subject:

命令提示(Cmd.exe)命令行字符串限制

哪个州:

在运行Microsoft Windows XP或更高版本的计算机上,可以在命令提示符下使用的字符串的最大长度为8191个字符.在运行Microsoft Windows 2000或Windows NT 4.0的计算机上,可以在命令提示符下使用的字符串的最大长度为2047个字符.

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.

此限制适用于命令行,其他进程继承的单个环境变量(例如PATH变量)以及所有环境变量扩展.如果使用命令提示符运行批处理文件,则此限制也适用于批处理文件处理.

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甚至提供了有关如何解决此问题的指导.

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.

在您的情况下,您使用的是cin,但似乎存在相同的限制.

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.

相关问题:命令行字符串的最大长度

这篇关于如何在C ++中使用cin.get()给更多的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆