即使我做过>>也不允许我输入输入CIN

时间:2014-10-27 21:48:13

标签: c++

#include <fstream>
#include <string>
#include <Windows.h>
#include <iostream>


using namespace std;
double c;
int axis, ar;
string answer, lamb;




/*Aspect ratio calculation form*/
/*Y axis is divided by disired aspect ratio, cinema wise the default aspect ratio is 2.35, so       It's divided by that number and we'll get our x RESO*/
/*fntMentaL*/
void aspectRatio()
{
cout << "Enter your Y Axis" << endl;
cin >> axis;
cout << "Enter your desired aspect ratio, if you want a list of known aspect ratios, type Y" <<   endl;
if (answer == "y" || answer == "Y")
{
    cout << "2.39 / A current widescreen cinema standard" << endl;
    cout << "1.77 / HD video std.; U.S. digital broadcast TV std." << endl;
    cout << "1.6667 / The golden ratio" << endl;
    cout << "Now enter your aspect ratio: " << endl;
    cin >> ar;
    c = axis / ar;
    cout << "Your X resolution should be : " << c << endl;
    cout << "Do you want me to set a game command to set this resolution ? (Y/N)" << endl;
    cin >> lamb;
    if (lamb == "y" || lamb == "Y")
    {

        ofstream myfile;
        myfile.open("Resolution.txt");
        myfile << c;
        myfile.close();

    }
    else
    {
        cout << "It didin't work" << endl;

    }




}
else
{
    cin >> ar;
    c = axis / ar;
    cout << "Your X resolution should be : " << c << endl;
    cout << "Do you want me to set a game command to set this resolution ? (Y/N)" << endl;
    cin >> lamb;
    if (lamb == "y" || lamb == "Y")
    {

        ofstream myfile;
        myfile.open("Resolution.txt");
        myfile << c;
        myfile.close();

    }
    else
    {
        cout << "It didin't work" << endl;

    }
}


}

所以是的,我在运行它时没有任何问题,但是在最后一个问题上,你是否要我设置这个分辨率,当这个人需要回答Y或N时,它甚至不让我回答,有什么想法吗?程序需要复制并粘贴人员输入的X分辨率,但是当我甚至无法启动操作时它不能。

1 个答案:

答案 0 :(得分:0)

我想你忘了cin&gt;&gt;回答,所以当你检查答案是否等于y时,你会得到未定义的行为(因为答案从未设置过)。

另外,填充ar的else块有点尴尬 - 如果用户输入y以外的内容,它实际上会提示用户输入新内容。但是,它不会将先前的输入传递到ar。如果这是你的意图,它应该看起来更像这样,记住你真的应该对输入做一些类型检查:

/*Aspect ratio calculation form*/
/*Y axis is divided by disired aspect ratio, cinema wise the default aspect ratio is 2.35, so       It's divided by that number and we'll get our x RESO*/
/*fntMentaL*/
void aspectRatio()
{
cout << "Enter your Y Axis" << endl;
cin >> axis;
cout << "Enter your desired aspect ratio, if you want a list of known aspect ratios, type Y" <<   endl;
cin >> answer; //set answer to something!
if (answer == "y" || answer == "Y")
{
    cout << "2.39 / A current widescreen cinema standard" << endl;
    cout << "1.77 / HD video std.; U.S. digital broadcast TV std." << endl;
    cout << "1.6667 / The golden ratio" << endl;
    cout << "Now enter your aspect ratio: " << endl;
    cin >> ar;
    c = axis / ar;
    cout << "Your X resolution should be : " << c << endl;
    cout << "Do you want me to set a game command to set this resolution ? (Y/N)" << endl;
    cin >> lamb;
    if (lamb == "y" || lamb == "Y")
    {

        ofstream myfile;
        myfile.open("Resolution.txt");
        myfile << c;
        myfile.close();

    }
    else
    {
        cout << "It didin't work" << endl;

    }




}
else
{
    // this is dangerous, you have no idea what 'answer' is, but I gather this is homework,
    // where 'garden path only' type design is OK...
    c = axis / answer; 
    cout << "Your X resolution should be : " << c << endl;
    cout << "Do you want me to set a game command to set this resolution ? (Y/N)" << endl;
    cin >> lamb;
    if (lamb == "y" || lamb == "Y")
    {

        ofstream myfile;
        myfile.open("Resolution.txt");
        myfile << c;
        myfile.close();

    }
    else
    {
        cout << "It didin't work" << endl;

    }
}


}