当我输入时,程序会自动关闭

The program autocloses when I input

本文关键字:程序 输入      更新时间:2023-10-16
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    int k = 0;
    int n;
    int y[0];
    ifstream infile;
    infile.open("xxx.in.txt");
    infile.seekg(1);   //stores line 1 in n
    infile >> n;
    infile.seekg(2);     //stores line 2 in array
    infile >> y[0];
    infile.close();
    for( int x = 0; x < n; x++ )
        for( int j = 1; j < n; j++ )
            if ( y[x] > y[j] )
            {
                k = k++; //should probably use a flag
            }
    ofstream outfile;
    outfile.open("xxx.out.txt");
    outfile << k;
    outfile.close();
    return 0;
}

当我运行程序时,它应该自动运行,因为它不需要用户输入。但是相反,当我运行它时,它不会做任何事情,它只是一个空白的命令提示符

您正在读取和写入文件,而不是标准输入/输出。

如果要将某些内容打印到标准输出(即控制台窗口),请使用 std::cout。