c++内存/输入问题
C++ Memory/Input Problem
我试图在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
为数组分配内存。
如果你也把你得到的错误贴出来,也会对回答你的问题有很大的帮助。
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 编写时C++中的输入重定向问题
- 输入std::数组时出现问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- 用户输入字符串的文件附加问题..C++
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 我应该如何在C++中使用结构体解决输入失败的问题?
- 我遇到使用 c++ 逐行读取输入文件的问题
- 在 2D 矢量中获取多个输入时出现问题
- 不确定出了什么问题(字符串输入)
- 在C++中读取 unicode 输入时出现问题
- 函数,从问题中获取输入,并在主函数中使用该输入
- c++中重载输入运算符的问题
- 通过终端在文件中输入时检测EOF(文件结束)时出现问题
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- 从标准输入读取多个类型时出现问题
- visual studio 2008 - c++问题输入