Help with functions

Pages: 12
Im having a problem with this code I get a invalid conversion from int to int error can anyone let me know what i am doing wrong?

#include <iostream>
using namespace std;

int curve(int);

int main ()
{

const int NUM_SCORES = 6;
int scores[NUM_SCORES];
int count;

int grades;

cout << "Enter the scores for the " << NUM_SCORES << " tests: ";

for (count = 0; count < NUM_SCORES; count++)
cin >> scores[count];

cout << "The scores you entered are: ";

for (count = 0; count < NUM_SCORES; count++)
cout << " " << scores[count];

cout << endl;
cout << "The curved scores are: ";

for (count = 0; count < NUM_SCORES; count++)
grades = curve(scores);
cout << " " << grades;
return 0;
}

int curve(int scores)
{
int curve = 10;

scores + curve;
}
those types of messages are cryptic to read at first, but they tell you exactly what the problem is. It probably really says cant convert int* to int or int[] to int or something, post the exact message next time.

here:
grades = curve(scores); //curve takes an int. scores is not an int, scores is an array.
also, I HIGHLY recommend not using the same variable name twice in one file. You can do it, but it just causes problems for the reader and writer. Here, you have 2 versions of 'scores'.
ok i get that now but how would you recommend solving this? Would i call each array scores[1-6] individually and parse them or would there be a simpler way? The scores have to be passed to a function and sent back to main or else i would've had the code done by now. also can you explain it so a pea brain like me can understand it? I really do want to learn this.
sorry bout all this people do tend to tell me i am overthinking things.
I don't know what function int curve(int); is supposed to do. The declaration says that it receives a single integer as a parameter and returns a single integer. I just made a simple version which returns the input multiplied by 2.

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
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

int curve(int);

int main ()
{

    const int NUM_SCORES = 6;
    int scores[NUM_SCORES];
    int count;

    int grades;

    cout << "Enter the scores for the " << NUM_SCORES << " tests: ";

    for (count = 0; count < NUM_SCORES; count++)
        cin >> scores[count];

    cout << "The scores you entered are: ";

    for (count = 0; count < NUM_SCORES; count++)
        cout << " " << scores[count];

    cout << endl;
    cout << "The curved scores are: ";

    for (count = 0; count < NUM_SCORES; count++)
    {
        grades = curve(scores[count]);
        cout << " " << grades;
    }
}

int curve(int scores)
{
    int result = scores * 2;
    return result;
} 


Enter the scores for the 6 tests: 1
2
3
4
5
6
The scores you entered are:  1 2 3 4 5 6
The curved scores are:  2 4 6 8 10 12


Back to the function:
1
2
3
4
5
6
int curve(int scores)
{
    int curve = 10;

    scores + curve;
} 
There are several problems here. Line 3. int curve = 10; A variable is declared with the same name as the function itself. The compiler permits this, but it looks like a recipe for confusion. If we say 'curve' do we mean the function or the variable?

Line 5, scores + curve; this adds two integers, but doesn't do anything with the total. That line may as well not be there at all since it has no effect on anything.

Line 6. A serious problem. The closing brace of the function is reached, but it did not return any value. It promised in the function header that it would return an integer value. There should be a return statement there which does return whatever integer value is appropriate.
what you want to do is unclear.

if you want to process an array into one answer, you need something like:

1
2
3
4
5
6
7
int curve(int * s, int length)
{
    for(int j = 0; j < length; j++)
      do something to s[j];
     
     return some_answer;
}


if you want to process 1 entry of an array to get 1 answer each:

1
2
3
4
5
6
7
8
9
10
11
12
int curve(int s)
{ 
      do something to s;     
     return some_answer;
}

and call that in main with:
for(j = 0; j < size; j++)
 {
   result = curve(scores[j]);
   .. do something with result ..
}



you can combine the 2 ideas and return an array of answers, as a third approach.
which of these you need depends on what you want to do.

What i need to do is write a program to fill an array with six test scores, add 10 points to each score and display the revised score. Do not use a function. Just write it all in main( );

