Exceptions

Given a Constructor: Account(std::string aN, std::string ss, std::string n, double b);
Implement the constructor for the account class. Throw an exception if an attempt is made to create an account using invalid data. The following conditions must be met for data to be considered valid:
The account number must be a 10 digit string.
SSN must be a 9 digit string.
Name must have at least 1 character in it.
The balance cannot be negative.

I am able to do the account number and ssn but for the name and balance, I am having trouble. I believe this part is almost correct but I have no idea about what I am missing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  Put the code you need help with here.
int characters = 0;
	int accname = 0;
	for (int j=0; j < n.length(); j++){
	accname = n[j];
	if (isalpha(accname)){
	characters = characters+1;
	if (characters <= 1){
	throw n;
	}
	}
	}
	name = n;

		
	if (b < 0){
	throw balance;
	}
	balance = b;
	

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

#include <iostream>

bool checkAlpha(std::string Name);

int main()
{

	std::string Name = "Joe Bloggs";

	if (checkAlpha(Name))
		std::cout << "You have a name with alpha chars!";
	else
		std::cout << "You must have at least 1 alpha!";

	return 0;
}

bool checkAlpha(std::string Name)
{
	bool isChar = false;
	// zero length string?
	if (Name.length() == 0)
		return isChar;
	// loop and set isChar if we find at
	// least one alpha.
	for (int i = 0; i < Name.length(); i++)
		if (isalpha(Name[i]))
			isChar = true;
	// return 
	return isChar;
}
Topic archived. No new replies allowed.