c++ - 反转行的程序不打印到标准输出

标签 c++

感谢您的帮助! 我真的很感激,这是来自一个新程序员。 我试过编译它,它编译得很好——但是当我用一些数字(1、2、3、4、5、6、7、8、9)测试它以确保它正常工作时,没有任何东西打印到标准输出。该程序应该从标准输入读取最多 1000 行并将它们反转并将它们发送到标准输出。只是为了反转行,而不是每行中的每个字母。再次感谢。

#include <iostream>
using namespace std;
const int N = 999;

int arrayreadin(string line[]){
    int x=0;
    string liner[N];
    while(!cin.fail() && x<=N){
        getline(cin,liner[x]);
        x++;
    }
    return x;
}

void output(string line[], int i){
    int x;
    string liner[N];
    while (x>=0){
        cout << liner[x] << endl;
        x--;
    }
    return;
}
int main(){
    int i, x;
    string line[N];
    arrayreadin(line, x);
    output(line, x);
    return 0;
}

最佳答案

您在这两种方法中都创建了本地字符串数组,但从不返回/使用这些方法中的数据 - 您在 main 中已经有一个数组 - 使用它。另请注意,x 是从 arrayreadin 返回的 - 它不是参数:

int arrayreadin(string line[]){
    int x=0;
    while(!cin.fail() && x<N){ // Array is 0..N-1
        getline(cin,line[x]);
        x++;
    }
    return x;
}

void output(string line[], int x){
    int z= x-1; // Again N-1 to 0
    while (z >= 0){
        cout << line[z] << endl;
        z--;
    }
    return;
}
int main(){
    int i, x;
    string line[N];
    x = arrayreadin(line); // #lines returned
    output(line, x); // passed into output
    return 0;
}

关于c++ - 反转行的程序不打印到标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21668799/

相关文章:

c++ - g++ 创建静态库 : could not read the symbols archive has no index

c++ - 使用 std::stringstream 转换为字符串在空格处失败

c++ - 在 C++11 中使用异步的分段​​错误(核心转储)

c++ - 重载 == 运算符导致丢弃限定符错误

c++ - 试图制作一个原始的控制台贪吃蛇游戏。 0 个警告/错误。为什么我的程序会崩溃? (C++)

c++ - 如何编译 C++ 代码并将其与已编译的 C 代码链接?

c++ - OpenCV:使用 cv::triangulatepoints() 进行立体相机跟踪的问题

c++ - 为什么我的角落小部件没有显示在 QTabWidget 中?

c++ - 语法错误 : Identifier 'string' error C2061

c++ - SFML 仅适用于 Debug模式