function not working

hi, i wrote this program, but the function i created is not working, it's showing error everytime i run the program, what do i need to fix here?

#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;
void gpa(double, double);

int main()
{
const double INVALID_GRADE = -1.0;
bool needInput = true; // control for my input loop
double gpa;
double grade;
char ch='y';


while(needInput) // while the program needs valid input
{
while(ch == 'y' || ch == 'Y')
{
// Prompt for user input
cout << "Enter the grade to be converted ( 0 - 100 ) : " ;
cin >> gpa; // attempt to get the value from the user
if(cin.fail()) // if cin failed
{
cout << "\nInvalid Input! Please enter a numeric value. "; // error msg
fflush(stdin); // clears the keyboard buffer
cin.clear(); // reset the cin object
cout << "Do you want to continue ? y / n " << endl;
}
else if(gpa <= INVALID_GRADE)
{
cout << "\nGrade entered is invalid "; // error msg
fflush(stdin); // clears the keyboard buffer
cin.clear(); // reset the cin object
cout << "Do you want to continue ? y / n " << endl;
}
else
{
needInput = false; // program has valid input, cause the loop to end
// Processing
// calculate using funcion
gpa(gpa, grade);
// output
cout << "GPA is " << gpa << endl;
cout << "Do you want to continue ? y / n " << endl;
cin >> gpa;
}

}// end of input loop
} // end of while loop

cout << endl << endl;
system("pause");
return 0;
}

void gpa(double gpa, double grade)
{

if(grade >= 89.5 && grade < 100) then
gpa = 5.0;
elseif(grade >= 84.5 && grade < 88.5) then
gpa = 4.5;
else if(grade) >= 79.5 ) && grade < 83.5) then
gpa = 4.0;
else if(grade >= 74.5 && grade < 78.5) then
gpa = 3.5;
else if(grade >= 69.5 ) && grade < 73.5) then
gpa = 3.0;
else if(grade >= 64.5 ) && grade < 68.5) then
gpa 2.5;
else if(grade >= 59.5 && grade < 63.5){
gpa = 2.0;
else if(grade >= 54.5 && grade < 58.5) then
gpa = 1.5;
else if(grade >= 49.5 && grade < 53.5) then
gpa = 1.0;
else if(grade < 49.5) then
gpa = 0.0;
else
gpa = -1.0;
return gpa ; // return statement
}
What is the error?

p.s. if you are trying to return the changed value of gpa back to main then you need to declare it as a reference parameter, not as pass by value.
i am getting the following errors

In function `int main()':
`gpa' cannot be used as a function

In function `void gpa(double)':
`then' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
expected `;' before "gpa"

`elseif' undeclared (first use this function)
expected `;' before "then"
expected primary-expression before "else"
expected `;' before "else"
expected primary-expression before "else"
expected `;' before "else"
expected primary-expression before "else"
expected `;' before "else"
expected primary-expression before "else"
expected `;' before "else"
expected primary-expression before "else"
expected `;' before "else"
expected `}' at end of input
gpa can be the name of a double, or the name of a function. Not both. I suggest you rename your function gpa to something like calculateGpa.

then is not in C++. Wherever you got that from, throw it away.

elseif is not C++. I suspect you meant else if


That rats' nest of if statements is a mess, with bad brackets all over the place, a missing = sign, a rogue { kicking around, just a real mess that needs organising properly so you can see the mistakes.

Your function gpa is a void function - it does not return anything. At the end you're trying to return a double. Pick one.

stdin lives in cstdio, which you have not included.

Don't use stdlib.h - this is C++, use cstdlib

Last edited on
thanks for your help
Last edited on
Topic archived. No new replies allowed.