0
這是我的主要:爲什麼我的程序跳過步驟
#include<iostream>
#include<limits>
#include"header.h"
#include<map>
#include<string>
#include<sstream>
using namespace std;
int main(){
bool setflag = true;
string inputcmd;
while(setflag){
cout << "TYPE A COMMAND" << endl;
cin>> inputcmd;
if (inputcmd == "make"){
cout << "MAKING NEW PROJECT" << endl;
get_project(cin);
}
else if (inputcmd == "retrieve"){
cout << "RETRIEVING YOUR PROJECT" << endl;
}
else if (inputcmd == "quit")
{ setflag = false; }
else { cout << "invalid input please try again"; }
}
return 0;
}
這是我的結構的代碼:
using namespace std;
typedef struct{
string proname;
string prodesc;
string protime;
}project;
這是我的標題代碼(在我的所有功能定義):
#include<iostream>
#include"project_struct.cpp"
#include<map>
project current;
map<string, project> promap;
string getFileContents(istream& file_contents){
string line;
getline(file_contents, line);
return line;
}
void get_project(istream& in){
project newproject;
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 << endl;
}
我questio n是這個,由於某種原因,當我鍵入make時,在我的cmd控制檯輸入它,創建一個新項目,但跳過項目的名稱,換句話說它不請求我的項目的名稱,它直接要求我的項目描述。這是爲什麼?我該如何解決它?
搜索網站「getline跳過」。我保證你會找到答案。 – chris
我們剛剛遇到了類似的問題(http://stackoverflow.com/questions/20692860/why-is-my-small-c-code-behaving-unexpectedly)遇到同樣的問題。用'>>'提取命令會在輸入流中留下換行符。然後,當您嘗試爲項目名稱「getline」時,它會在輸入中看到新行,並提取一個空字符串。 –
很久以前有人推薦使用cin.ignore(std :: numeric_limits :: max(),'\ n');但是這留下了相同的問題,唯一的區別是我必須輸入每個命令兩次才能註冊 –
notamathwiz