2014-09-26 1 views
2

이름을 입력 할 때 코드에서 전체 이름을 사용할 수 있기를 바랍니다. Cin이 공백을 허용하도록하려면 어떻게해야합니까?cin은 공백을 허용합니까?

공백으로 이름을 입력 할 때 결과는 주제 이름의 다음 입력이 성이라고 가정합니다. 이 문제를 어떻게 해결할 수 있습니까?

#include<iostream> 
using namespace std; 

int main() 
{ 
    float mark1, mark2, mark3, mark4; 
    char subject1[25], subject2[25], subject3[25], subject4[25]; 
    char name; 
    float average; 

    cout << "Please enter your name"<< endl; 
    cin >> name; 

    cout << "Please enter the first subject name"<< endl; 
    cin >> subject1; 

    cout << "Please enter the mark for " <<subject1<< endl; 
    cin >>mark1; 

    cout << "Please enter the second subject name"<< endl; 
    cin >> subject2; 

    cout << "Please enter the mark for " <<subject2<< endl; 
    cin >>mark2; 

    cout << "Please enter the third subject name"<< endl; 
    cin >> subject3; 

    cout << "Please enter the mark for " <<subject3<< endl; 
    cin >>mark3; 

    cout << "Please enter the fourth subject name"<< endl; 
    cin >> subject4; 

    cout << "Please enter the mark for " <<subject4<< endl; 
    cin >>mark4; 

    average=(mark1+mark2+mark3+mark4)/4; 

    cout << name << "'s " << subject1<< " mark is " <<mark1 <<", " << subject2<< " mark is " << mark2 <<", " <<subject3 <<" mark is " <<mark3 <<", " <<subject4<< " mark is " <<mark4 <<". His/her average is " <<average<< endl; 

    cout << endl; 

    system("PAUSE"); 
    return 0; 
} 

답변

1

에 한번 사용 예를 들어 getline(cin, <variable name>)

: getline reference

을 :

string name; 
getline(cin, name); 
cout << name; 

하는 생각의 getline을 사용하는 방법에 대한 읽을 수 있는지 확인