GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2005, 23:07
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

i need a little help


the code down here is one i wrote to convert a digital date to an analogue one.if you compile and run it it will show the wrong month wich is probably due to the fact that arrays start at 0 and not at 1,to compensate for that i used a for loop ithin main to handle this and it worked exept that the program enters the loop and nver comes out.if anyone could fix this little problem for me i'll really appreciate it.

CPP / C++ / C Code:
//this program takes a digital date and outputs it in analogue!!!!!!

#include<iostream>
using namespace std;

				
void getinputs(int,int ,int);
void printoutputs(int, int, int);

int day,month,year;
 char *strmonth[12] = {"January", "February", "March", "April", "May", "June",
		    "July", "August", "September", "October", "November", "December"};

//Select day of Week



int main()
{

	cout << "THE PROGRAM WILL TRANSFORM YOUR DIGITAL DATE TO ANALOGUE"<<endl;
	cout << "please enter a digital date,seperated by spaces !!! "<<endl;

	getinputs(month,day,year);
	printoutputs(month,day,year);
	
		
		
	
return 0;
}

void getinputs(int,int,int)
{
	cin >>month>>day>>year;
}

void printoutputs(int, int, int)
{
	for (int i=1;i<=12;i++)
		*strmonth[i];
	cout <<"the date you entered was \n"<<"  "<<strmonth[month]<<"  "<<day<<"  "<<year<<endl;
	cout <<"let's do another \n";
}
Last edited by LuciWiz : 07-Mar-2005 at 06:22. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 07-Mar-2005, 07:49
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by ap6118
CPP / C++ / C Code:
//this program takes a digital date and outputs it in analogue!!!!!!
char *strmonth[12] = {"January", "February", "March", "April", "May", "June",
		    "July", "August", "September", "October", "November", "December"};

Doing this, you allocated 12 elements to your array, starting from index 0 to index 11.
Then, you try to access index 12 of the array - but that area of memory doesn't belong to you.

Quote:
Originally Posted by ap6118
CPP / C++ / C Code:
	for (int i=1;i<=12;i++)
		*strmonth[i];

Instead, start from 0 - this really is allocated, up until 11 (<=11, or <12).

So, what are you trying to do here? (I changed the indexes, as you see, so it won't crash or hang)

CPP / C++ / C Code:
	for (int i=0; i<12; i++)
		*strmonth[i];

Is this a test?

Well, as for the problem with the indexes, there is a quick fix: you have a 0-based array, but receive data that considers 1 as the starting index; just subtract 1 from the given data and you will have the 2 synchronized.

CPP / C++ / C Code:
	cout << "the date you entered was \n" << "  " << strmonth[month-1] << "  " << day 
		<< "  " << year << endl;
	cout <<"let's do another \n" << endl;

As a side note, I am really reticent about the use of global variables and cin, although if you are really good it might not get you into trouble. I guess in plain C global variables are OK (some say so, I don't agree ), but in C++ you might want to try and avoid them... (Now, I don't know which of them you are trying to use, since you mix them together. In C++ I wouldn't declare the function prototype in the same file - then again, I would use objects so the functions would really be methods )

I see you are using formal parameters in your functions, but actually you are not using them

I'm guessing you had problems with the readout, because you gave the function an int, and the data didn't change after getinputs was called. The trick is to use passing by reference (please read this!)

You could do this by using pointers or references (in case you work in C++)

With pointers:

CPP / C++ / C Code:
//this program takes a digital date and outputs it in analogue!!!!!!

#include<iostream>
using namespace std;

void getinputs(int *, int *, int *);
void printoutputs(int, int, int);

//int day,month,year; I declared those in main
char *strmonth[12] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};

//Select day of Week

int main()
{
	int day, month, year;
	cout << "THE PROGRAM WILL TRANSFORM YOUR DIGITAL DATE TO ANALOGUE" << endl;
	cout << "please enter a digital date, separated by spaces !!! " << endl;

	getinputs(&month, &day, &year);
	printoutputs(month, day, year);

	return 0;
}

void getinputs(int * month, int * day, int * year)
{
	cin >> *month >> *day >> *year;
	//Some data validation would be in order 
}

void printoutputs(int month, int day, int year)
{
	/*
	for (int i=0; i<12; i++)
	{
	*strmonth[i];
	}
	*/
	cout << "the date you entered was \n" << "  " << strmonth[month-1] << "  " << day 
		<< "  " << year << endl;
	cout <<"let's do another \n" << endl;
}

Using references:

CPP / C++ / C Code:
#include<iostream>
using namespace std;

void getinputs(int &, int &, int &);
void printoutputs(int, int, int);

//int day,month,year; I declared those in main
char *strmonth[12] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};

//Select day of Week

int main()
{
	int day, month, year;
	cout << "THE PROGRAM WILL TRANSFORM YOUR DIGITAL DATE TO ANALOGUE" << endl;
	cout << "please enter a digital date, separated by spaces !!! " << endl;

	getinputs(month, day, year);
	printoutputs(month, day, year);

	return 0;
}

void getinputs(int & month, int & day, int & year)
{
	cin >> month >> day >> year;
}

void printoutputs(int month, int day, int year)
{
	/*
	for (int i=0; i<12; i++)
	{
		*strmonth[i];
	}
	*/
	cout << "the date you entered was \n" << "  " << strmonth[month-1] << "  " << day 
		<< "  " << year << endl;
	cout <<"let's do another \n" << endl;
}

I went along with the code you wrote so far so you would understand it, but you may want to stick to C or C++...
I hope this is OK.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 07-Mar-2005, 09:20
ap6118 ap6118 is offline
Junior Member
 
Join Date: Feb 2005
Posts: 62
ap6118 is on a distinguished road

thanks a bunch


the pointer method make a lot more sence and is a lot more efficient.thanks a lot,i read a little more on pointers and i think i'm ready to play around with them now.
thanks a bunch
ciao
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.