Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-14-2003, 06:05 AM   #1
Member (8 bit)
 
Join Date: Aug 1999
Location: Florida
Posts: 237
Send a message via AIM to revelation
Trouble with passing something in C++

This probably looks like a simple problem to most of you, but I am struggling in a class in C++. I am to write a program that averages the test scores that are entered. The first function is to ask for the number of scores and then dynamically allocate memory for that number of entries. I have done the program but I get 0 for an average because I am not passing anything back to the main program from the function that allocates the memory and asks for the scores...
revelation is offline   Reply With Quote
Old 05-14-2003, 06:51 AM   #2
Member (8 bit)
 
cameroth's Avatar
 
Join Date: Sep 2002
Posts: 158
Send a message via AIM to cameroth
I'm learning c++ as well, so this may not be correct. First of all, how are you passing the variables? referece, passing a copy, using return, or something else? Also, are you using vectors, matricies, or just normal variables? It might be easier to see the mistake, if we could see the code.
cameroth is offline   Reply With Quote
Old 05-15-2003, 06:14 AM   #3
Member (8 bit)
 
Join Date: Aug 1999
Location: Florida
Posts: 237
Send a message via AIM to revelation
I am using variables. I guess what I am sending into the main program is the location of the memory space allocated by the function. This is a pointer to that location. I think I am just using the wrong code for the funcion or the call to it.

The funcion is just to ask the user how many scores to be entered (I made these of type int). It then would allocate enough memory and do an error check to make sure there is enough memory. This is then passed into the main program that asks for the grades, sorts them, and then gives the average grade. The instructor said that we could write this part as seperate functions or as just do this part all in main.
revelation is offline   Reply With Quote
Old 05-15-2003, 10:52 AM   #4
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
Are you required to do error checking? Because it's unlikely that you could really allocate too much memory on a modern machine.

Show the code or we can't help.
Paul Victorey is offline   Reply With Quote
Old 05-16-2003, 06:12 AM   #5
Member (8 bit)
 
Join Date: Aug 1999
Location: Florida
Posts: 237
Send a message via AIM to revelation
Here is what I have so far....





#include
void allocate(int *);


void main (void)

{
int *scores,total=0, average;
int numScores, count;

allocate(scores);


//Get the test scores to be averaged
cout<<"Please enter the test scores: \n";

for (count=0;count {
cout<<"Score "<<(count+1)<<": ";
cin>>scores[count];
}

//Display scores...
cout<<"The Scores You Entered Are:\n";
for (count=0;count {
cout<<"Day "<<(count+1)<<": ";
}

//Display average score...
for (count=0;count {
total +=scores[count];
}

average=total/numScores;

cout<<"The Average Score Is: "<
}





void allocate(int *scores)

{

int numScores=0, count;

cout<<"How many test scores do you want to enter: \n";
cin>>numScores;

scores=new int[numScores]; //allocate memory
if (scores==NULL) //test for null pointer
{
cout<<"Error allocating memory!\n";
return;
}


}
revelation is offline   Reply With Quote
Old 05-16-2003, 07:40 AM   #6
Member (8 bit)
 
cameroth's Avatar
 
Join Date: Sep 2002
Posts: 158
Send a message via AIM to cameroth
well, i the use visual c++ 5.0 compiler, so i can't say for sure if these ideas are right. also, i leaned about pointers a lot earlier this year, so i don't really remember how they work. what i do for my compiler is #include. also, void allocate(int &pointer *); might work better because then the inputted value will be passed back to the main function. make sure you do that at the beginning, and when you call the function. also, it looks like you've got the { and }'s mixed up. make sure that each function is enclosed in the {}'s. I'll write back again later when I get home and have time to look at it. i'm sure that paul will be able to fix your program no problem if i can't figure out what's wrong.

Last edited by cameroth; 05-16-2003 at 08:52 AM.
cameroth is offline   Reply With Quote
Old 05-16-2003, 06:41 PM   #7
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
Yeah, your pointer never makes it back to the main program. You can either use a pointer to a pointer, or pass by reference, or return the pointer. The problem is, you're passing a *copy* of the pointer, so you can't alter where the original pointer points. You could modify data it pointed to (it actually doesn't point anywhere in particular though) but you can't alter the pointer itself like you are doing.

Probably, the most intuitive is to do this:

Code:
int * allocate(){
   int * scores;
.
.
.
   return scores;
}
Paul Victorey is offline   Reply With Quote
Old 05-17-2003, 02:08 PM   #8
Member (8 bit)
 
Join Date: Aug 1999
Location: Florida
Posts: 237
Send a message via AIM to revelation
I just set up the function as you suggested. What then would be the call to this function?
revelation is offline   Reply With Quote
Old 05-17-2003, 03:17 PM   #9
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
scores = allocate();
Paul Victorey is offline   Reply With Quote
Old 05-17-2003, 03:44 PM   #10
Member (8 bit)
 
Join Date: Aug 1999
Location: Florida
Posts: 237
Send a message via AIM to revelation
I know I must be doing something wrong....
Here is what I have now:


Code:
#include;
int* allocate();

void main (void)

{

    int *scores;

	int total=0, average,count, numScores;
	count;

	scores=allocate();

	//Get the test scores to be averaged
	cout<<;"Please enter the test scores: \n";

	for (count=0;count>scores[count];
	}

	//Display scores...
	cout<<"The Scores You Entered Are:\n";
	for (count=0;count>numScores;

	scores=new int[numScores];  //allocate memory
	if (scores==NULL)           //test for null pointer
	{
		cout<<"Error allocating memory!\n";
		
	}
	return scores;

}
Edited by moderator: Tried (unsuccessfully) to fix problems with code formatting...

Last edited by Paul Victorey; 05-17-2003 at 08:01 PM.
revelation is offline   Reply With Quote
Old 05-18-2003, 04:22 AM   #11
Member (8 bit)
 
cameroth's Avatar
 
Join Date: Sep 2002
Posts: 158
Send a message via AIM to cameroth
off the top of my head, the first mistake that i see is that you forgot to close your parantheses around the for loop in your main function, and you don't need a ; after it, because that would mess it up. also, i'm not sure if you want the if structure in the for loop, but if you do, you need brackets around all of it. i'm not sure how or if #include; works, but i'd do #include; finally, at the beginning of the main function, instead of void main (void), i'd just do main() at the end of the main function, instead of return scores, i'd do return 0;
cameroth is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

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



All times are GMT -5. The time now is 07:59 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2