Search code examples
c++dev-c++

Why getline does not work in a switch, and cin does not take the data appropriately?


I need to take in the make model and years of the car the particular product fits. The program is going to write a file with text in it that will be an outline of the html. I need with the crucial information inserted into the correct spots to quickly be able to add products to our web page. When I use a switch statement or if statements to separate the categories of the products, the input gets messed up and does not let me answer one of the questions. I can't just use cin because I need to take dates that look like "2008 - 2009 - 2010 - 2011".

Here is what I've got so far! This one is just regular cin, I have tried getline you could quickly c I started yesterday and I'm pretty new so forgive me if this is an easy fix, but I cant 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()    
}

Solution

  • Change year[256] to string year;

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

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

    Your main problem is that the stream operator >> leaves the newline in the stream so when you try to use getline it reads an empty line. ignore() will chew up the newline and let you read the string you expect.

    So this should get you on your way.

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

    You have some other small problems but you'll figure them out. The worst is forgetting to terminate the loop.

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