printing data in class pointers

i am having trouble printing data got thru dynamic mem alloc in c++... this is the code please help......please note that if i declare both power and coeff as int it seems to work fine


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
class polynomial
{
public: int power;
float coefficient;
class polynomial *nextpolynomial;

};
class polynomial *first,*next,*newnext;
void input()
{


first = (class polynomial *)malloc(sizeof(class polynomial *));
cout<<"\nEnter the coefficient";
cin>>first->coefficient;
cout<<"\nEnter the power";
cin>>first->power;
first->nextpolynomial=NULL;
}
void nextinput()

{
next=first;
while(next->nextpolynomial!=NULL)
next=next->nextpolynomial;
newnext = (class polynomial *)malloc(sizeof(class polynomial *));
cout<<"\nEnter the coefficient";
cin>>newnext->coefficient;
cout<<"\nEnter the power";
cin>>newnext->power;
next->nextpolynomial=newnext;
next=newnext;
next->nextpolynomial=NULL;




}
void displayeqn()
{


newnext=first;
while(newnext!=NULL)
{

cout<<newnext->coefficient<<"x^";
cout<<newnext->power<<" ";
newnext=newnext->nextpolynomial;
}


}
void line()
{
int i;
for(i=0;i<80;i++)
cout<<"*";
cout<<"\n\n";
}
void heading()
{
cout<<"\t\t POLYNOMIAL DIFFRENTIATION USING LINKED LIST\n\n";
}
void main()
{
clrscr();
char userch;
line();
heading();
line();
input();
x:cout<<"\n Do u want to continue?please type y or n:";
cin>>userch;
switch(userch)
{
case 'y':{
nextinput();goto x;
}

case 'n':break;
default :cout<<"\ninvalid choice";goto x;
}
displayeqn();
getch();
}
Use [ code ] [ /code ] tags. And be a bit more descriptive about the problem - what do you expect to happen, and what happens? Also, only post the relevant section of your code.
am sorry am new to this forum and to pgmming...to be honest i do not know which part of the code is in error ..thaz why i put in the whole code...i am trying to create a code for polynomial differentiation using linked lists((i have just got the input in the above code and displaying it)). The value of power prints just fine but the coefficient value is printing some junk values...please help...i have reposted the code taking out irrelevant (( i think)) code...thanks a lot for replying

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
class polynomial
{
       public:	int	power;
		float	coefficient;
		class polynomial *nextpolynomial;

};
class polynomial       *first,*next,*newnext;
void input()
{


			first = (class polynomial *)malloc(sizeof(class polynomial *));
			cout<<"\nEnter the coefficient";
			cin>>first->coefficient;
			cout<<"\nEnter the power";
			cin>>first->power;
			first->nextpolynomial=NULL;
}
void nextinput()

{
		next=first;
		while(next->nextpolynomial!=NULL)
		 next=next->nextpolynomial;
		newnext = (class polynomial *)malloc(sizeof(class polynomial *));
		cout<<"\nEnter the coefficient";
		cin>>newnext->coefficient;
		cout<<"\nEnter the power";
		cin>>newnext->power;
		next->nextpolynomial=newnext;
		next=newnext;
		next->nextpolynomial=NULL;




}
void displayeqn()
{


	newnext=first;
	while(newnext!=NULL)
	{

		cout<<newnext->coefficient<<"x^";
		cout<<newnext->power<<" ";
		newnext=newnext->nextpolynomial;
	}


}
void main()
{
	clrscr();
	char	userch;
		input();
	       x:cout<<"\n Do u want to continue?please type y or n:";
		cin>>userch;
		switch(userch)
		{
			case 'y':{
				     nextinput();goto x;
				 }

			case 'n':break;
			default :cout<<"\ninvalid choice";goto x;
		}
		displayeqn();
	getch();
}
Last edited on
:)) he means: use [code] [/code] and use your code between it...
Last edited on
sorry .. have done it now
can somebody help me pls....
Topic archived. No new replies allowed.