C++ C++;数组(无限制输入)

C++ C++;数组(无限制输入),c++,C++,我试图做的是,允许用户输入任意数量的变量(例如:1 6 945 fhds),我的程序将检查任何字符串。我听说过数组,但我觉得我只能限制用户可以有多少输入。Foo似乎需要预先输入编码到程序中?有人能澄清怎么做吗 我试过: #include <iostream> using namespace std; int main() { int x; int array[x]; cout << "Enter your numbers: "; for (int x = 0; x =

我试图做的是,允许用户输入任意数量的变量(例如:1 6 945 fhds),我的程序将检查任何字符串。我听说过数组,但我觉得我只能限制用户可以有多少输入。Foo似乎需要预先输入编码到程序中?有人能澄清怎么做吗

我试过:

#include <iostream>
using namespace std;

int main() {
int x;
int array[x];
cout << "Enter your numbers: ";
for (int x = 0; x = <char>; ++x) {
cin >> x;
}
cout << x; 
return 0;
}
#包括
使用名称空间std;
int main(){
int x;
int数组[x];
cout>x;
}

到目前为止还存在一些问题,比如变量“x”的多次声明和误用;for循环可能不应该使用x,因为它已经被定义,并且在未初始化时x不应该是数组大小

至于你想做什么,有点难说,但我想我能帮上忙

int main()
{
    const int ARRAY_SIZE = 1000; //initialize so it doesn't end up being either 0 or literally anything
    int myArray[ ARRAY_SIZE ];
    for( int x = 0; x < ARRAY_SIZE; x++ )
    {
        cin >> myArray[ x ];
    }
    return 0;
}
intmain()
{
const int ARRAY_SIZE=1000;//初始化,这样它就不会变成0或任何值
int myArray[数组大小];
对于(int x=0;x>myArray[x];
}
返回0;
}
现在,这将循环1000次,要求输入一个数字,直到数组被写入并填满。如果你想停止数组,你必须添加一种方法来中断for循环并记录它停止的位置

int main()
{
    const int ARRAY_SIZE = 1000;
    int myArray[ ARRAY_SIZE ];

    int arrayEnd = 0;
    for( int x = 0; x < ARRAY_SIZE; x++ )
    {
        int inputBuffer = 0; // this variable saves the users input and checks to see if the user wants to exit before saving the variable to myArray.
        cin >> inputBuffer;
        if( inputBuffer != -1 ) // -1 is just a value the user enters to stop the loop, choose any number you want for this.
        {
            myArray[ x ] = inputBuffer;
        }
        else
        {
            arrayEnd = x;
            break; // stops the for loop if the number above is entered.
        }
    }

    if( arrayEnd == 0 )
    {
        arrayEnd = ARRAY_SIZE;
    }
    return 0;
}
intmain()
{
常量int数组_SIZE=1000;
int myArray[数组大小];
int-arrayEnd=0;
对于(int x=0;x>输入缓冲区;
如果(inputBuffer!=-1)/-1只是用户输入用于停止循环的值,请为此选择所需的任何数字。
{
myArray[x]=输入缓冲区;
}
其他的
{
arrayEnd=x;
break;//如果输入了上面的数字,则停止for循环。
}
}
如果(arrayEnd==0)
{
arrayEnd=数组大小;
}
返回0;
}
如果您想要真正无限制的或更具延展性的整数数组,您可以新建一个整数数组来设置数组大小,如下所示

int main()
{
    int* myArray = nullptr;
    int arraySize = 0;
    cin >> arraySize;

    myArray = new int[ arraySize ];

    for( int x = 0; x < arraySize; x++ )
    {
        cin >> myArray[ x ];
    }

    delete[] myArray;
    return 0;
}
intmain()
{
int*myArray=nullptr;
int-arraySize=0;
cin>>排列;
myArray=newint[arraySize];
对于(int x=0;x>myArray[x];
}
删除[]myArray;
返回0;
}

