내 반복 루핑에서 문제가 발생합니다. 처음으로 문제에 올바르게 대답하면 프로그램을 진행할 수 있습니다. 그러나 정수를 사용하면 false로 반복됩니다. 올바르게 루프에 응답해도 종료되지 않고 문자열 값을 저장합니다. 문제는이 질문에서 사람이 정수를 입력하는 것을 원하지 않는다는 것입니다. 따라서 정수에 대한 행을 확인합니다.부울은 while 루프와 함께 작동하지 않습니다.
#include "stdafx.h"
#include "stdio.h"
#include <iostream>
#include <ctime>
#include <string>
#include <time.h>
#include <algorithm>
#include "ThreeWayRace.h"
#include <cctype>
#include <functional>
using namespace std;
void Options()
{
string carColor;
int carNumber;
int s;
cout << "Please type a color in for your car: ";
cin>>carColor;
bool contains_non_alpha
= std::find_if(carColor.begin(), carColor.end(),
std::not1(std::ptr_fun((int(*)(int))std::isalpha))) != carColor.end();
while (contains_non_alpha == true)
{
cout << "Please enter letters only. ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin>>carColor;
}
'contains_non_alpha' 같은
do while
루프를 사용할 수 있습니다. 그것은 마법에 의해서만 변하지 않을 것입니다 ... – John3136부울은 절대로 변경되지 않으므로 루프가 종료되지 않습니다 – Li357
이 경우에는 ['std :: all_of'] (http://en.cppreference.com/w/cpp/algorithm/all_any_none_of), 간단하게'std :: isalpha'를 술어로 전달합니다. 'std :: find_if' 대신에 말입니다. –