wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

Numeric/Character Arrays Part 1

This is very simple. You are to construct 3 arrays (1 character, 1 integer, and 1 real (float)). You can do this in the main function as follows:

#include <iostream.h>

void main()
{
char personname[10][30] = {"Smith, John", "Adams, Mary", "Hernandez, Marisa", "Ng, Van, "Einstein, Edith"};
int personage[10] = {'21', '24', '22', '23', '32'};
float persongpa = {2.94, 3.01, 2.32, 3.76, 3.25};
short numberpeople = 5;
short offset, personnumber, legalresponse, menuchoice, endprogram = 1;

 

(Are there errors here? You bet!)

As you can see, we are building a quick database (not really) of 5 (the value stored in variable numberpeople) people's names, their ages, and their GPAs. In other words, in tabular form, it might appears as:

Name Age GPA
Smith, John 21 2.94
Adams, Mary 24 3.01
Hernandez, Marisa 22 2.32
Ng, Van 23 3.76
Einstein, Edith 32 3.25

Notice that I have allowed for a list of 10 people (as indicated by the subscript), but I have only entered 5 people onto the list. That is because we are going to allow the user to add more people onto the list (if they wish).

Once again, we are going to display a menu, which should appear as:

I don't think I need to give you the code, since you are all pretty good at displaying menus already.

There are a few additional considerations, however:

  1. You need to make sure the person enters a 'Legal' response (the integer 1, 2, 3, 4, 5, or 6). You are not to accept anything else.

  2. The program is to keep working until the user enters a '6' to quit the program

How do you do that?? Well, here is a similar program that might help you out. This program can be used to address point 1 above ("You need to make sure the person enters a 'Legal' response (the integer 1, 2, 3, 4, 5, or 6). You are not to accept anything else"):

#include <iostream.h>
void main()
{
    char characterentered = 'X';
    while ((characterentered < 'a') || (characterentered > 'c'))
    {
        cout << "Enter either the character lower case 'a', 'b', or 'c' ONLY ";
        cin >> characterentered;
        if ((characterentered < 'a') || (characterentered > 'c'))
        cout << "\nNo -- I said Enter either the character lower case 'a', 'b', or 'c' ONLY\n\n";
    }
}

This program gives the user a prompt ("Enter either the character lower case 'a', 'b', or 'c' ONLY  "), gets the input and stores it at location characterentered, checks the input (if ((characterentered < 'a') || (characterentered > 'c'))) and will not allow the user to get out of the while (while ((characterentered < 'a') || (characterentered > 'c))) loop until the user enters either an 'a', a 'b', or a 'c'.

I know you don't believe me, so here is the output for the (corrected) code above:

You will notice, from the output, That when I entered an invalid character ('Y'), the program checked it (if ((characterentered < 'a') || (characterentered > 'c'))) and then printed out the error message (cout << "\nNo -- I said Enter either the character lower case 'a', 'b', or 'c' ONLY\n\n";). When I finally entered a valid character ('b'), the program ended.

To check to see how the executable code works, click here. CAVEAT: Depending on your browser, the (end of the) sample code may flash by.

For the assignment program, I used the variable legalchoice (see above) to check this. I initially set it to 1 and changed it only when after I checked the user's input and found out it was valid. When I enter an 'illegal' menu choice, here is what my program output looks like:

To check to see how the executable code works, click here. CAVEAT: Depending on your browser, the (end of the) sample code may flash by.

As I mentioned, this program addresses point 1, but not necessarily point 2 ("The program is to keep working until the user enters a '6' to quit the program"). Here is another similar example, using the code from the example above:

#include <iostream.h>
void main()
{
    char characterentered;
    int endprogram = 1;

    while (endprogram == 1)
    {
        characterentered = 'X';
        while ((characterentered < 'a') || (characterentered > 'd'))
        {
            cout << "Enter either the character lower case 'a', 'b', 'c' or 'd' to quit ONLY ";
            cin >> characterentered;
            if ((characterentered < 'a') || (characterentered > 'd'))
            cout << "\nNo -- I said Enter either the character lower case 'a', 'b', 'c' or 'd' to quit ONLY\n\n";
            if (characterentered == 'd')
                endprogram = 0;
        }
    }
}

This is a little different. Essentially, I 'nested' all of the previous code inside an outer loop:

        while (endprogram == 1)
        {
            characterentered = 'X';
                •
                •
                •
        }

Inside of this loop, I initialized characterentered = 'X'; since I wanted to make sure I entered the inner loop(while ((characterentered < 'a') || (characterentered > 'd'))). Inside the inner loop, I added one more check:

            if (characterentered == 'd')
                    endprogram = 0;


If the user entered the character 'd', it would not only get them out of the inner loop, but also out of the outer loop (since the variable endprogram was now set to '0'.

Again, I know you don't believe me, so here is the output for the (corrected) code above:

Notice that this time, I first entered the character 'f', which we know is invalid, and caused the error message: "No -- I said Enter either the character lower case 'a', 'b', 'c' or 'd' to quit ONLY"

I next entered a valid character ('c'), and while this got me out of the inner loop, it did not get me out of the outer loop (I need to enter a 'd' to do that). So I next entered a valid character 'd' which also was the signal to end the program (and it did).

Try the program out by clicking here. CAVEAT: Depending on your browser, the (end of the) sample code may flash by.

I followed the same logic for the program at hand, and here is the output I received:

Once again, I initially entered an invalid choice ('7') and again, I received the same error message. I then entered a valid choice ('2'), which got me out of the inner loop, but not the outer loop. I finally indicated that I wanted to quit the program (by entering '6'), and the program ended.

Feel free to play around with this program by clicking here. CAVEAT: Depending on your browser, the (end of the) sample code may flash by.

Get this part of the program running, and we will go on to Part 2.

This page was last updated on 07/06/05