C++中不寻常的iostream事件

Unusual iostream happenings in C++?

本文关键字:iostream 事件 不寻常 C++      更新时间:2023-10-16

我有以下程序,它编译良好,没有错误或警告:

#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "classes.h"
int width = 0;
int height = 0;
int init(int width = 640, int height = 480, int bpp = 32) // needs to be the first  statement called in main()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetVideoMode(width, height, bpp, SDL_OPENGL);
    SDL_WM_SetCaption("SpaceInvaders", NULL);
    glClearColor(0,0,0,0);
}
void setstates(int width = 640, int height = 480) // needs to be called when the    window is resized
{
    ::width = width;
    ::height = height;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
int main (int argc, char** argv)
{
    setstates();
    std::cout << ::width << std::endl;
    std::cout << ::height << std::endl;
    int a ;
    std::cin >> a;
    return 0;
}

我发现奇怪的是,当我在主函数中调用cout并运行程序时,控制台出现,但什么都不显示,当我按下一个字母并输入时,程序返回0,就像在cin语句中一样,但控制台甚至不会显示我按下的字母。

然而,我在编译后的程序目录中得到了一个stdout.txt文档,其中显然是用换行符格式化的定制信息。

所以我的问题是问题出在哪里?为什么会发生这种事,我以前从未经历过。

感谢您提前抽出时间

SDL重定向您的输出,如常见问题解答中所述。