C++에서 사용자 문자열을 가져 오는 코드를 작성했습니다. 문제가 어디에 왜함수를 사용하여 C++ 문자열 입력
는 입력 섹션은 건너Enter choice: 1
Enter input:
Enter choice:
나도 몰라 : 여기에 코드를 위의 코드의
#include<iostream>
#include<string>
using namespace std;
string input;
void enterstring()
{
cout<<"\nEnter input: ";
getline(cin,input);
}
void displaystring()
{
cout<<"\nyour string is "<<input<<endl;
}
{
int main()
{
int choice=0;
while(choice!=3)
{
cout<<"Enter choice: "<<;
cin>>choice;
switch(choice)
{
case 1: enterstring();
break;
case 2: displaystring();
break;
case 3: cout<<"\nQuit";
break;
default: cout<<"\ninvalid choice try again";
break;
}
return 0;
}
출력이다. 논리가 잘못되었거나 구문에 문제가있는 것입니다. while 루프 등을 사용하지 않고 함수를 호출하면 정상적으로 작동하지만이 경우 작동하지 않습니다. 도와주세요.
는'CIN >> 입력 사용해보십시오하며 program.'의 지정된 시작이다 주요 라는 전역 함수를 포함한다 enterString() 함수에서' – Shashwat
'프로그램을 - -ISO/IEC 14882-2011 3.6.1 p1 – nurettin
@Shashwat은 문자열을 쓸 때 cin >> 입력을 사용합니다. "세계는 이기적인 사람들로 가득합니다"라고 입력하면 무한 루프가됩니다. 이것을 피하기 위해 나는 getline을 사용했다. –