// JP opened flex table

Click to See Complete Forum and Search --> : CLass Linked list


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!!

beachbum3159
December 4th, 2001, 01:46 AM
i have gotten the program to read in characters now but with one problem... here is the code:

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
#include<string>
using namespace std;
void amount(int x);
void getData(float& gpa, string& name);
const int nil = 0;

class node_type
{
public:
float id;
string nam;
node_type *next;
};

int main()
{
int k(0), i;
node_type *first, *p, *q, *newnode;
float ident;
string namet;

amount(k);

first = new node_type;
p = first;
getData(ident, namet);
(*first).id = ident;
(*first).nam = namet;
(*first).next = nil;

for(i = 1; i<=k; i++)
{
getData(ident, namet);
newnode = new node_type;
(*newnode).id = ident;
(*newnode).nam = namet;
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<<"\n ALL GPA's:\n";
cout<<"NAME GPA\n";
cout<<"------------------";
cout.precision(1);
while(q!=nil)
{
cout<<"\n"<<setiosflags(ios::left|ios::showpoint|ios::fixed)
<<setw(15)<<(*q).nam
<<setw(10)<<(*q).id;
q= (*q).next;
}
q = first;
p = (*first).next;
while( p != nil)
{
if ((*first).id<3.0)
{
delete first;
first = p;
q = first;
p = (*p).next;
}
else
break;
}
if(((*first).id<3.0)&&(p == nil))
{
delete first;
q = nil;
}
else
q = first;

cout<<"\n\n\n GPA's ABOVE 3.0: \n";
cout<<"NAME GPA\n";
cout<<"------------------";
cout.precision(1);
if (q != nil)
{
while(q!=nil)
{
cout<<"\n"<<setiosflags(ios::left|ios::showpoint|ios::fixed)
<<setw(15)<<(*q).nam
<<setw(10)<<(*q).id;
q= (*q).next;
}
}
else
cout<<"\nNO GPA's ABOVE 3.0!\n\n";
cout<<"\n\n";
return 0;
}
//=====================================================================
//This function is used to get the data.
//=====================================================================
void getData(float& gpa, string& name)
{
cout<<"Enter Last Name: ";
getline(cin, name, '\n');
cout<<"Enter G.P.A: ";
cin>>gpa;
cin.ignore();

}
//=====================================================================
//This function asks how many times it should ask for a name and gpa.
//=====================================================================
void amount(int x)
{
cout<<"How many students would you like to process? ";
cin>>x;
x--;
}




when i run the program this code asks "how many times to be run" then asks for the first name and after i hit enter it goes right to the dispaly of the sorted GPA's and prints out garbage... if i take the " amount function out (void amount(int x)" then it will work but after i enter the name i have to hit enter two times for it to ask for the gpa... any clues??

nikb
December 4th, 2001, 07:19 AM
Hello,

I have a few comments I'd like to make. So, here we go:

- Why do you use the construct "(*newnode).id = ident;" when C++ offers the direct way of doing it: "newnode->id=ident;"?

- You're calling amount, and never store the return value... Notice, that you're passing the variable in, but not by reference. You should rewrite amount as one of the two options:int amount()
{
int x;
cout<<"How many students would you like to process? ";
cin>>x;

return x-1;
}

orvoid amount(int &x)
{ // this is your original code, but it forces x to be passed by reference
cout<<"How many students would you like to process? ";
cin>>x;
x--;
}

I prefer the first version.


Good luck,

-n

beachbum3159
December 4th, 2001, 11:04 AM
i tried what you said but it didnt work..thanks anyway

Dmitry Zemskov
January 14th, 2002, 11:26 AM
This is the problem line -

(*first).nam[15] = namet[15];




You are just copying the byte with index 15 here.

//JP added flex table