Modified Code

I modified the code in my original post. Now, I can get it to execute (thanks to invaluable help ;) The problem now is that while I can add nodes to the tree, I can't search the tree. Can someone please compile this and see if they can help me find the error in the program? You guys have been great.

#include
#include
#include
#include

struct stud
{
int id;
char cnum[8];
int sect;
int test[3];
int assn[6];
int asavg, tstavg, final, avg;

stud *left; //<---- pointers to children
stud *right; //<----
};

// function prototypes go here - menu, display menu, create

void menu();
void create(); // creates list and binary tree
void display(); //searches tree and displays info

void main()
{
menu();
}

void menu()
{
int choice;

cout << "1. Input Student info
";
cout << "2. Display Student info
";
cout << "3. Exit Program
";
cin >> choice;

while (choice !=1 && choice !=2 && choice !=3)
{
cout << "That is not a valid choice. Please re-enter 1 or 2: ";
cin >> choice;
}

switch (choice)
{
case 1: create();
clrscr();
break;
case 2: clrscr();
display();
break;
case 3: cout << " Have a great day! Good-bye.";
getch();
break;
}

}

void create()
{
stud info, *nodePtr, *newnode, *root; //newnode - newnode; nodeptr traverses infobase
root=NULL; //root of tree

int numstud, i, tcount=0, acount=0; //tcount and acount tally test/assn grades

cout << "How many students do you want to enter into the infobase? ";
cin >> numstud;

for (i=0; i> info.id;
cout << "Course Number - 7 characters:
";
cin >> info.cnum;
cout << "Section Number - 4 digits:
";
cin >> info.sect;
for (int t=0; t<=2; t++)
{
cout << "Enter test grade " << t+1 << ":"; //Enter 3 test grades
cin >> info.test[t];
tcount+=info.test[t];
}
for (int a=0; a<=5; a++)
{
cout << "Enter assignment grade " << a+1 << ":"; //Enter 6 assgn grades
cin >> info.assn[a];
acount+=info.assn[a];
}
cout << "Enter final exam grade: ";
cin >>info.final;

info.asavg=acount/6; //assignment total/6
info.tstavg=tcount/3; //test total/3
info.avg=(info.asavg+info.tstavg+info.final)/3;
clrscr();

newnode = new stud; //begin tree
newnode->id=info.id;
newnode->left=newnode->right=NULL;

if (!root)
root=newnode;
else
{
nodePtr=root;
while (nodePtr!=NULL)
{
if (info.id < nodePtr->id)
{
if (nodePtr->left)
nodePtr=nodePtr->left;
else
{
nodePtr->left=newnode;
break;
}
}
else if (info.id > nodePtr->id)
{
if (nodePtr->right)
nodePtr = nodePtr->right;
else
{
nodePtr->right=newnode;
break;
}
}
else
{
cout << "This student is already listed in the infobase.
";
getch();
clrscr();
menu();
break;
}
}
}
}
clrscr();
menu ();
}

void display()
{
int num;
stud *nodePtr;
char choice;

cout << " Display Menu

";
cout << "To access student data, please enter the Student ID: ";
cin >> num;

while (nodePtr)
{
if (nodePtr->id==num)
{
cout << "ID: " << nodePtr->id << endl;
cout << "Course Number: " << nodePtr->cnum << endl;
cout << "Section Number: " << nodePtr->sect << endl;
cout << "Assignment Grades:
";
cout << " Assgn 1: " << nodePtr->assn[0] << endl;
cout << " Assgn 2: " << nodePtr->assn[1] << endl;
cout << " Assgn 3: " << nodePtr->assn[2] << endl;
cout << " Assgn 4: " << nodePtr->assn[3] << endl;
cout << " Assgn 5: " << nodePtr->assn[4] << endl;
cout << " Assgn 6: " << nodePtr->assn[5] << endl;
cout << "Test Grades:
";
cout << " Test 1: " << nodePtr->test[0] << endl;
cout << " Test 2: " << nodePtr->test[1] << endl;
cout << " Test 3: " << nodePtr->test[2] << endl;
getch();
}

}

cout << "Would you like to search for a different student?";
cin >> choice;

while (choice!='Y' && choice!='y' && choice!='n' && choice!='N')
{
cout << "Only 'Y' and 'N' are acceptable responses.
"
<< "Please re-enter: ";
cin >> choice;
}

if (choice=='Y' || choice=='y')
{
clrscr();
display();
}
if (choice=='N' || choice=='n')
{
menu();
}
}

