Getline not working in switch and cin not taking data properly? Can anyone help?

Hey guys, I am working on a project for my work, but it seems to me that this is not so.

I need to take the model of the model and the years of the car, which specific product is suitable. The program is going to write a file with text in it, which will be the html outline that I need, with important information inserted at the right points to quickly add products to our web page. When I use the switch statement or instructions to separate product categories, the input is messed up and does not allow me to answer one of the questions. I can't just use cin because I need to accept dates that look like "2008 - 2009 - 2010 - 2011"

That's what I have so far! This is a regular routine cin, I tried getline, you could quickly c I started yesterday, and I'm pretty new, so forgive me if this is easy to fix, but I can not find anything.

#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()    
}
+3
source share
1 answer

Change the year [256] to the year of the line;

Change cin → year; to getline (cin, year);

Add the line cin.ignore (); before getline.

The main problem is that the stream operator -> leaves a newline in the stream, so when you try to use getline, it reads an empty line. ignore () chews on a new line and allows you to read the line you expect.

So this should help you.

cin.ignore();
cout << "Please Enter the Years this molding fits" << endl;
getline(cin, year);

You have a few more minor problems, but you’ll find out. Worst of all forgets to end the cycle.

if(getinput=="yes")
{
    cout << "Great, Lets keep working!" << endl;
   //End of If statement
}
else
{
    loop = 0;
    continue;
}
+5
source

All Articles