Discussion:
Please help fix this pointer program!!
(too old to reply)
crashandburn
2005-03-03 05:18:16 UTC
Permalink
/* The purpose of this program is to take three integer quantities,
indicating month,day and year, and then displaying the corresponding day
of the week,
the month,the day and the year in a more legible manner*/

#include<iostream.h>
#include<conio.h>

void readinput (int *pmm,int *pdd, int *pyy); //function prototype//
int *convert (int pmm,int pdd,int pyy); //function prototype//

void main()
{
int mm,dd,yy;
int day_of_week;
int *pmm,*pdd,*pyy;

pmm=&mm;
pdd=&dd;
pyy=&yy;

static char *weekday[]={"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
static char * month[]={"January","February","March","April",
"May","June","July","August","September","October","November",
"December"};
//opening message//

cout<<"DATA CONVERSION ROUTINE\nTO STOP,ENTER 0 0 0"<<endl<<endl;

readinput(pmm,pdd,pyy);

while(*pmm>0)
{
day_of_week = *convert(pmm,pdd,pyy);
cout<<*weekday[day_of_week]<<","<<month[*pmm-1]<<*pdd<<","<<*pyy;
readinput(pmm,pdd,pyy);
}
}

void readinput(int *pmm,int *pdd,int *pyy)
{
cout<<endl<<"Enter mm dd yyyy: ";
cin>>*pmm>>*pdd>>*pyy;
return;
}
//numerical conversion//

int convert(int *pmm,int *pdd,int *pyy)
{

long *pndays;
long *pncycles;
int *pnyears;
int *pday;


*pyy-=1900;
*pndays=(long)(30.42*((*pmm)-1))+*pdd;
if((*pmm)==2) ++(*pndays);
if(((*pmm)>2)&&((*pmm)<8)) --(*pndays);
if(((*pyy%4)==0) && ((*pmm)>2))++*pndays);


*pncycles= (*pyy)/4;
*pndays+= (*pncycles)*1461;

*pnyears= (*pyy)%4;
if((*pnyears)>0)
(*pndays)+= 365 * (*pnyears)+1;
if ((*pndays)>59) --(*pndays);
*pday= (*pndays)%7;
return(*pday);
}
I tried to write this program using all pointers variables. No value
should passed or printed using normal variables ,only pointer should be
used.but unfortuantely this program shows millions of errors. can someone
help fix those..??
Randy
2005-03-03 17:40:27 UTC
Permalink
crashandburn wrote:
...
Post by crashandburn
int convert(int *pmm,int *pdd,int *pyy)
{
long *pndays;
long *pncycles;
int *pnyears;
int *pday;
These are pointers. But what do they point to?

...
Post by crashandburn
I tried to write this program using all pointers variables. No value
should passed or printed using normal variables ,only pointer should be
used.but unfortuantely this program shows millions of errors. can someone
help fix those..??
Randy
Jonathan Bartlett
2005-03-08 18:26:30 UTC
Permalink
Many problems, just from looking at the surface:

1) convert is defined in one location as returning "int" and another
location as "int *". It looks like it's returning an int.

2) You don't actually have any memory you are pointing to. Remember,
pointers point TO other data locations. If you haven't defined any data
locations, YOU AREN'T POINTING TO ANYTHING!!!

long *a;
*a = 2;

is meaningless, because a doesn't yet point anywhere. If instead you wrote

long *a;
long b;

a = &b;
*a = 2;

Then you are setting the value of "b" to 2. If you want heap-allocated
memory instead of stack-allocated memory, you can use malloc:

long *a;
a = malloc(sizeof(long));
*a = 2;

3) Lots of language errors. The reason you are getting so many errors
is likely that you are compiling with gcc instead of g++. Some nits:
a) There is no reason to include conio.h
b) iostream should be included as <iostream>, not <iostream.h>
c) main should return an int

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017

Continue reading on narkive:
Loading...