<>但是如果你没有新的基础知识,我不建议使用新的,因为新的数据很容易导致内存泄漏和更多的东西要跟踪。C++中的可调数组称为“向量”:

矢量阵列;
cout>temp)
阵列。推回(临时);

coutC++并不是一种快速学习的语言。这里有很多警告和设计模式,您需要做大量的研究,以熟悉它们而不会造成损害。用户4581301建议是一个很好的开始

网上也有一些很好的参考资料,但更糟糕的是

例如,您的问题有几个问题(除了已经提到的初始化等问题)

我将尝试从更高的层次上解决两个问题

  • 我建议您在需要时使用数组。如果可以,请选择不同的抽象。明智地选择数据类型。以下是一些标准示例。你也可以利用。您可以使用指针并将其视为一个数组,在这种情况下,您可以完全控制内存的管理。您甚至可以使用混合方法,使用类似向量的连续容器,并通过指针访问它。然而,指针和数组相当脆弱,并且容易出错。面对例外,他们更是如此,我建议总是首先考虑原则。

  • 根据你计划阅读的数据,你可能需要考虑你正在使用的类型。您可能还需要考虑编码。我想了解一下弦的世界

  • 阅读全文
  • 按空间分割并获取输入
  • 检查每个输入的类型
  • #include <string>
    #include <iostream>
    #include <vector>
    #include <sstream>
    
    
    std::vector<std::string> split(const std::string &s) {
        std::stringstream ss(s);
        std::string item;
        std::vector<std::string> elems;
        while (std::getline(ss, item, ' ')) {
            elems.push_back(item);
        }
        return elems;
    }
    
    int main()
    {
        std::string line;
        std::getline(std::cin, line);
        std::vector<std::string> elems = split(line); 
        for (int i = 0; i < elems.size; i++)
        {
            /*
            if is number 
              do something
            else
                ignore
            */
        }   
    }
    
    #包括
    #包括
    #包括
    #包括
    std::vector split(const std::string&s){
    标准::stringstream ss(s);
    std::字符串项;
    std::向量元素;
    while(std::getline(ss,item“”){
    元素推回(项目);
    }
    返回元素;
    }
    int main()
    {
    std::字符串行;
    std::getline(std::cin,line);
    标准::向量元素=分割(线);
    对于(int i=0;i
    是要存储字符串,还是只打印字符串?这是
    int x;int数组[x]只会给你带来痛苦。一方面,它不会在所有编译器上编译,因为x不是常数,但更重要的是,x没有被赋值,因此如果它编译,数组的大小是未定义的。@user4581301我想响应字符串:如果输入是数字,则执行y,如果不是,则执行x。这是我的另一个问题,但我想我一次只能问一个问题。在这个特殊的问题中,我不确定如何实际输入无限的字符串,以及如何按顺序扫描字符串。基于您的模糊问题“我听说过数组”和您的模糊代码片段,我建议您购买一本教科书或做一个在线教程。@John3136您能提供一个到在线教程的链接吗?我在网上查找了阵列教程,正如我在帖子中所说的,阵列只允许有限数量的输入(例如:5)。在一个特别的教程中,作者提到了无限输入,但他们写了类似int foo{43,58,32,4234}的东西,这意味着用户不能真正输入任何东西。虽然这种方法在直C中有一些优点,因为需要增加复杂性来消除其缺点,但是
    
    #include <string>
    #include <iostream>
    #include <vector>
    #include <sstream>
    
    
    std::vector<std::string> split(const std::string &s) {
        std::stringstream ss(s);
        std::string item;
        std::vector<std::string> elems;
        while (std::getline(ss, item, ' ')) {
            elems.push_back(item);
        }
        return elems;
    }
    
    int main()
    {
        std::string line;
        std::getline(std::cin, line);
        std::vector<std::string> elems = split(line); 
        for (int i = 0; i < elems.size; i++)
        {
            /*
            if is number 
              do something
            else
                ignore
            */
        }   
    }