0

I am writing a program that is to convert text that a user gives. I have tried this method by itself in a test program and it works perfectly; however when I try to implement it into the larger program, the user cannot give the program an input to store. The relevant code is as follows:

int main()
{
    string str = "NULL";
    int mappings = 0;
    readMappings(mappings);
    receiveInput(str);
    displayInput(mappings, str);
    return 0;
}
void readMappings(int &count)
{
    ifstream readMappings;                                                  // Creates the function "readMappings"
    string filename;                                                        // Generates the filename variable to search for mapping document
    cout << "Enter the filename of the mapping document: ";
    cin >> filename;                                                        // User enters filename
    readMappings.open(filename);                                            // "readMappings" function opens the given mappings document
    while (!readMappings.is_open())
    {
        cout << "Unsble to open file. Please enter a valid filename: ";     // If the user enters an invaled filename, the program will ask again
        cin >> filename;
        readMappings.open(filename);
    }   
    if (readMappings.good())                                                // Mapping document is successfully opened
    {
        readMappings >> count;                                              // Reads first line
    }
    readMappings.close();                                                   // If everything fails in this function, the document will close
}
void receiveInput(string &input)
{
    char correctness;
    do {
        cout << "\nPlease enter the text you would like to be converted to NATO:\n";
        getline(cin, input);
        cout << "You are about to convert: \"" << input << "\".\nIs this correct? (Y/N)" << endl;
        cin >> correctness;
    } while (correctness == 'N' || correctness =='n');
}

I thought it may have been the program waiting for another input from the user so I added a variable I assumed it would already fill, but it did not fix my solution. In my opinion, the problem is in the receiveInput function, but I could be wrong. Thanks in advance for any assistance.

Also, I am using function prototypes with correct reference variables.

  • Classic example of [mixing getline and stream extraction](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Revolver_Ocelot Feb 07 '16 at 19:19

1 Answers1

0

I see two problems:

1) You're not checking for an EOF condition, after invoking std::getline().

2) You are mixing together both std::getline and the >> operator. Now, there's actually nothing technically wrong with that, but both std::getline and the >> operator have very nuanced semantics, when it comes to error checking and input consuming, that you need to get 100% right, in order to correctly use them together.

Just replace your usage of the >> operator with std::getline, so you're using std::getline exclusively, and make sure to check for fail() or eof(). You will find plenty of examples of how to correctly check for end of file and failure conditions, with std::getline, here on stackoverflow.com.

Sam Varshavchik
  • 84,126
  • 5
  • 57
  • 106
  • Can explain in more detail by what you mean by _Just replace usage of the `>>` operator with `std::getline`_? Are you referring to the `getline(cin, input);` line? – Aaron T Maynard Feb 07 '16 at 20:29
  • I typically mean what I say. When I say replace "the >> operator" with std::getline, I am, obviously, referring to the >> operator, and not the existing call to std::getline(). You only have one function in the shown code that uses both, and that's what I was referring by "mixing together both std::getline and the >> operator". – Sam Varshavchik Feb 07 '16 at 22:45