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

Numeric/Character Arrays Part 3

If you look at your code, you will notice it is getting very unwieldy: It is long, hard to understand, and a lot of code is duplicated (particularly the display of the elements in the list). We are going to make it easier to follow, and avoid excess code duplication, by breaking it up using functions.

I am going to give you my main function, and all of the include and function prototypes I used:

#include <iostream.h>
#include <stdio.h>
#include <string.h>

short displaymenu();
short validchoice(short choiceentered);
void displaylist(short number, char names[][30], int ages[], float gpas[]);
short addperson(short number, char names[][30], int ages[], float gpas[]);
short getperson(short number, char names[][30], int ages[], float gpas[]);
void changename(char newname[30]);
short changeage(short number, char newname[], short age);
float changegpa(short number, char newname[], float gpa);

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

    while (endprogram == 1)
    {
        legal = 1;
        while (legal == 1)
        {
            menuchoice = displaymenu();
            legal = validchoice(menuchoice);
        }
        if (menuchoice == 6)
            endprogram = 0;
        else
        {
            if (menuchoice == 1)
            displaylist(numberpeople, personname, personage, persongpa);
            if (menuchoice == 2)
            numberpeople = addperson(numberpeople, personname, personage, persongpa);
            if (menuchoice > 2)
                personnumber = getperson(numberpeople, personname, personage, persongpa);
            if (menuchoice == 3)
            {
                changename(personname[personnumber]);
                displaylist(numberpeople, personname, personage, persongpa);
            }
            if (menuchoice == 4)
            {
                personage[personnumber]
                = changeage(personnumber, personname[personnumber], 
                   personage[personnumber]);
                displaylist(numberpeople, personname, personage, persongpa);
            }
            if (menuchoice == 5)
            {
                persongpa[personnumber]
                = changegpa(personnumber, personname[personnumber],
                   persongpa[personnumber]);
                displaylist(numberpeople, personname, personage, persongpa);
            }
        }
    }
}