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;
}
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.