C++ 调用函数时,while循环跳过一步

C++ 调用函数时,while循环跳过一步,c++,function,loops,while-loop,cin,C++,Function,Loops,While Loop,Cin,这是我的代码: 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 n

这是我的代码:

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;
string getFileContents(istream&file\u内容){
弦线;
getline(文件内容,行);
回流线;
}
项目阅读项目(istream&in){
新项目;
while(cin){

cout当您在
main
函数中进行初始输入时,它会读取一个字符串,但会在缓冲区中租用换行符。因此,当您稍后调用
std::getline
时,它会将该换行符读取为空行

你可以通过做某事来克服它

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...

一个简单而完整的示例显示了我上面描述的技术:

#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: ";
    }
}
#包括
#包括
void read_项目()
{
std::字符串名称、描述、截止日期;

std::cout当我执行(cin>>inputcmd)时,由于某种原因,它会在这个while循环中被卡住,当我执行cin.ignore(…)调用时,它会给出很多乱七八糟的错误output@notamathwiz在我的答案中添加了一个完整的、有效的示例来说明我的意思。更简单的方法是使用
getline
来阅读命令。
#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: ";
    }
}