converting string to int and checking for a comma.

Hello, im in the process of writing a input validation code. But i seem to be stuck on a certain part. For the first part, i need to check that my input has a comma in it and if it doesn't it should loop back. I have already completed the previous loops to check for digits and character lengths. And I need to convert a string into an int in order to return the function.
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
 const int SIZE = 50; 
    char input[SIZE];
	cin.getline (input,SIZE);

	
    int numChar = 0, cnt = 0;

	while(input[cnt++] != '\0')
	{
		numChar++;
	}

	if  (numChar >= 30)    
	{
		
	cout << "Error, Must be no greater than 30 characters long.\n";
	}

	
	for (int cnt = 0; input[cnt] != '\0'; cnt++)
	{
		if (isdigit(input[cnt]))
		cout << "Error, name must not contain a digit.\n";
		
	}
	
	for (int cnt = 0; input[cnt] != '\0'; cnt++)
	{
		if (input[cnt] != ',')
		{
			cout << "Error, password must contain a comma.\n";
			
		}
	}


int GetID (void)
{
	string input;
	int x = 1;
	cin >> input;
	int maxstring = 4;

	while ( input.size() != 4)
	{
		cout << " Id must be 4 numbers" << endl;
		cin >> input;
	}
	
	//x = input;
	//return x;
	return x;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int GetID (void)
{
	string input;
	cin >> input;
	int maxstring = 4;

	while ( input.size() != 4)
	{
		cout << " Id must be 4 numbers" << endl;
		cin >> input;
	}
	
	std::istringstream buf(input.substr(0,4));
	long int num;
	buf >> num;
	cout << num;
	

	return num;
}


updated code
27
28
29
30
31
32
33
34
	for (int cnt = 0; input[cnt] != '\0'; cnt++)
	{
		if (input[cnt] != ',')
		{
			cout << "Error, password must contain a comma.\n";
			
		}
	}

You are crying "Error!" whenever a character is not a comma. Perhaps you should try counting how many commas there are instead.
Last edited on
i still need help, my getname function gets skipped over and i still don't know how to check for comma.

Here is my entire program:


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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

string GetName (void);
int GetID (void);
int GetYear ( void);
string GetGender (void) ; 
//****************************************************************************************************
int main()
{
	const int SIZE = 50; 
    char input[SIZE];
	string output, gender, name;
	int year, id;


	year = GetYear(); // done
	name = GetName();
	id = GetID (); // done
	gender = GetGender(); // done



	cout << name;
	cout << setw(7) << "ID:" << id;
	cout << setw(10) << "       Graduated in:" <<  year;
	cout << setw(10) <<gender;
	cout << endl;
	
	return 0;
}
//****************************************************************************************************

string GetGender (void)
{

	char gender;
	string x;
	cout << "What is your gender?" << endl;
	cin >> gender;
	
	 while ( !(gender == 'm' || gender == 'M' || gender == 'f' || gender == 'F') )
   {
      cout << "Please, enter F or M: ";
      
      cin.clear();
      cin.ignore (1000, '\n');

      cin >> gender;
   }
	 if ( gender == 'f')
	 {
		 gender = 'F';
	 }
	  if ( gender == 'm')
	 {
		 gender = 'M';
	 }

	if ( gender = 'f')
	{
		x = "Female";
	}
		
	if ( gender = 'M')
	{
		x = "Male";
	}
	
	return x;



	
}

	
//****************************************************************************************************
int GetID (void)
{
	cout << "Enter your ID number" << endl;
	string input;
	cin >> input;
	

	while ( input.size() != 4)
	{
		cout << " Id must be 4 numbers" << endl;
		cin >> input;
	}
	
	std::istringstream buf(input.substr(0,4));
	long int num;
	buf >> num;

	

	return num;
}
//****************************************************************************************************

string GetName (void)
{
	

	const int SIZE = 50; 
	int value; 
    char input[SIZE];
	cout << "Enter your name" << endl;
	cin.getline (input,SIZE);

	
    int numChar = 0, cnt = 0;

	while(input[cnt++] != '\0')
	{
		numChar++;
	}

	if  (numChar >= 30)    
	{
		
	cout << "Error, Must be no greater than 30 characters long.\n";

	}

	
	for (int cnt = 0; input[cnt] != '\0'; cnt++)
	{
		if (isdigit(input[cnt]))
		cout << "Error, name must not contain a digit.\n";


		
	}
	
	
	


	string output;
	output = input;
	return output;
}


	
//****************************************************************************************************

int GetYear (void)
{
	cout << "Enter your graduation year"; 
	start:
	string input;
	cin >> input;

	
	std::istringstream buf(input.substr(0,4));
	long int num;
	buf >> num;


	 if ((num >= 1895) && (num <= 2016)) 
	 {
        
	 }
	 else
	 {
		 cout << "Error, year must be from 1895 - 2016 and/or contain only 4 numbers\n";
		 goto start;
	 }

			
	return num;	
	
}
	
	
	
Topic archived. No new replies allowed.