0

this is my code:

string getFileContents(istream& file_contents){
    string line;
    getline(file_contents, line);

return line;
}

project read_project(istream& in){
    project newproject;
    while(cin){
        cout << "Enter your project name: ";
        newproject.proname = getFileContents(cin);

        cout << "Enter a description: ";
        newproject.prodesc = getFileContents(cin);

        cout << "How long until deadline: ";
        newproject.protime = getFileContents(cin);

    promap.insert(pair<string, project> ( newproject.proname , newproject));
    cout << endl << "You created a new project: " << newproject.proname
    << endl << "Project description: " << newproject.prodesc ;
}
}



int main(){

string inputcmd;

while (cin){
cout << "TYPE A COMMAND" << endl;   
cin >> inputcmd;

if (inputcmd == "makenew")
    cout << "MAKING NEW PROJECT";
    read_project(cin);
}
return 0;

my objective is to successfully store a project type in a map. the user first enters a 'command' "makefile", this calls a read_project function, which both operate with cin as a parameter. the problem is when i run the code it gives strange outcome, like the first time i type makefile and it skips the "enter your project name: " and does right for the "enter your project description". why does it do that? on all the succeeding loops it works correctly, first asking for the project name and waiting for the input.

notamathwiz
  • 225
  • 1
  • 5
  • 14

1 Answers1

2

When you do the initial input in the main function, it reads a string but lease the newline in the buffer. So when you later call std::getline it reads that newline as an empty line.

You can overcome it by doing e.g.

cout << "TYPE A COMMAND" << endl;   
cin >> inputcmd;

// Skip to the end of the line, and remove the newline from the input buffer
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// The rest of the code...

Also, you shouldn't do e.g. while (cin) ..., as the eof and error flags will not be set until after an input operation fails. That means that if you press the EOF key (CTRL-Z or CTRL-D (depending on system)) at the first input prompt, you will not notice it until after you attempted to read all input, which will fail but you don't check for it.

Instead do e.g.

cout << "TYPE A COMMAND" << endl;
while (cin >> inputcmd)
{
    ...

    cout << "TYPE A COMMAND" << endl;
}

A simple and complete example showing the techniques I described above:

#include <iostream>
#include <string>

void read_project()
{
    std::string name, descr, deadline;

    std::cout << "Project name: ";
    std::getline(std::cin, name);

    std::cout << "Project description: ";
    std::getline(std::cin, descr);

    std::cout << "Project deadline: ";
    std::getline(std::cin, deadline);

    std::cout << "Project entered:\n"
              << "    Name       : " << name << '\n'
              << "    Description: " << descr << '\n'
              << "    Deadline   : " << deadline << '\n';
}

int main()
{
    std::string cmd;

    std::cout << "Enter command: ";
    while (std::cin >> cmd)
    {
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        if (cmd == "makenew")
            read_project();

        std::cout << "Enter command: ";
    }
}

Note: You might want to add extra error checking for the std::getline calls as well.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621