c++内存/输入问题

C++ Memory/Input Problem

本文关键字:问题 输入 内存 c++      更新时间:2023-10-16

我试图在c++中创建一个小程序,其中用户输入多行,程序在之后输出的所有行,给出EOT命令(ctrl-d)

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

(这是一个假设的练习,我不想使用任何向量,列表等,只希望包含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;   
    }
}
}

试试这样做(未经测试,在记事本中编辑):

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

我认为你不能这样做

//Create a new temp array to hold our data
string temp_lines[line_counter+1];

作为line_counter是一个变量,数组大小必须是编译时的时间常数。否则使用new为数组分配内存。

如果你也把你得到的错误贴出来,也会对回答你的问题有很大的帮助。