String input validation?

Apr 9, 2012 at 10:13pm
Hi guys, I am attempting to complete an assignment that requires the user to enter in the name, number and points scored for 5 basketball players. My input validation works well with numbers but not the name(string) of the player. As i understand I need to check each char in the string for validation(no numbers or symbols). I am having a difficult time with this. I created a function bool(isLetter) and call that for string validation but that appears to be a bit off as well. Any ideas to help push me in the right direction would be much appreciated. Thanks in advance. Here is my code:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;


//struct of Basketball Player info
struct BasketballPlayerInfo
{
	string name; //player name
	
	int playerNum, //player number
		pointsScored; //points scored

};

bool isLetter(string);

int main()
{
	int index, //loop count
		total = 0; //hold total points
	const int numPlayers = 5; //nuymber of players
        BasketballPlayerInfo players[numPlayers]; //Array of players

	//explain program to user
	cout << "This program will display the names, numbers, and points scored\n";
	cout << "of five basketball player based on the data that you input. A\n";
	cout << "total score will also be displayed. Please enter in only alpha\n";
	cout << "-betical characters for names as well as numeric chartacters\n";
	cout << "for numbers and points, failing to do so will result in program\n";
	cout << "termination. Thank you\n";
	cout << "-----------------------------------------------------------------" << endl;
	

	//ask user for Basketball Player Info
	cout << "Enter the name, number, and points scored for each of the 5 players.\n";

	for (index = 0; index < numPlayers; index++)
	{
		//collect player name
		cout << " " << endl;
		cout << "Enter the name of player # " << (index + 1);
		cout << ": ";
		
		//input validation
		if(!(getline(cin, players[index].name)))
		{
			isLetter(players[index].name);
		}
		
		
		//collect players number
		cout << "Enter the number of player # " << (index + 1);
		cout << ": ";
		
		//input validation
		if(!(cin >> players[index].playerNum))
		{
			cout << "Player number must be numeric characters only!\n";
			cout << "Program terminating please start over." << endl;
			system("pause");
			exit(0);
		}
		//collect points scored
		cout << "Enter points scored for player # " << (index + 1);
		cout << ": ";

		//input validation
		if(!(cin >> players[index].pointsScored))
		{
			cout << "Points scored must be numeric characters only!\n";
			cout << "Program terminating please start over." << endl;
			system("pause");
			exit(0);
		}
		 
		cin.ignore();
	}

	//display
	cout << "\n";
	cout << "Here is the information for each player: \n";
	cout << fixed << showpoint << setprecision(2) << setw(5);
	cout << "\n";
	cout << setw(26) << "Name" << setw(18) << "Number" <<setw(16) << "Points\n";
	cout << "-----------------------------------------------------------" << endl;
	

	for(index = 0; index < numPlayers; index++)
	{
		cout << "Player # " << (index + 1);
		cout << ": " << setw(15) << players[index].name << setw(15) << players[index].playerNum << setw(15) << players[index].pointsScored << endl;
		cout << "-----------------------------------------------------------" << endl;
		
	}

	//display total points scored by all players
	for(index = 0; index < numPlayers; index++)
	{
		//hold total
		total += players[index].pointsScored;
	}

	cout << "Total Points scored are: " << total << endl;

 system("pause");
return 0;

}

bool isLetter(string name)
{
	for(int i = 0; i < name.length(); i++)
	{
		if(! (name[i] >= '0' && name[i] <= '9' || name[i] == ' ') ) return false;
		{
			cout << "Player name must be alphabetical characters only!\n";
			cout << "Program terminating please start over." << endl;
			system("pause");
			exit(0);
		}

		return true;
	}
}
Last edited on Apr 9, 2012 at 10:14pm
Apr 9, 2012 at 10:20pm
In your validation function you seem to be returning invalid if any of the characters is not a number or a space, which is not what you want. Also note that your block after the if there isn't part of the if; the if if only including the return false you've written after it.
Apr 9, 2012 at 10:35pm
Thanks Zhuge. I have changed the function to this but something still is not registering here,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool isLetter(string name)
{
	for(int i = 0; i < name.length(); i++)
	{
		if(name[i] >= 0 || name[i] <= 9 || name[i] == ' ') 
		{
			return false;
			cout << "Player name must be alphabetical characters only!\n";
			cout << "Program terminating please start over." << endl;
			system("pause");
			exit(0);
		}

		return true;
	}
}
Apr 9, 2012 at 10:38pm
There is such a function as std::isalpha declared in header <cctype> that will make your task very simple.
Here is an example of using it with standard algorithm std::all_of


1
2
3
4
5
inline bool isValidName( std::string name )
{
   return ( std::all_of( name.begin(), name_end(),
                                [] ( char c ) { return ( std::isalpha( c ) ); } ) );
}


You can substitute the standard algorithm for your loop.
Last edited on Apr 9, 2012 at 10:38pm
Apr 9, 2012 at 10:38pm
returning false ends the function. none of the following code executes. and the system pause and system exit also might be causing issues. i would suggest removing all of them.
Apr 9, 2012 at 10:47pm
Ok, thanks, I was only adding the system pause so that the console would stay up so I could see if the exit prompt was correct. I thought that was the way using Visual C++ on a window's machine. I traditionally have done this using Macvim and have switched for school. The exit(0) I added to terminate, should it not be there? The reason for it was so that the program terminated rather than clearing the buffer etc, I had confusion on clearing input when the input is tallying for a grand total, so I found it a bit easier for myself to just terminate until I grasped the concept.
Apr 10, 2012 at 12:13am
isLetter needs a lot of work, seems like the rest of it does to.
1
2
3
4
if(!(getline(cin, players[index].name)))
{
  isLetter(players[index].name);
}


Do you ever even call isLetter? How would you have !getline()?

if(name[i] >= 0 || name[i] <= 9 || name[i] == ' ')

This does not work correctly, you have || instead of &&. Why can't a name have a space anyway? (That's actually something to have purposefully allowed by using getline().

Try to avoid system("pause");. A cin.get(); works well, just tell the user to hit enter, and also make sure to clear the buffer before hand (something I think should be done after every extraction anyway).

You should be able to return 0; instead of exit(0). Then again, you should be fixing the cin and letting the user try to input another number.
Topic archived. No new replies allowed.