|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (8 bit)
|
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...
|
|
|
|
|
|
#2 |
|
Member (8 bit)
|
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.
|
|
|
|
|
|
#3 |
|
Member (8 bit)
|
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. |
|
|
|
|
|
#4 |
|
Member (12 bit)
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. |
|
|
|
|
|
#5 |
|
Member (8 bit)
|
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; } } |
|
|
|
|
|
#6 |
|
Member (8 bit)
|
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
Last edited by cameroth; 05-16-2003 at 08:52 AM. |
|
|
|
|
|
#7 |
|
Member (12 bit)
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;
}
|
|
|
|
|
|
#8 |
|
Member (8 bit)
|
I just set up the function as you suggested. What then would be the call to this function?
|
|
|
|
|
|
#9 |
|
Member (12 bit)
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
|
scores = allocate();
|
|
|
|
|
|
#10 |
|
Member (8 bit)
|
I know I must be doing something wrong....
Here is what I have now: Code:
#include Last edited by Paul Victorey; 05-17-2003 at 08:01 PM. |
|
|
|
|
|
#11 |
|
Member (8 bit)
|
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
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|