C++;内存/输入问题 我试图在C++中创建一个小程序,用户在其中输入多行,程序输出强> > 强>一个EOT命令(CTRL D)

C++;内存/输入问题 我试图在C++中创建一个小程序,用户在其中输入多行,程序输出强> > 强>一个EOT命令(CTRL D),c++,input,C++,Input,但我在执行程序时遇到了一些错误。我想我做错了很多 (这是一个假设的练习,我不想使用任何向量、列表等,只想包括iostream #include <iostream> using namespace std; int main() { //Temp input string string input_string; //Array with input lines string lines[1]; //Counter for input lines size_t lin

但我在执行程序时遇到了一些错误。我想我做错了很多

(这是一个假设的练习,我不想使用任何向量、列表等,只想包括iostream

#include <iostream>

using namespace std;

int main()
{
//Temp input string
string input_string;

//Array with input lines
string lines[1];    

//Counter for input lines
size_t line_counter = 0;

//Input terminated checker
bool breaker = false;

//Eternal loop
for(;;){

    //Get line, store in input_string and set breaker if input is terminated
    if(getline(cin, input_string).eof()) breaker = true;

    //Create a new temp array to hold our data
    string temp_lines[line_counter+1];
    for(size_t counter = 0; counter != line_counter; ++counter){
        //And use a for loop to get data from our last array with data
        temp_lines[counter] = lines[counter];
    }

    //Create a second array and repeat process
    //because c++ doesn't allow us to create dynamic array's 
    string lines[line_counter+1];
    for(size_t counter = 0; counter != line_counter; ++counter){
        lines[counter] = temp_lines[counter];
    }

    //store input in the new array
    lines[line_counter] = input_string;            

    //increase the input counter
    ++line_counter;

    //if breaker is set terminate loop but output lines first
    if(breaker){

        //for each input
        for(size_t anothercounter = 0; anothercounter != line_counter; ++anothercounter){

            //output the inputed line
            cout << anothercounter << ": " << lines[anothercounter] << "\n";

        }

        //break out of eternal for loop
        break;   
    }

}

}
#包括
使用名称空间std;
int main()
{
//临时输入字符串
字符串输入_字符串;
//带输入线的数组
弦线[1];
//输入线计数器
尺寸线计数器=0;
//输入终止检查器
布尔断路器=假;
//永恒循环
对于(;;){
//获取行,存储在输入字符串中,并在输入终止时设置断路器
if(getline(cin,input_string).eof())断路器=true;
//创建一个新的临时数组来保存数据
字符串临时行[行计数器+1];
用于(大小计数器=0;计数器!=行计数器;++计数器){
//并使用for循环从上一个包含数据的数组中获取数据
临时线路[计数器]=线路[计数器];
}
//创建第二个数组并重复该过程
/因为C++不允许我们创建动态数组
字符串行[行计数器+1];
用于(大小计数器=0;计数器!=行计数器;++计数器){
行[计数器]=临时行[计数器];
}
//将输入存储在新数组中
行[行\u计数器]=输入\u字符串;
//增加输入计数器
++线路计数器;
//若断路器已设置,则终止回路,但首先终止输出线路
如果(断路器){
//对于每个输入
对于(大小\u t另一个计数器=0;另一个计数器!=行\u计数器;++另一个计数器){
//输出输入的行

有一件事我看错了,那就是你不能这样做

//Create a new temp array to hold our data
string temp_lines[line_counter+1];
因为
line\u counter
是一个变量,数组大小必须是编译时时间常数。否则,请使用
new
为数组分配内存

此外,如果您还发布了您遇到的错误,这将有助于回答您的问题。

尝试类似的方法(未经测试,在记事本中编辑):

#包括
使用名称空间std;
int main()
{
//临时输入字符串
字符串输入_字符串;
//带输入线的数组
字符串*行=0;
//用于临时存储的阵列
字符串*临时线=0;
//输入线计数器
尺寸线计数器=0;
//输入终止检查器
布尔断路器=假;
//永恒循环
对于(;;){
//获取行,存储在输入字符串中,并在输入终止时设置断路器
if(getline(cin,input_string).eof())断路器=true;
//将所有行从原始阵列复制到临时阵列,以允许调整原始阵列的大小
临时行=新字符串[行计数器+1];
对于(大小tmp=0;tmp对于(size\u t tmp=0;tmp为什么使用
字符串而不是
向量
?我想你说过你只能包含
iostream
?还可以发布你得到的错误。如果可能的话,发布整个编译器输出。尽管有几个编译器允许这样做,但最好记住这是违反规则的。@MooingDuck这也是非法的
line\u counter
的值在每个循环中都会递增,每次需要初始化一个新数组,编译器无法预测其大小。因此,它不能在堆栈上分配,必须使用
new
操作符在堆上分配。这就是我所说的。但是,默认情况下,gcc允许它,而且大多数com编译器有alloca的功能,可以完成这项工作。编译和运行都很好。(排序是我最后一个问题,我的错)
#include <iostream>

using namespace std;

int main()
{
    //Temp input string
    string input_string;
    //Array with input lines
    string * lines = 0;    
    // Array used for temporary storage
    string * temp_lines = 0;
    //Counter for input lines
    size_t line_counter = 0;
    //Input terminated checker
    bool breaker = false;
    //Eternal loop
    for(;;){
        //Get line, store in input_string and set breaker if input is terminated
        if(getline(cin, input_string).eof()) breaker = true;
        // Copy all lines from original array to temporary array, to enable resizing the original
        temp_lines = new string[line_counter+1];
        for(size_t tmp = 0; tmp < line_counter; tmp++) temp_lines[tmp] = lines[tmp];
        temp_lines[line_counter] = input_string;
        delete [] lines;
        lines = new string[line_counter+1];
        for(size_t tmp = 0; tmp <= line_counter; tmp++) lines[tmp] = temp_lines[tmp];
        delete [] temp_lines;
        //increase the input counter
        ++line_counter;
        //if breaker is set terminate loop
        if(breaker) break;
    }
    //for each input
    for(size_t anothercounter = 0; anothercounter != line_counter; ++anothercounter){
        //output the inputed line
        cout << anothercounter << ": " << lines[anothercounter] << "\n";
    } 
}