Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » whats wrong with my PrintInfo() function
Closed Thread
Old 02-03-2005, 05:30 AM   #1 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Default whats wrong with my PrintInfo() function

ive made a program that keeps data using list structure, but theres something wrong with the program that cause the "print" function didnt work well .... please help^^

//dkoperasi.h ------------------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#define nil NULL
#define Next(L) (L)->Next

struct Node
{
int no_brg;
char nama_brg[20];
int jml_brg;
int harga;
Node * Next;
};
typedef Node* address;
typedef Node* List;

void CreateEmpty(List &L);
bool IsEmpty(List L);
bool isOneElmt(List L);
void InsertFirst(List &L, address P);
address alokasi(int noBrg, char namaBrg[20], int jmlBrg, int Harga);
void PrintInfo(List L); // [B]i think the problem isin here^^[B]
void InsVFirst(List &L, int noBrg, char namaBrg[20], int jmlBrg, int Harga);
void DelFirst(List &L);

//dkoperasi.cpp---------------------------------------------------------------------------------
#include "dkoperasi.h"

void CreateEmpty(List &L)
{
L=nil;
}

bool IsEmpty(List L)
{
return (L==nil);
}

bool isOneElmt(List L)
{
return (Next(L)==nil);
}

void InsertFirst(List &L, address P)
{
Next(P)=L;
L=P;
}

address alokasi(int noBrg, char namaBrg[20], int jmlBrg, int Harga)
{
address P;
P=new(Node);
if(P != nil)
{
(P)->no_brg;
(P)->nama_brg;
(P)->jml_brg;
(P)->harga;
Next(P)=nil;
}

return (P);
}

void PrintInfo(List L)
{
if(!IsEmpty(L))
{
cout<<((L)->nama_brg)<<endl;
cout<<((L)->no_brg)<<endl;
cout<<((L)->jml_brg)<<endl;
cout<<((L)->harga)<<endl;
PrintInfo(Next(L));
}
}

/*int no_brg;
char nama_brg[20];
int jml_brg;
int harga;*/

void DelFirst(List &L)
{
address P;
P=L;
L=Next(L);
delete(P);
}

void InsVFirst(List &L, int noBrg, char namaBrg[20], int jmlBrg, int Harga)
{
address P;
P=alokasi(noBrg,namaBrg,jmlBrg,Harga);
InsertFirst(L,P);
}

//mkoperasi.cpp ----------------------------------------------------------------------------------------------------------------------------

/*
nama : Vidya Putra
no.mhs : 03507
kelas : C
*/

#include "dkoperasi.h"
int main()
{
int pilih;
List l;
int jumlah_barang;
int no_barang;
int harga_barang;
char nama_barang[20];

CreateEmpty(l);

do
{
system("CLS");
cout<<" Menu "<<endl;
cout<<"1. Insert First List "<<endl;
cout<<"2. Delete First List "<<endl;
cout<<"3. Print Info List "<<endl;
cout<<"4. test "<<endl;
cout<<"0. End of Program "<<endl;
cout<<"Masukkan pilihan anda : "<<endl;
cin>>pilih;

switch(pilih)
{
case 1:
{
cout<<"Masukkan data : "<<endl;

cout<<"nama barang : ";
cin>>nama_barang;

cout<<"jumlah barang : ";
cin>>jumlah_barang;

cout<<"no barang : ";
cin>>no_barang;

cout<<"harga barang : ";
cin>>harga_barang;

InsVFirst(l,no_barang,nama_barang,jumlah_barang,ha rga_barang);
cout<<"Data diterima"<<endl;
getch();
}break;
case 2:
{
DelFirst(l);
cout<<"Data terdepan dihapus"<<endl;
getch();
}break;
case 3:
{
cout<<"Data dalam list : "<<endl;
PrintInfo(l);cout<<endl;
getch();
}break;
case 4:
{
cout<<(l)->nama_brg<<endl;
getch();
}
}
}while(pilih != 0);
return(0);
}
vidyaputra is offline  
Old 02-03-2005, 10:55 AM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

I don't know of anything wrong programatically, but logically I think you should re-work your code. Instead of calling the PrintInfo() function recursivly I think you should use a for or foreach statement to loop through all the items in your list....
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 02-03-2005, 10:30 PM   #3 (permalink)
 
Newb Techie

Join Date: Aug 2004

Posts: 22

vidyaputra

Default

thanks for the advise ...
actually ive found the problem, its it the alokasi(alocation) function, i havent assign the input.^^(clumpsy)

address alokasi(int noBrg, char namaBrg[20], int jmlBrg, int Harga)
{
address P;
P=new(Node);
if(P != nil)
{
(P)->no_brg; //suppose to assign the input value
(P)->nama_brg;
(P)->jml_brg;
(P)->harga;
Next(P)=nil;
}

------------------------------------------------------------------------------------
but there is another problem, if i want to allow the user to edit the data that they've insert ... should i make a function or there are other way???

because ive made this "Edit" function, but every time the function accessing the data on the list ... the program will terminated /error.

this is what the "Edit" function do:
1. resiving an address return from "Search" function.
2. reciving data input
3. accessing the node, by its address that recived....

-vip-
vidyaputra is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On