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.
#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#forfor( unsignedchar c : str )
if( !std::isalpha(c) ) returnfalse ; // return false if the character is not an alpha character
// https://en.cppreference.com/w/cpp/string/byte/isalphareturntrue ; // 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.
#include <iostream>
#include <string>
usingnamespace 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
}
#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#forfor( unsignedchar c : str )
if( !std::isalpha(c) ) returnfalse ; // return false if it is not an alpha character
// https://en.cppreference.com/w/cpp/string/byte/isalphareturntrue ; // 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;
}
}
@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"
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.