골프 구조에 대한 메시지가 표시 될 때 다음 코드를 실행하고 새 줄을 삽입하면 (Enter 키를 누름) 함수에 대한 두 번째 호출은 입력을 요청하지 않고 완료됩니다 나는 다시 진입했다.빈 줄이있는 cin.get() 문제
나는 cin.get(), cin.clear(), cin.ignore (...)를 읽었지만 아무 것도 도움이되지 않는다.
저는 여러 개의 .cpp 파일과 헤더와는 아무런 관련이 없지만 코드를 그대로 사용하고 있다고 확신합니다.
Visual Studio C++ 2010 - Express를 사용하고 있습니다.
미리 도움 주셔서 감사합니다.
헤더 파일 : golf.h
#ifndef GOLF_H
#define GOLF_H
const int Len = 40;
struct golf{
char fullname[Len];
int handicap;
};
int setgolf(golf & g);
void showgolf(const golf & g);
#endif
golf.cpp
#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>
using namespace std;
int setgolf(golf & g){
cout << "Enter a name for the golf structure:" << endl;
if (cin.get(g.fullname,Len)) {
cin.get(); // deals with the '\n' incase the user inputs a valid string
return 1;
}
else{
return 0;
}
}
void showgolf(const golf & g){
cout << "this golf structure contains the following information:" << endl;
cout << "name: " << g.fullname << endl ;
cout << "handicap: " << g.handicap << endl ;
}
주()
#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
golf myGolf;
// check of int setgolf(golf & g);
int answ = setgolf(myGolf); //try to input empty string
showgolf(myGolf);
cout << "the number returned :" << answ << endl ;
answ = setgolf(myGolf); // normal string
showgolf(myGolf);
cout << "the number returned :" << answ << endl ;
return 0;
}
자유롭게 기능하는 ['std :: getline()'] (http://en.cppreference.com/w/cpp/string/basic_string/getline)을 생각해보십시오. – WhozCraig
나는 cin.get()이 Enter 키를 눌렀을 때 생성되는 개행 문자를 사용하지 않는다고 생각합니다. 그래서 당신이 다시 그것을 부를 때마다, 그것은 아무런 입력이없이 거기에서 소비됩니다. 빠른 답변을 원하면 cin.get()을 두 번 호출하여 단일 입력을 가져옵니다. – mostruash
mostruash -> 이미 몇 번 시도해 보았습니다. 불운. WhozCraig -> cin.get()에 익숙해 지려고합니다. *** myGolf를 시작하는 것을 잊었습니다. myGolf = { "some-name", 6}; 그러나 그것은 여기에 관련이 없습니다 – Stasv