今日免费次数已满, 请开通会员/明日再来

3
我需要获取适用于特定产品的汽车品牌、型号和年份信息。该程序将编写一个文本文件,其中包含HTML的大纲。我需要将关键信息插入到正确的位置,以便能够快速添加产品到我们的网页中。当我使用switch语句或if语句来分离产品类别时,输入会出现混乱,并且不允许我回答其中一个问题。我不能仅使用cin,因为我需要采取看起来像“2008-2009-2010-2011”这样的日期。
以下是我目前的代码!这只是常规的cin,我已经尝试过getline,您可以很快地查看。我昨天才开始学习,所以如果这是一个简单的修复,请原谅我,但我找不到任何东西。
#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()    
}

1
那么你的问题是什么?程序在做什么与你想要它做的不同? - John Zwinck
1
你能提供一个样例输入以及你期望的可能输出/行为吗?此外,你的程序将进入无限循环,因为你没有从 while(1) 循环中正确退出。 - Purnima
1
放弃使用 DevC++ - 它不再被开发了,并且许多人会说它一开始就不应该被开发。转而使用 Code::Blocks。 - user2100815
1个回答

5

将变量 year[256] 改为字符串类型的 year;

将 cin >> year; 改为 getline(cin, year);

在 getline 前添加一行 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;
}

谢谢大家的帮助,我要试一下。如果这是个愚蠢的问题,我很抱歉,但为什么你必须终止循环呢?通常情况下,我只是在实验时选择“是”,然后用“x”退出,所以我没有看到需要终止循环。这样做会导致内存泄漏吗? - Rob
我不确定你所说的“用x退出”是什么意思 - 是关闭终端窗口吗? - Duck
是的,我不知道技术术语,我只是使用窗口右上角的X。 - Rob
是的,那不是正确结束程序的方式。它会发送一个信号来终止你的程序,除非你正确处理它,否则它不会以有序的方式保存数据等。在循环中放置一个退出语句并正确执行。 - Duck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接