Home > OS >  Using cin.ignore AFTER cin.get to ignore extra inputs
Using cin.ignore AFTER cin.get to ignore extra inputs

Time:03-30

The user is prompted to "enter a middle initial". What happens if they enter a space, full name, or maybe a letter followed by a period '.' ? How can we modify the program to handle this using cin.ignore? This is the code I currently have: I commented out the area I'm having trouble with.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string fname, lname;
    char MI;
    cout << "Please enter your first name: ";
    cin >> fname;                                           
    cout << "Please enter your middle initial: ";
    cin.ignore(1, '\n');                                    
    cin.get(MI);                                        
    cout << "Please enter your last name: ";
      //cin.ignore('\n')
    cin >> lname;
    cout << "Your name is " << fname << " " << MI << " " << lname << endl;
    return 0;
}

When I have this other cin.ignore in it still doesn't do anything and the last name reads the extra inputs. I've tried adding a number of characters to read and it still doesn't fix the problem. When I run it it just skips the input for last name. I also tried changing the last name input to getline but if still didn't do anything.

CodePudding user response:

You can just use std::getline and std::istringstream:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string fname, lname;
    std::string MI;

    std::cout << "Please enter your first name: ";
    std::getline(std::cin, fname);
    std::istringstream iss(fname);
    iss >> fname;

    do
    {
        std::cout << "Please enter your middle initial: ";
        std::getline(std::cin, MI);

    } while (MI.size() != 1);

    std::cout << "Please enter your last name: ";

    std::cin >> lname;
    std::cout << "Your name is " << fname << " " << MI << " " << lname << std::endl;
    return 0;
}

Here for fname I have used std::getline to get user input and then I've used std::istringstream to get only one word of the input.

For MI I have made it a string and until and unless the user doesn't provide a single character, the program doesn't continue.

And the lname part is the same.

CodePudding user response:

You should change:

cin.ignore(1, '\n');                                    
cin.get(MI);                                        

To simply:

cin >> MI;

Let operator>> ignore any white space, including line breaks, between the first name and the middle initial.

After reading MI, you can then use the following to ignore everything up to the next input:

cin.ignore(numeric_limits<streamsize>::max(), '\n');

Try this:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main ()
{
    string fname, lname;
    char MI;
    cout << "Please enter your first name: ";
    cin >> fname;
    cout << "Please enter your middle initial: ";
    cin >> MI;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Please enter your last name: ";
    cin >> lname;
    cout << "Your name is " << fname << " " << MI << " " << lname << endl;
    return 0;
}
  • Related