Then once I have accomplished that go back and put the calculation in a function. main() will prompt for and accept a value, pass the value to the function, but instead of returning the value with a return statement, try it with a call by reference (or a reference variable that refers to the memory location of the original value.) main() will display the result.
This would be alot simpler if i could return the value with a return statement.
so should i write the whole for statement in curve()? or am i wrong again?
try something like

void pbr_func(int &pbr) //& is pass by ref
{
pbr +=10; //changes whatever comes in
}

...
main
pbr(x);
so something like this?
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
33
34
35
36
37
38
#include <iostream>
using namespace std;

int curve(int);

int main ()
{

const int NUM_SCORES = 6;
int scores[NUM_SCORES];
int count;
int result;

cout << "Enter the scores for the " << NUM_SCORES << " tests: ";

for (count = 0; count < NUM_SCORES; count++)
cin >> scores[count];

cout << "The scores you entered are: ";

for (count = 0; count < NUM_SCORES; count++)
cout << " " << scores[count];

cout << endl;
cout << "The curved scores are: ";

for (count = 0; count < NUM_SCORES; count++)      //edited code
result = curve(int & scores);
cout << result << " ";                                               //end edited code

return 0;
}

int curve(int & scores)
{
scores += 10;
}
 
Last edited on
im down to one error code now thanks so very much for your help. I get this error: expected primary-expression before int. so its expecting me to put int into a function?
Last edited on
when i take the int out of the call it brings me back to the problem that you helped me with originally converting int to int.
I also tried this but gave me the error newScores was not declared in the scope:

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
33
34
35
36
37
#include <iostream>
using namespace std;

int curve(int);

int main ()
{

const int NUM_SCORES = 6;
int scores[NUM_SCORES];
int count;
int result;

cout << "Enter the scores for the " << NUM_SCORES << " tests: ";

for (count = 0; count < NUM_SCORES; count++)
cin >> scores[count];

cout << "The scores you entered are: ";

for (count = 0; count < NUM_SCORES; count++)
cout << " " << scores[count];

cout << endl;
cout << "The curved scores are: ";

for (count = 0; count < NUM_SCORES; count++)      //edited code
result = curve(newScores);
cout << result << " ";                                               //end edited code

return 0;
}

int curve(int & scores)
{
int newScores = scores + 10;
}
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
#include <iostream>

int curve(int);

