2013-12-19 2 views
0

내 주 :이 내 헤더 코드 (내 모든 기능 정의가있는 곳)왜 내 프로그램이 단계를 건너 뛰는 것은이

using namespace std; 

typedef struct{ 
    string proname; 
    string prodesc; 
    string protime; 
}project; 

입니다 :

#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; 
} 

이 내 구조체 코드

#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은 어떤 이유에서인지, 입력 할 때 어떤 이유로 cmd 콘솔 입력에 새 프로젝트를 만들지 만 프로젝트 이름을 건너 뜁니다. 즉, 프로젝트 이름을 요청하지 않고 직접 요청합니다. 내 프로젝트 설명. 그게 왜? 어떻게 해결할 수 있습니까?

+2

"getline skipping"사이트를 검색하십시오. 나는 당신이 대답을 찾을 것이라는 것을 보장합니다. – chris

+0

우리는 방금 [비슷한 질문] (http://stackoverflow.com/questions/20692860/why-is-my-small-c-code-behaving-nexpectedly) 같은 문제가 발생했습니다. '>>'명령을 추출하면 입력 스트림에 개행 문자가 남습니다. 그런 다음 프로젝트 이름에 대해'getline '을 시도하면 입력에 남아있는 개행을보고 빈 문자열을 추출합니다. –

+0

오래 전 누군가 cin.ignore (std :: numeric_limits :: max(), '\ n'); 하지만 그것은 동일한 문제를 남겨 둡니다. 유일한 차이점은 각 명령에 두번 입력해야한다는 것입니다. – notamathwiz

답변

0

나는 그것을 이해했다고 생각합니다. 내가 바로 내 main()에서 cin>>inputcmd;

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

을 넣으면 문제가 해결 보인다.

모두 해피 코딩 :)