Comments

  • you have created the head of the list in the create() function -- and it is local only to that function. you should pass a pointer to it from main() so that it can be visible to other functions . For example:

    [code]
    int main()
    {
    stud *root = NULL;

    create(&root);
    diaplay(root)
    }

    void create(stud **root)
    {
    // blabla

    if (!*root)
    *root = newnode;
    else
    {
    nodePtr = *root;
    // blabla
    }
    }

    void display(stud* root)
    {
    // do something
    }

  • Ok - Thanks - I have made these changes - but now I am getting compiler errors. that I don't know how to fix -
    Here is the program re-modified with comments where I get the errors

    #include
    #include
    #include
    #include

    struct stud
    {
    int id;
    char cnum[8];
    int sect;
    int test[3];
    int assn[6];
    int asavg, tstavg, final, avg;

    stud *left; //<---- pointers to children
    stud *right; //<----
    };

    // function prototypes go here - menu, display menu, create

    void menu();
    void create(stud *); // creates list and binary tree
    void display(stud *); //searches tree and displays info

    void main()
    {
    menu();
    }

    void menu()
    {
    int choice;
    stud *root = NULL;


    cout << "1. Input Student info
    ";
    cout << "2. Display Student info
    ";
    cout << "3. Exit Program
    ";
    cin >> choice;

    while (choice !=1 && choice !=2 && choice !=3)
    {
    cout << "That is not a valid choice. Please re-enter 1 or 2: ";
    cin >> choice;
    }

    switch (choice)
    {
    case 1: clrscr();
    /**********************************************************************
    Here I get connot convert stud to stud * - I have tried changing the &
    to a * and still get the error. It also tells me this a type mismatch because
    it wanted stud * and got stud How do I fix this one?
    **********************************************************************/
    create(&root);
    break;
    case 2: clrscr();
    /**********************************************************************
    Here I get connot convert stud to stud * It also tells me this a type mismatch
    because it wanted stud * and got stud How do I fix this one?
    **********************************************************************/
    display(*root);
    break;
    case 3: cout << " Have a great day! Good-bye.";
    getch();
    break;
    }

    }

    void create(stud *root)
    {
    stud info, *nodePtr, *newnode; //newnode - newnode; nodeptr traverses infobase
    int numstud, i, tcount=0, acount=0; //tcount and acount tally test/assn grades

    cout << "How many students do you want to enter into the infobase? ";
    cin >> numstud;

    for (i=0; i> info.id;
    cout << "Course Number - 7 characters:
    ";
    cin >> info.cnum;
    cout << "Section Number - 4 digits:
    ";
    cin >> info.sect;
    for (int t=0; t<=2; t++)
    {
    cout << "Enter test grade " << t+1 << ":"; //Enter 3 test grades
    cin >> info.test[t];
    tcount+=info.test[t];
    }
    for (int a=0; a<=5; a++)
    {
    cout << "Enter assignment grade " << a+1 << ":"; //Enter 6 assgn grades
    cin >> info.assn[a];
    acount+=info.assn[a];
    }
    cout << "Enter final exam grade: ";
    cin >>info.final;

    info.asavg=acount/6; //assignment total/6
    info.tstavg=tcount/3; //test total/3
    info.avg=(info.asavg+info.tstavg+info.final)/3;
    clrscr();

    newnode = new stud; //begin tree
    newnode->id=info.id;
    newnode->left=newnode->right=NULL;

    if (!root)
    /**********************************************************************
    Here I get connot convert stud to stud * It DOES NOT tell me this a type mismatch
    How do I fix this one?
    **********************************************************************/
    *root=newnode;
    else
    {
    /**********************************************************************
    Here I get connot convert stud to stud * It DOES NOT tell me this a type mismatch
    How do I fix this one?
    **********************************************************************/
    nodePtr=*root;
    while (nodePtr!=NULL)
    {
    if (info.id < nodePtr->id)
    {
    if (nodePtr->left)
    nodePtr=nodePtr->left;
    else
    {
    nodePtr->left=newnode;
    break;
    }
    }
    else if (info.id > nodePtr->id)
    {
    if (nodePtr->right)
    nodePtr = nodePtr->right;
    else
    {
    nodePtr->right=newnode;
    break;
    }
    }
    else
    {
    cout << "This student is already listed in the infobase.
    ";
    getch();
    clrscr();
    menu();
    break;
    }
    }
    }
    }
    clrscr();
    menu ();
    }

    void display(stud *root)
    {
    int num;
    stud *nodePtr;
    char choice;

    cout << " Display Menu

    ";
    cout << "To access student data, please enter the Student ID: ";
    cin >> num;

    while (nodePtr)
    {
    if (nodePtr->id==num)
    {
    cout << "ID: " << nodePtr->id << endl;
    cout << "Course Number: " << nodePtr->cnum << endl;
    cout << "Section Number: " << nodePtr->sect << endl;
    cout << "Assignment Grades:
    ";
    cout << " Assgn 1: " << nodePtr->assn[0] << endl;
    cout << " Assgn 2: " << nodePtr->assn[1] << endl;
    cout << " Assgn 3: " << nodePtr->assn[2] << endl;
    cout << " Assgn 4: " << nodePtr->assn[3] << endl;
    cout << " Assgn 5: " << nodePtr->assn[4] << endl;
    cout << " Assgn 6: " << nodePtr->assn[5] << endl;
    cout << "Test Grades:
    ";
    cout << " Test 1: " << nodePtr->test[0] << endl;
    cout << " Test 2: " << nodePtr->test[1] << endl;
    cout << " Test 3: " << nodePtr->test[2] << endl;
    getch();
    }

    }

    cout << "Would you like to search for a different student?";
    cin >> choice;

    while (choice!='Y' && choice!='y' && choice!='n' && choice!='N')
    {
    cout << "Only 'Y' and 'N' are acceptable responses.
    "
    << "Please re-enter: ";
    cin >> choice;
    }

    if (choice=='Y' || choice=='y')
    {
    clrscr();
    /**********************************************************************
    Here I get connot convert stud to stud * It also tells me this a type mismatch
    How do I fix this one?
    **********************************************************************/
    display(*root);
    }
    if (choice=='N' || choice=='n')
    {
    menu();
    }
    }
    : you have created the head of the list in the create() function -- and it is local only to that function. you should pass a pointer to it from main() so that it can be visible to other functions . For example:
    :
    : [code]
    : int main()
    : {
    : stud *root = NULL;
    :
    : create(&root);
    : diaplay(root)
    : }
    :
    : void create(stud **root)
    : {
    : // blabla
    :
    : if (!*root)
    : *root = newnode;
    : else
    : {
    : nodePtr = *root;
    : // blabla
    : }
    : }
    :
    : void display(stud* root)
    : {
    : // do something
    : }
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories