beachbum3159
December 3rd, 2001, 12:35 PM
this is the code i have:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void getData(float& gpa, char name[15]);
const int nil = 0;
class node_type
{
public:
float id;
char nam[15];
node_type *next;
};
void main()
{
node_type *first, *p, *q, *newnode;
int i;
float ident;
char namet[15];
first = new node_type;
p = first;
getData(ident, namet);
(*first).id = ident;
(*first).nam[15] = namet[15];
(*first).next = nil;
for(i = 1; i<=3; i++)
{
getData(ident, namet);
newnode = new node_type;
(*newnode).id = ident;
(*newnode).nam[i] = namet[i];
if ((*first).id>ident)
{
(*newnode).next = first;
first = newnode;
continue; // causes the for loop to go to the next iteration of the loop
}
node_type *prev = first;
node_type *curr;
for (curr = (*first).next ; curr != nil ; curr = (*curr).next)
{
if ((*curr).id > ident)
break; // causes the for loop where its located to terminate
prev = curr;
}
(*newnode).next = curr;
(*prev).next = newnode;
}
q = first;
system("cls");
cout<<"The list contains: \n";
cout.setf(ios::showpoint|ios::fixed|ios::right);
cout.precision(1);
while(q!=nil)
{
cout<<"Name is: "<<(*q).nam<<endl;
cout<<"GPA is: "<<(*q).id<<endl;
q= (*q).next;
}
}
//=====================================================================
//This function is used to get the data.
//=====================================================================
void getData(float& gpa, char name[15])
{
cout<<"Enter Last Name: ";
cin>>name;
cout<<"Enter G.P.A: ";
cin>>gpa;
}
it works fine for the GPA but it prints out garbage for the name... can someone please help me?
i will rate ur response!!
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void getData(float& gpa, char name[15]);
const int nil = 0;
class node_type
{
public:
float id;
char nam[15];
node_type *next;
};
void main()
{
node_type *first, *p, *q, *newnode;
int i;
float ident;
char namet[15];
first = new node_type;
p = first;
getData(ident, namet);
(*first).id = ident;
(*first).nam[15] = namet[15];
(*first).next = nil;
for(i = 1; i<=3; i++)
{
getData(ident, namet);
newnode = new node_type;
(*newnode).id = ident;
(*newnode).nam[i] = namet[i];
if ((*first).id>ident)
{
(*newnode).next = first;
first = newnode;
continue; // causes the for loop to go to the next iteration of the loop
}
node_type *prev = first;
node_type *curr;
for (curr = (*first).next ; curr != nil ; curr = (*curr).next)
{
if ((*curr).id > ident)
break; // causes the for loop where its located to terminate
prev = curr;
}
(*newnode).next = curr;
(*prev).next = newnode;
}
q = first;
system("cls");
cout<<"The list contains: \n";
cout.setf(ios::showpoint|ios::fixed|ios::right);
cout.precision(1);
while(q!=nil)
{
cout<<"Name is: "<<(*q).nam<<endl;
cout<<"GPA is: "<<(*q).id<<endl;
q= (*q).next;
}
}
//=====================================================================
//This function is used to get the data.
//=====================================================================
void getData(float& gpa, char name[15])
{
cout<<"Enter Last Name: ";
cin>>name;
cout<<"Enter G.P.A: ";
cin>>gpa;
}
it works fine for the GPA but it prints out garbage for the name... can someone please help me?
i will rate ur response!!