int main ()
{
    const int NUM_SCORES = 6;
    int scores[NUM_SCORES];

    std::cout << "Enter the scores for the " << NUM_SCORES << " tests: ";
    for( int& v : scores ) std::cin >> v ; // http://www.stroustrup.com/C++11FAQ.html#for

    std::cout << "The scores you entered are: ";
    for( int v : scores ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    std::cout << "The curved scores are: ";
    for( int v : scores ) std::cout << curve(v) << ' ' ;
    std::cout << '\n' ;
}

int curve( int score )
{
    // return the 'curved' value of the score
    return score + 10;
}
Thanks JLBorges but I'm not allowed to use a return statement I have to call the function to main.
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
#include <iostream>

void curve( int& score ) ;

int main ()
{
    const int NUM_SCORES = 6;
    int scores[NUM_SCORES];

    std::cout << "Enter the scores for the " << NUM_SCORES << " tests: ";
    for( int& v : scores ) std::cin >> v ; // http://www.stroustrup.com/C++11FAQ.html#for

    std::cout << "The scores you entered are: ";
    for( int v : scores ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    // update the scores with the 'curved' value
    for( int& v : scores ) curve(v) ;

    std::cout << "The curved scores are: ";
    for( int v : scores ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

void curve( int& score )
{
    // update the score with its 'curved' value
    score += 10;
}
I also cannot use range based loops as im using C++ 98 mode im sorry to be a pain
Just change the range based loops into classical for loops, if you are forced to use legacy C++.

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

void curve( int& ) ;

int main ()
{
    const int NUM_SCORES = 6;
    int scores[NUM_SCORES];

    std::cout << "Enter the scores for the " << NUM_SCORES << " tests: ";
    for( int i = 0 ; i < NUM_SCORES ; ++i ) std::cin >> scores[i] ; 

    std::cout << "The scores you entered are: ";
    for( int i = 0 ; i < NUM_SCORES ; ++i ) std::cout << scores[i] << ' ' ;
    std::cout << '\n' ;

    // update the scores with the 'curved' value
    for( int i = 0 ; i < NUM_SCORES ; ++i ) curve( scores[i] ) ;

    std::cout << "The curved scores are: ";
    for( int i = 0 ; i < NUM_SCORES ; ++i ) std::cout << scores[i] << ' ' ;
    std::cout << '\n' ;
}

void curve( int& score )
{
    // update the score with its 'curved' value
    score += 10;
}
Last edited on
Ok i finally stopped getting errors when i call the function which is great but when I get the curved values i get trash.

in this one:
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
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int curve(int&);

int main ()
{

const int NUM_SCORES = 6;
int scores[NUM_SCORES];
int count;


cout << "Enter the scores for the " << NUM_SCORES << " tests: ";

for (count = 0; count < NUM_SCORES; count++)
cin >> scores[count];

cout << "The scores you entered are: ";

for (count = 0; count < NUM_SCORES; count++)
cout << " " << scores[count];

int result;

for( int i = 0 ; i < NUM_SCORES ; count++ ) 
result = curve( scores[count]) ;

cout << endl;
cout << "The curved scores are: ";

for (count = 0; count < NUM_SCORES; count++)  
cout << result << " ";

return 0;
}

int curve(int & scores)
{
scores += 10;
}


and this one returns the same values i input:
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
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

int curve(int&);

int main ()
{

const int NUM_SCORES = 6;
int scores[NUM_SCORES];
int count;
int result;

cout << "Enter the scores for the " << NUM_SCORES << " tests: ";

for (count = 0; count < NUM_SCORES; count++)
cin >> scores[count];

cout << "The scores you entered are: ";

for (count = 0; count < NUM_SCORES; count++)
cout << " " << scores[count];

for( int i = 0 ; i < NUM_SCORES ; count++ ) 
curve( scores[count]) ;

cout << endl;
cout << "The curved scores are: ";

for (count = 0; count < NUM_SCORES; count++)  
cout << scores[count] << " ";

return 0;
}

int curve(int & scores)
{
scores += 10;
}
closed account (48T7M4Gy)
@Shiro12 This might be what you are trying to do. 'curve' to me is a not-so-good name because it could mean a whole lot of things. The reason the output was strange is probably because you are adjusting an array and not a single value so the output is junk.

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
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

void adjust(int[], int[], int); // <---

int main ()
{
    
    const int NUM_SCORES = 3; // <--- because I'm lazy
    int scores[NUM_SCORES];
    
    int adjusted_scores[NUM_SCORES]; // <---
    
    int count = 0;
    
    cout << "Enter the scores for the " << NUM_SCORES << " tests: ";
    
    for (count = 0; count < NUM_SCORES; count++)
        cin >> scores[count];
    
    cout << "The scores you entered are: ";
    
    for (count = 0; count < NUM_SCORES; count++)
        cout << " " << scores[count];
    
   // int result = 0; // <---
    
   // for( int i = 0 ; i < NUM_SCORES ; count++ ) // <---
        adjust( scores, adjusted_scores, NUM_SCORES) ; // <---
    
    cout << endl;
    cout << "The curved scores are: ";
    
    for (int i = 0; i < NUM_SCORES; i++) // <---
        cout << adjusted_scores[i] << " ";
    
    return 0;
}

void adjust(int scores[], int adj_scores[], int limit) // <---
{
    for(int i = 0; i < limit; ++i)
        adj_scores[i] = scores[i] + 10;
}


Enter the scores for the 3 tests: 21 22 32
The scores you entered are:  21 22 32
The curved scores are: 31 32 42 Program ended with exit code: 0
Last edited on
Pages: 12