Alex
Alex

Reputation: 47

C++ how to cout once instead of multiple times in a for loop

void f3(string x){
    for (int i=0; i < x.length(); i++){

        if ('A'<=x[i] && 'z'>=x[i]){

            cout << "English Alphabet" << endl;
        }
        else {
            cout << "NOT English Alphabet" << endl;
            break;
        }
    }
}

how to get only one result (cout)? like "English Alphabet" instead of 4 times for "abcd".

examples:

gimme a string:
abcd
English Alphabet
English Alphabet
English Alphabet
English Alphabet

or

gimme a string:
abc!
English Alphabet
English Alphabet
English Alphabet
NOT English Alphabet

and if I input space the program does not work. How could I fix it? Thanks for the help!

Upvotes: 0

Views: 5249

Answers (5)

A L
A L

Reputation: 1

This is very simple and logical way to execute a line of code once:

int x=1
if (x==1)
{
   //your cout-code here;
   x++; 
}  

x becomes 2, so next time it won't be executed

Upvotes: 0

ogabrielp
ogabrielp

Reputation: 89

I am not sure if I got what you want, and I do not know C++, but something that may help you is using a boolean to define whether or not the input has non-English characters in it. You would define it as true from the beginning, and as soon as you find a character that isn't an English character, you set it to false. When you step out of the for loop, you use an if to check if your input has only English characters. It would be something like this:

void f3(string x) {
    bool isEnglish = true;

    for (int i=0; i < x.length(); i++){

        if ('A'>x[i] || 'z'<x[i]){
            isEnglish = false;
            break;
        }
    }

    if (isEnglish) {
        cout << "English Alphabet" << endl;
    } else {
        cout << "NOT English Alphabet" << endl;
    }
}

If you input space the program should run OK, but it will always return "not english alphabet" for it. That's because the test you're making takes into consideration if the current character of the input is between characters A and z of the ASCII character space. See the image below:

enter image description here

See how space is character number 32, which is smaller than 'A', and therefore, will not enter your if block, and fall inside the else, thus returning 'NOT English alphabet'. Numbers, the @ sign, the & sign and everything before 'A' and everything after 'z' will also fail the test. Also, brackets ('[' and ']') will pass as English alphabet input.

If you want to only include letters, your if should look like:

if (x[i] < 'A' || (x[i] > 'Z' && x[i] < 'a') || x[i] > 'z')

If you want to include spaces, add another &&:

if ((x[i] < 'A' || (x[i] > 'Z' && x[i] < 'a') || x[i] > 'z') && x[i] != ' ')

The same logic can be used to add any characters to your verification.

Good luck!

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490138

Since you've tagged this as C++, I'm going to assume you're prefer a solution written in real C++.

First, I'd look up std::isalpha. It's defined specifically to determine whether a character is alphabetic1.

Second, I'd look up std::all_of and std::any_of. They're defined to determine whether any/all of a collection fits a particular requirement.

Putting those together, you could do something like this:

if (!std::all_of(x.begin(), x.end(), 
                 [](unsigned char c) { return std::isalpha(c); })
    std::cout << "Not ";

std::cout << "English alphabet\n";

1. Note that the result of isalpha isn't necessarily based on the English alphabet. Rather, it's based on the the current locale, so with (for example) a French locale, it would say that not only was an "a" alphabetic, but also that "â" and "à' were alphabetic as well (which your test would not). Likewise, for a German locale it would say "ß" was alphabetic (and so on). The default locale is named "C"; for things like determining whether something is alphabetic, it follows the English alphabet.

Upvotes: 1

Raindrop7
Raindrop7

Reputation: 3911

You can declare a local Boolean variable so as soon as non-English detected the loop breaks immediately setting the the variable to false.

The matter is trickier: Checking for non-English characters:

void f3(string x){
    bool IsEng = true;
    for(int i = 0; i < x.length(); i++){
        if ( !( x[i] >= 'A' && x[i] <= 'z') ){
            IsEng = false;
            break;
        }
    }

    if(IsEng)
        cout << "English Alphabet" << endl;
    else
        cout << "Not English Alphabet" << endl;
}

Upvotes: 0

Neb
Neb

Reputation: 2280

Here two solutions:

With a bool variable:

void f3(string x)
{
    bool notEnglish = false;

    for (int i=0; i < x.length(); i++)
    {
        if ( ('A' > x[i] || 'z' < x[i]) && x[i] != ' ' )
        {
            notEnglish = true;
            break;
        }
    }

    if (notEnglish) std::cout << "NOT ENGLISH" << std::endl;
    else std::cout << "ENGLISH" << std::endl;
}

Without a bool and with int as return value:

int f3(string x)
{
    for (int i=0; i < x.length(); i++)
    {
        if ( ('A' > x[i] || 'z' < x[i]) && x[i] != ' ' )
        {
            std::cout << "NOT ENGLISH" << std::endl;
            return -1;
        }
    }

    std::cout << "ENGLISH" << std::endl;
    return 1;
}

I prefer the second one since you have a feedback (the return value) of what is occured inside the function.

Upvotes: 0

Related Questions