I need help with functions.

Hi, I'm doing an assignment for school to do with functions and procedures. SO basically we are asked to make a program that asks you to put in your test percentages and the program tells you the grade of each input, the average, highest and lowest values. We are supposed to keep track of the results with an array or vector, but I'm not quite sure how to do that.

Here is what I've done so far.
---------------------------------

#include <iostream>
using namespace std;

void Determine_Grade(int intInput);
int Find_Highest(int intInput);

int main ()
{
int intInput;
int Highest;

cout << "Enter result '1' (or -1 if no more result): \n";
cin >> intInput;
while (intInput != -1)
{
Determine_Grade(intInput);
cout << "Enter result '1' (or -1 if no more result): \n";
cin >> intInput;
}



Highest = Find_Highest(intInput);


return 0;
}

void Determine_Grade(int intInput)
{
if (intInput >= 90)
cout << "Grade B will be assigned to this result.\n";

else if (intInput >=70)
cout << "Grade B will be assigned to this result.\n";

else if (intInput >=60)
cout << "Grade C will be assigned to this result.\n";

else
cout << "Grade U will be assigned to this result.\n";

}

int Find_Highest(int intInput)
{


}

-------------------------------------

So Ive completed one of the functions, and it works well. As you can see, determining the highest input value is left blank. I can't seem to figure out how to make the program tell apart each input value. Normally what I would do to find out the highest value would be with an if/else statement (if (intFirstNumber > intSecondNumber)).
But since all the values inputed are under "intInput", I can't figure out how to do it..

Thank you for your time :)
well you are doing it well so far at first glance,

you would need an array to store the data, you can pass the array by reference which will make changes of that array, pull data, etc depending on what you want to do with it..

in order to do that,

1
2
3
4
5
returnType myFunction (int *pointer)
{
    // do some stuff

}


not sure what you have learned yet, but an array is actually a pointer to the first element of the array, so you can send that to the function and use that with in, any changes made to the array in the function will be changed in the whole program.

In the future, use code blocks to make your code more readable, hope this helps
I'm still confused..
How/where would I use that?
or would it be easier to use vectors to store my input data?
vectors are more complicated in my opinion.

look up array to functions
@oonej: Arrays are not pointers. That is not pass-by-reference.

@Tessycakes:

1
2
3
#include <vector>

vector<int> v ;



To add something to the end of a vector

v.push_back(input) ;


To access what you've stored:

cout << v[index] ;

where index is between 0 and v.size()-1.

okay so I'm not quite sure if I'm doing this right.
I put
v.push_back(input) ;
If I'm correct, it should add the value to the end of the counter right?
Then I'm not to sure about the index value. Should I change up the values of my program?
oops, i meant to say that i put v.push_back(input) ; into my while loop.
ok so I shortened my program to play around with the vector.
Here it is,
_________________________
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
int intInput=0;
vector<int> V;
cout << "Enter result '1' (or -1 if no more result): \n";
cin >> V[intInput];
while (intInput != -1)
{
V.push_back(V[intInput]) ;
cout << "Enter result '1' (or -1 if no more result): \n";
cin >> V[intInput];
}

cout << V[intInput] ;
return 0;
}
_____________________________________
Sorry about posting it like this, I don't know how to post it in blocks.
So everytime I try to run the above program, I get errors (vector subscripts out of range, standard C++ libraries out of range,..).
I've been using cire's help and my textbook.
It seems like this wouldn't work since my vector doesn't have a set size. Most example in my text use a for loop, with a counter and a known vector size. I'm really confused with this, am I missing something?
I know I have a lot of questions, I just can't seem to find the answer in my text.
Here's a quick example that illustrates the ways you need to use the vector. I didn't run this at all, so hopefully there are no errors lurking within.

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
#include <iostream>
#include <vector>

int main()
{
     using std::vector ;
     using std::cin ;
     using std::cout ;

     vector<int> v ;

     int value ;

    std::cout << "Enter values (negative value to stop): " ;

     // fill up v
     while ( (cin >> value) && value > 0 )
          v.push_back(value) ;

     if ( v.size() == 0 )  // we got no valid input!
     {
           std::cerr << "No valid input encountered.  Exiting.\n" ;
           return 0 ;
     }
    
     // find the sum of all the values in v.
     int total = 0 ;
     for ( int i=0 ;  i< v.size() ; ++i )
          total += v[i] ;

     cout << "n\Total: " << total << '\n' ;
}

Thank you Cire :)

Seeing your example really helped!
Topic archived. No new replies allowed.