【发布时间】:2014-09-24 07:49:50
【问题描述】:
我有这个 C++ 简单程序;
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
using std::getline;
#include <string>
using std::string;
struct Repository
{
string name;
string path;
string type;
string command;
};
int main()
{
Repository rp;
cout << "\nEnter repo name: ";
cin >> rp.name;
cout << "Enter repo path: ";
cin >> rp.path;
cout << "Enter repo type: ";
cin >> rp.type;
cout << "Enter repo command: ";
getline(cin, rp.command);
cout << "\nRepository information: " << endl;
cout << rp.name << "\n" << rp.path << "\n" << rp.type << "\n" << rp.command << endl;
return 0;
}
当执行到 getline(cin, rp.command) 时,程序只打印“Enter repo command:”并跳过 getline(cin, rp.command) 行,这样用户就没有时间响应了。可能是什么问题?
【问题讨论】:
-
你的结构是什么样的?
-
我们需要查看
Repository的定义。 -
需要更多细节。崩溃怎么办?
.command是什么类型的? -
@PaulR,添加了结构定义。
-
上面的代码不应该崩溃,你的问题可能在别处,发布一个显示崩溃的最小可编译示例。
标签: c++