일부 C++을 연습하면서 책에서 일부 코드를 실행했습니다. break seams와 함께 if 문을 조금 쓸 필요가 없습니다.if 문 입력 (cin)
if (! (cin >> dstep)) break;
이 솔기는 나에게 해킹이되어서 간단히 cin >> dstep을 사용하여 if 나 break가 프로그램을 잘 실행합니다. 이견있는 사람?
int main()
{
using namespace VECTOR;
srand(time(0)); //seed random-number generator
double direction;
Vector step; //creates default object
Vector result(0.0, 0.0); //
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep)) //if NOT inputing into dstep THEN break/means if INPUTING is TRUE, keep going and don't break out of loop
break;
//cin >> dstep // why not just use this?
while (result.magval() < target)
{
direction = rand() % 360;
step.set(dstep, direction, 'p'); //sets the values dstep and direction based on the form; in this case 'p'
result = result + step;
steps++;
}
cout << "After " << steps << " steps, the subject "
"has the following location:\n";
cout << result << endl;
result.polar_mode();
cout << " or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval()/steps << endl;
steps = 0;
result.set(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.get();
return 0;
}
'break'없이 '길이 입력'프롬프트에 대해 유효하지 않은 길이 (문자열 일 수 있음)를 입력하면 어떻게됩니까? – Naveen
'Q'또는 'P'또는 ant int가 아닌 문자를 입력하면 주 루프가 종료되고 "bye"가 출력됩니다. –