Check if the input is integer

I need to create a program that accepts a name, to validate that a character and an integer shouldn't be accepted. I made a way to not accept a single character but how can i make a condition to not accept an integer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include<iostream>
using namespace std;
int main()
{
	string name;
	
	enter:
	cout << "Enter employee name: ";
	getline(cin, name);
	if (name.length() == 1) {
		goto enter;
	} else {
		cout << name;
		return 0;
	}
}
You could use a loop and cin.get - use the version that reads con char.
As long as the input is a character add it to the string.
Define what you mean by 'integer shouldn't be accepted'

Is 123q OK? Is q234 bad??? What about leading spaces?

In this context possibly use a string stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include <sstream>

using namespace std;
int main()
{
	string name;

enter:
	cout << "Enter employee name: ";
	getline(cin, name);
	if (name.length() < 2) {
		goto enter;
	} else {
		istringstream iss(name);
		int i;

		if (iss >> i)
			goto enter;

		cout << name;
		return 0;
	}
}


Also, now that you're 'discovered' goto, please wipe that knowledge from your mind and forget all about it. It's bad practice to use.

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 <sstream>

using namespace std;
int main()
{
	string name;

	do {
		cout << "Enter employee name: ";
		getline(cin, name);

		if (name.length() > 1) {
			istringstream iss(name);
			int i;

			if (!(iss >> i)) {
				cout << name;
				break;
			}
		}
	} while (true);
}

Hello kramsuiluj,

As seeplus has pointed out using "goto" is bad form and not a good practice, but it does still have a rare use, but that is small.

Your original code with some additions that may help:
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
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <string>  // <--- Needed for "std::getline()".

using namespace std;

int main()
{
    constexpr size_t LEN{ 1 };
 
    string name;

    do
    {
        cout << "\n Enter employee name: ";

        getline(cin, name);

        if (name.length() == LEN)
        {
            std::cerr << "\n     Name needs to be more than 1 letter!\n\n";
        }
        else
        {
            cout << "\n You entered: " << name << '\n'; // <--- Used for testing. Comment or remove when finished.

            break;
        }

    } while (true);

 // <--- Once a proper length name is entered you can loop through each element of the string to check it for what you need or do not need.

    return 0;  // <--- Not required, but makes a good break point. 

See http://www.cplusplus.com/reference/cctype/ for a list of functions that you could use.

Something to start with.

Andy
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
#include<iostream>
#include<string>
using namespace std;

string getName()
{
   const string notAllowed = "0123456789!?~#@{}|£$%^&*+";
   const string notOnly = " ";
   
   string name;
   cout << "Enter a name: ";
   getline( cin, name );
   
   bool wrong = name.length() <= 1
             || name.find_first_of( notAllowed ) != string::npos
             || name.find_first_not_of( notOnly ) == string::npos
                                      // add any other reasons for it being faulty
             ;
   
   return wrong ? getName() : name;   // if faulty, call recursively, otherwise return name
}


int main()
{
   string name = getName();
   cout << name << '\n';
}
Last edited on
Topic archived. No new replies allowed.