2017-04-25 95 views
-6
World::World() { 
    int width = -1; 
    int height = -1; 

    cout << "Aby rozpoczac gre wpisz rozmiar planszy" << endl; 
    cout << "Aby uruchomic domyslne romiary zostaw pole puste" << endl; 

    cout << "Podaj szerokosc: "; 
    // I don't see what i'm typing and i need to press enter twice; 
    cin >> width; 

    cout << "Podaj wysokosc: "; 
    // I don't see what i'm typing too and make my program crash 
    cin >> height; 

    World(height, width); 
} 

cout工作很好,但cin是soooo buged。 虽然第一个cin我没有看到我在输入什么,我需要按两次输入; 虽然SECEND CIN我什么也没看到过,虽然我按任意键,程序崩溃C++ <iostream> cin不能正常工作

+0

的代码你展示我们应该工作。你的问题在别处。 –

+1

你觉得'世界(高度,宽度)是多少'? –

+1

我的建议是学会使用调试器。并且不要急于认为您发现了某种类型的编译器或系统错误。 – drescherjm

回答

1

线

World(height, width); 

构造一个临时对象,将其丢弃。当前对象的成员变量永远不会被正确初始化。

简化您的代码。移动代码以获取输入数据给调用它的函数。例如,在main中使用它。

int width = -1; 
int height = -1; 

cout << "Aby rozpoczac gre wpisz rozmiar planszy" << endl; 
cout << "Aby uruchomic domyslne romiary zostaw pole puste" << endl; 

cout << "Podaj szerokosc: "; 
// I don't see what i'm typing and i need to press enter twice; 
cin >> width; 

cout << "Podaj wysokosc: "; 
// I don't see what i'm typing too and make my program crash 
cin >> height; 

World w(height, width); 

简化默认construtor(与真正的成员变量替换heightMemberwidthMember):

World::World() : heightMember(0), widthMember(0) {}