restrict string input with letters only while using the void class

I was trying to make a set for insert the input and get record to output the input for the class.

However I want to restrict the input from the setInsertx() while using string of the variable by only allow letters for capital and non-capital letters on the input of the string, if the input was invalid such as none of the input contain letters then an error output message will appear as you can see on the while loop on setInsertx().

Yes it wasnt compile yet but I still have no idea how to make a requirement to restrict the input letters only on the while loop requirement

Keep in mind the C++ language must be a beginner level and dont make it very complicated since it will be easy to do for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

class Insert
{
    private:
    string x;

    public:
    void setInsertx();
    void getInsertx();
}

void Insert::setInsertx()
{
    cout << "Enter letter or words" << endl;
    cin >> x;

    while ()
    { 
        cout << "Error\n";
        cout << "Enter letters or words" << endl;
        cin >> x;
    }
}

void Insert::getInsertx()
{
    cout << "You typed " << x << "\n";
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>

bool all_alpha( const std::string& str ) // return true if there are no non-alpha characters
{
    for( unsigned char c : str ) if( !std::isalpha(c) ) return false ;
    return true ;
    // alternatively, use std::all_of in <algorithm>
}

std::string get_alpha_str()
{
    std::string str ;
    std::cout << "enter letters: " ;
    std::cin >> str ;
    if( all_alpha(str) ) return str ;
    else
    {
        std::cout << "error. try again\n" ;
        return get_alpha_str() ; // try again
    }
}
@JLBorges
I dont really get what these coding mean.

Can you give me an explanation how does your code work based on the c++ language you have used?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <cctype>

bool all_alpha( const std::string& str ) // return true if there are no non-alpha characters in str
{
    // for each character in the string
    // range-based loop: https://www.stroustrup.com/C++11FAQ.html#for
    for( unsigned char c : str )
        if( !std::isalpha(c) ) return false ; // return false if the character is not an alpha character
                                              // https://en.cppreference.com/w/cpp/string/byte/isalpha

    return true ; // if we didn't return false earlier, every character is an alpha character: return true
}

std::string get_alpha_str()
{
    // accept a string as input from the user
    std::string str ;
    std::cout << "enter letters: " ;
    std::cin >> str ;

    // if the input string consists of only alpha characters, we are done: return it
    if( all_alpha(str) ) return str ;

    else
    {
        // otherwise, report an error in input and repeat the process
        std::cout << "error. try again\n" ;
        return get_alpha_str() ; // try again (by calling this very function once again)
    }
}
@JLBorges
So you dont have to use set function for the classes right?

But I really need set and get functions to do like I did before.

At least I need to give a statement for the while loop in setInsertx() with the while()

I did an easy code where I can restrict input only the numbers from 100 to 200 by using this code and it worked so I want to something similar to this code but restrict the input of the string with only letters and spaces only on while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

class Insert
{
    private:
    int y; //variable for the input

    public:
    void setInserty(); //set function for the input
    void getInserty(); //get function for the output to appear the value of the input
}

void Insert::setInserty()
{
    cout << "Enter 100 to 200" << endl;
    cin >> y; //input for the set function

    while (y < 100 || y > 200) //loops again if the input was invalid that the number is less than 100 or more than 200, the while statement will loop again until you get the correct input.
    { 
        cout << "Error\n";
        cout << "Enter around 100 to 200" << endl;
        cin >> y;
    }
}

void Insert::getInserty()
{
    cout << "Your number is " << y << "\n"; //output of the get function will appear the value of the input variable if the input was valid
}
Last edited on
> I need to give a statement for the while loop in setInsertx() with the while()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <cctype>

bool all_alpha( const std::string& str ) // return true if there are no non-alpha characters in str
{
    // for each character in the string
    // range-based loop: https://www.stroustrup.com/C++11FAQ.html#for
    for( unsigned char c : str )
        if( !std::isalpha(c) ) return false ; // return false if it is not an alpha character
                                              // https://en.cppreference.com/w/cpp/string/byte/isalpha

    return true ; // if we didn't return false earlier, every character is an alpha character: return true
}

class Insert
{
    private: std::string x;

    public:
        void setInsertx();
        void getInsertx();
};

void Insert::setInsertx()
{
    std::cout << "Enter letter or words: " ;
    std::cin >> x;

    while ( !all_alpha(x) ) // ****
    {
        std::cout << "Error\nEnter letters or words: " ;
        std::cin >> x;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <cctype>

class Insert {
private:
	std::string x;

public:
	void setInsertx();
	void getInsertx() const ;
};

void Insert::setInsertx() {
	bool good {};

	do {
		std::cout << "Enter letter or words: ";
		std::getline(std::cin, x);

		good = true;

		for (const auto& ch : x)
			if (!std::isalpha(static_cast<unsigned char>(ch)) && !std::isspace(static_cast<unsigned char>(ch))) {
				good = false;
				break;
			}
	} while (!good && (std::cout << "Error\n"));
}

void Insert::getInsertx() const {
	std::cout << "You typed " << x << "\n";
}

int main() {
	Insert i;

	i.setInsertx();
	i.getInsertx();
}

@JLBorges
Yes your code is working but when I typed spaced words such as more than one word, it only appears one word.
For example I type "Hello there" on the input but the output only shows "Hello"
Last edited on
Because cin >> is used. This will only input one word until a white-space char is found. To get a whole line including the white-space chars use std::getline() as per L19 in my code above.
Topic archived. No new replies allowed.