开发者

C++ Memory/Input Problem

I'm trying to create a small program in c++ where the user puts in multiple lines and the program outputs all the lines of after a EOT command is given (ctrl-d)

But i'm getting some error's when executing the program. I think i did a lot wrong.

(This is an hypothetical exersise, i don't want to use any vectors, lists etc., and only want include 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开发者_StackOverflow社区 loop
        break;   
    }

}

}


Try something like this (untested, edited in notepad):

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


For one thing I can see wrong is you cannot do this

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

as line_counter is a variable, and array size must be a compile time time constant. Otherwise use new to allocate memory for the array.

Also it would help a lot in answering your question if you also post the errors you are getting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