:/ function not doing ANYTHING

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

void AddNewResident (char userOption, RecordType ResidentRecords [], int& counter, string userSocial, string userLastName, string userPhoneNumber, string userTypePhone)
{
	bool check = false;
	bool flag = false;
	char userInput = 0;

	do
	{
		if (counter < MAX_RESIDENTS)
		{
			while (!flag)
			{
				cout << "Would you like to enter a new resident (Y/N)? ";
				cin >> userInput;
			
				userInput = (toupper (userInput));

				if (userInput == 'N')
				{
					userOption = (DisplayMenu (userOption));
					flag = true;
				}
				else if (userInput == 'Y')
				{	
					string ReadAndValidateUserLastName (userLastName);
					string ReadAndValidateUserSocial (userSocial);
					string ReadAndValidateUserPhoneNumber (userPhoneNumber);
					string ReadAndValidateUserPhoneType (userTypePhone);

					flag = true;
				}

				if (flag == false)
				{
					cout << "Invalid Entry! Please try again!" << endl << endl;
				}
			}

			check = true;
		}
		else
		{
			cout << "There are too many lines of data!" << endl << endl;
			check = false;

			userOption = (DisplayMenu (userOption));
		}
	}
	while (!check);
}


Currently, it asks me if i want to add a new resident...then regardless of my input, it ends the program. Any suggestions?

What it SHOULD do is...while the array has room left in it, ask if the user wants to add a new resident, if NO, show the display menu of options again, if YES, it should call functions to prompt, read, and validate ssn, lastname, phone number, and phone type.
If there is no more room in the array, it will display an error saying there are too many lines of data, and return to the display menu of options...
You don't seem to understand the purpose of parameters.

string ReadAndValidateUserLastName (userLastName); That does not call a function. It creates a string.

Your logic is convoluted. ¿why do you ask if they want to add a resident inside the 'add_resident' function?
oh geeze, i didnt even catch that I had "string" before the function call. thanks!

Um, as for the last part, I am not sure how else to do that loop...outside of the add_resident function...
closed account (j2NvC542)
Hey, I would like to give you a function I think will come handy as you are programming.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool prompt(const string msg)
{
    char answer;

    do {
        cout << msg << " (y/n) ";
        cin >> answer;
        answer = tolower(answer);

        // If the user inputs more than one character, there will still be
        // characters left in the buffer, which in this case leads to outputting
        // the prompt question as many times as there are characters left
        // in the buffer.
        // The below line makes sure, everything left in the buffer is ignored.
        cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    } while (answer != 'y' && answer != 'n');

    if (answer == 'y')
        return true;
    else
        return false;
}


You should always try to make a function of everything that you reuse. And possibly make it universal, not only for this one case. Instead of a funtion like prompt_continue() or something similar, you have one prompt() for everything and you can type your message like you need it in this case.

With this function you can do this:
1
2
3
while (!prompt()) { // prompt() returned false
    // code to execute if the answer was negative
}


Now you don't need this ugly check variable anymore.
Last edited on
Topic archived. No new replies allowed.