Data validation

closed account (iyRG3TCk)
Write your question here.

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
  Put the code you need help with here.//Assume that this is my structure.
struct customer 
{
	string firstname;
	string lastname;
	int age;
	char Gender[1];

}cust;


//And this is where I get input from the user and "cin" to the structure.

cout << "\nThe ID number is not in the database.\n " << endl;
	
			cout << "Enter the first name: ";
			cin >> cust.firstname;
			cout << "Enter the last name: ";
			cin >> cust.lastname;
			cout << "Enter the age: ";
			cin >> cust.age;
			cout << "Enter the gender: ";
			cin >> cust.Gender;

//And my question is how to check weather user has entered a correct data  value to each and every structure variable in that structure.
//ex:
//age can't be "0".
//for Gender they must enter a "M" or "F".
//I want to display a message if the input is wrong and let user to input data again.


please help me! 
Last edited on
Hello Gimnath Priyadarshana,

After first and last names you could do something like:

1
2
3
4
5
6
while (cust.firstname.length() == 0)
{
	std::cout << "\n That is not a valid First Name. Please try again.";
	std::cout << "Enter the first name: ";
	std::getline(std::cin, cust.firstname);
}


And for age this might work:

1
2
3
if (cust.age < 1 || cust.age > 110)
{
    // Deal with out of range. 


And for gender:

1
2
3
4
while (toupper(cust.Gender) != 'M' && toupper(cust.Gender) != 'F')
{
    //  What is here would be similar to the other while loop.
}


This is untested for now, but the concept is good. I will work up a test shortly.

Two suggestions dealing with variable names. "Gender" is more commonly used as "gender" and "firstname" is more often seen as "firstName". This is what most people use because it is less confusing. I tend to use a capital letter at the beginning of a name to refer to a function name and also fora class or struct name.

Hoope that helps,

Andy

Edit: char Gender[1]; just needs to be char Gender;.
Last edited on
This could be a start (very basic):

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
//And my question is how to check weather user has entered a correct data 
// value to each and every structure variable in that structure.
//ex:
//age can't be "0".
//for Gender they must enter a "M" or "F".
//I want to display a message if the input is wrong and let user to input data again.
#include <iostream>
#include <cctype>

struct Customer 
{
    std::string firstname;
    std::string lastname;
    int age;
    char gender;
};

int main()
{
    Customer cust;

    std::cout << "Enter the first name: ";
    std::getline(std::cin, cust.firstname);

    std::cout << "Enter the last name: ";
    std::getline(std::cin, cust.lastname);

    do {
        std::cout << "Enter the age (1-100): ";
        std::cin >> cust.age;
        std::cin.ignore(1);
        if(cust.age < 1 || 100 < cust.age) {
            std::cout << "Wrong age. Retry.\n";
        }
    } while(cust.age < 1 || 100 < cust.age); // chose your age limits

    do {
        std::cout << "Enter the gender (m-f): ";
        char gender;
        std::cin >> gender;
        gender = std::toupper(gender);
        if(gender != 'M' && cust.gender != 'F') {
            std::cout << "Wrong gender. Retry.\n";
        }
        cust.gender = gender;
    } while(cust.gender != 'M' && cust.gender != 'F');

    std::cout <<   "Inserted data are:"
                 "\nFirst name: " << cust.firstname
              << "\nSurname:    " << cust.lastname
              << "\nAge:        " << cust.age
              << "\nGender:     " << cust.gender << '\n';
    return 0;
}

closed account (iyRG3TCk)
Thnx Handy Andy and Enoizat.. I appreciate your help..
Topic archived. No new replies allowed.