Code freezing at a particular point

This should be an incredibly simple thing to do and I have no idea why I am stalling where I am. All I want to do is loop through every character in a string and verify if it is alphabetic. If its not, I want to start a loop over again and ask the user to input a new value. For some reason nothing past the end of the length while is run. Any ideas?

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
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	string FirstName;
	int FirstLength;
	bool GetName;
	int NonAlpha;
	int i;
    char c;
    
	i = 0;
	GetName = true;
	NonAlpha = 0;

	if(GetName)
	{
		cout << "Enter First Name:" << '\n';
		cin >> FirstName;
		FirstLength = FirstName.length();
	   
		while (cin.fail())
			  {
				cin.clear();
				cin.ignore(200, '\n');
		        cin >> FirstName;
			  } 

		while((i <= FirstLength))
			{
				c = FirstName.at(i);

				if(isalpha(c))
				{
					++i;
				}
				else
				{
					NonAlpha = 1;
					++i;
				}
			}
			
	
		
		if((NonAlpha = 1))
			{
				i = 0;
				cout << "Error: Use only letters when entering names." << '\n';
			}
			else
			{
				GetName = false;
			}		  
	}

	cout << i << "." << c;
}
in line 50 you probably wanted if((NonAlpha == 1))

Somehow your code looks to complicated. Here is an easier example.
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
#include <iostream>
#include <string>

using namespace std;

bool ValidInput (const string& name);

int main ()
{
  bool valid = false;
  string input;

  while (!valid)
  {
    cout << "Enter name: ";
    cin >> input;

    valid = ValidInput (input);
    if (!valid)
    {
      cout << "\nYour input was not correct - try again.\n";
    }
    else
    {
      cout << "\nYour input was correct.\n";
    }
  }
  
  system ("pause");
  return EXIT_SUCCESS;
}

bool ValidInput (const string& name)
{
  for (char c : name)
  {
    if (!isalpha (c))
    {
      return false;
    }
  }
  return true;
}
That change to line 50 still doesn't solve the problem (line 61 doesn't appear, the program just terminates).

Ha, I love coding. Yours definitely is more straight-forward but certain parts of it I don't understand. I am reluctant to use something I can't explain.
(line 61 doesn't appear, the program just terminates)


http://www.cplusplus.com/forum/beginner/1988/
line 33 should be

while((i <FirstLength))
Topic archived. No new replies allowed.