0

I'm having some trouble with my while looping breaking its cycle. If I answer the problem correctly the first time, it allows me to proceed through the program. However, if I use an integer it loops false, even if I answer it correctly in the loop it will not exit and save the string value. The thing is, is that I don't want a person entering an integer in this question, so I check the line for any integers.

#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;

}
4
  • 4
    contains_non_alpha is not set inside the loop. It's not going to change just by magic... Commented Jun 22, 2016 at 2:56
  • The boolean will never change thus the loop will not exit Commented Jun 22, 2016 at 2:56
  • In this case you might want to use std::all_of, simply passing std::isalpha as the predicate. Instead of std::find_if I mean. Commented Jun 22, 2016 at 2:59
  • You might save yourself even more trouble by having a std::set<std::string> of known colour names, then using colour_names.count(carColur) to decide whether to loop asking for a valid colour name.... Commented Jun 22, 2016 at 4:01

1 Answer 1

2

As told by the comments you don't actually do the check in the loop, which means the value of contains_non_alpha will never change.

The simple solution is to do the check itself as part of the condition of the loop, no need for a temporary variable.

Using the std::all_of function as in my comment you could then do e.g.

while (!std::all_of(std::begin(carColor), std::end(carColor), std::isalpha))
{
    ...
}

You could also use a do while loop like this

void Options()
{
    std::string carColor;

    do
    {
        std::cout << "Please type a color in for your car (letters only): ";
        std::cin >> carColor;
    } while (!std::all_of(...));

    int carNumber;
    // ... rest of code...
}
Sign up to request clarification or add additional context in comments.

2 Comments

....I feel extremely silly for spending hours, upon hours trying to figure out how to check a string value for characters only..... Thanks a ton Joachim!
@NathanielPetterson Everything is easy once you know how it's done. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.