0
bool alphabeticString(string word)
{
for (int i = 0; i < word.length(); i++)
{

    if (isalpha(word[i])==true)
    {
        cout << word[i] << " is alphabetical\n";
        
    }
    
    if (isalpha(word[i])==false) {
        cout << "Please retry a character is not alphabetical\n";
        cin >> word;
    }

}
return true;
}

Trying to make the word variable become the new loop if a character isn't alphabetical

2
  • You will have to reset counter of the loop. i = -1;. Commented Oct 30, 2020 at 8:09
  • Btw. you don't need two if-s. You could easily do if ... else statement here. Commented Oct 30, 2020 at 8:33

3 Answers 3

1

I suggest splitting up the user input part from the checking part.

You could also use the standard library function std::all_of to check that all characters in the string fulfills the requirements.

Example:

#include <algorithm> // std::all_of
#include <cctype>    // std::isalpha
#include <iostream>
#include <stdexcept> // std::runtime_error
#include <string>

// the testing part
bool alphabeticString(const std::string& word) {
    return
        std::all_of(word.begin(), 
                    word.end(),
                    [](char ch) { // a lambda function for testing a char
                        return std::isalpha(static_cast<unsigned char>(ch)); 
                    }
        );
}

// the input part
std::string prompt() { 
    std::string word;

    while(std::cout << "Enter a word: " &&
          std::cin >> word &&
          not alphabeticString(word))
    {
        std::cout << "A character is not alphabetical, please try again.\n";
    }

    // cin is a bad state - extraction of a string failed
    if(not std::cin) throw std::runtime_error("utter failure");

    return word;
}

int main() {
    try {
        std::string word = prompt();
        std::cout << "You wrote: " << word << '\n';

    } catch(const std::exception& ex) {
        std::cerr << "Exception: " << ex.what() << std::endl;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could always call alphabeticString function recursively and return it like this:

if (isalpha(word[i])==false) {
    cout << "Please retry a character is not alphabetical\n";
    cin >> word;
    return alphabeticString(word); 
}

Comments

0

You could split the method:

bool isAlphabetic (std::string word)
{
   bool return_val = true; 
   for (int i = 0; i < word.length(); i++)
   {
      if (!isalpha(word[i]))
      { 
         return_val = false; 
         break;
      }
   }
   return return_val; 
}
std::string string ();
string << cin;
while (!isAlphabetic(string))
{
   cout << "Word is not alphabetic!" << endl;
   string << cin; 
}

1 Comment

Have edited my comment a lot, but now is should work correctly.

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.