getline在交换机中不起作用,cin不能正确获取数据吗?有人可以帮忙吗?

| 大家好,我正在为我的工作从事一个项目,但我似乎无法弄清楚。 我需要考虑特定产品适合的汽车的制造型号和使用年限。该程序将编写一个带有文本的文件,该文件将是我需要的html的轮廓,并将关键信息插入正确的位置,以便快速将产品添加到我们的网页中。当我使用switch语句或if语句分隔产品的类别时,输入变得混乱,并且无法让我回答其中一个问题。我不能只使用cin,因为我需要输入类似“ 2008-2009-2010-2011 \”的日期 这是我到目前为止所获得的!这只是常规的
cin
,我尝试过
getline
,您可以很快c从昨天开始,我很新,所以如果这是一个简单的解决方法,请原谅我,但我找不到任何东西。
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void proinfo();

int main()
{
    //Sytem Information
    system(\"TITLE This is a Beta of My Product Information Template Version 0.0.2\");

    //I\'m using this as a welcome screen to inform users of new improvements on patches and other pertinent information to the users
    cout << \"Welcome To My Product Information Template Maker!!! \\n It Is Still In Development\" << endl;
    cout << \"\\n\\nworking features are:\" << endl << \"\\t-None\\n\" << endl;
    system(\"PAUSE\");
    system(\"CLS\");
    proinfo();
}

void proinfo()
{
    //Declare Variables here
    int loop(1), protype;
    char make[256], model[256], year[256];
    string getinput;

    while(loop==1)
    {
        //This is the first menu the user sees where they have to choose what type 
        //of products users will be adding
        system(\"CLS\");
        cout << \"Welcome to My Product Information Template Version 0.0.0\" << endl;

        cout << \"Please select the number of the product\" << endl;
        cout << \"type you will be ading!\\n\" << endl;

        cout << \"1.Fe - Fe2 Moldings\" << endl;
        cout << \"2.CF - CF2 Moldings\" << endl;

        cout << \"What would you like to do? \";

        cin >> protype;

        if(protype==1)
        {
            //FE - FE2 Molding
            system(\"cls\");

            cout << \"You have selected\" << endl;
            cout << \"Fe - Fe2 Moldings\\n\" << endl;

            cout << \"Please Enter the Make of the molding\" << endl;
            cin >> make;

            cout << \"Please Enter the Model of the molding\" << endl;
            cin >> model;

            cout << \"Please Enter the Years this molding fits\" << endl;
            cin >> year;

            cout << \"You have just created a template for a(n)\" << year << \" \" << make << \" \" << model << endl;
            cout << \"Check My Documents For a file called Fe-Fe2template.txt\" << endl;

            //Asks to quit or restart
            cout << \"Would you like to make another tempalte?\" << endl;

            cin >> getinput;

            if(getinput==\"yes\")
            {
                cout << \"Great, Lets keep working!\" << endl;
                //End of If statement
            }

            system(\"PAUSE\");
        }

        if(protype==2)  
        {
            //CF - CF2 Molding
            system(\"cls\");

            //Asks to quit or restart
            cout << \"Would you like to make another tempalte?\" << endl;

            cin >> getinput;

            if(getinput==\"yes\")
            {
                cout << \"Great, Lets keep working!\" << endl;
                //End of If statement                   
            }

            system(\"PAUSE\");

            //End of Protype Switch Statement               
        }

        //End of while loop              
    }
    //End of Proinfo()    
}
    
已邀请:
将year [256]更改为字符串year; 更改cin >>年;到getline(cin,年); 添加行cin.ignore();在热线之前。 您的主要问题是流运算符>>将换行符留在流中,因此当您尝试使用getline时,它将读取一个空行。 ignore()将检查换行符,并让您阅读所需的字符串。 因此,这应该可以助您一臂之力。
cin.ignore();
cout << \"Please Enter the Years this molding fits\" << endl;
getline(cin, year);
您还有其他一些小问题,但可以解决。最糟糕的是忘记终止循环。
if(getinput==\"yes\")
{
    cout << \"Great, Lets keep working!\" << endl;
   //End of If statement
}
else
{
    loop = 0;
    continue;
}
    

要回复问题请先登录注册