-
Feb 9th, 2012, 05:01 PM
#1
Thread Starter
New Member
function wont work
I have this program but the function i created is not working properly, what am i doing wrong 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
}
-
Feb 9th, 2012, 05:05 PM
#2
Re: function wont work
In what way is it not working properly?
-
Feb 9th, 2012, 05:06 PM
#3
Re: function wont work
a couple of thoughts ... 1) what's the error you're getting? 2) initially I was thinking that you had a stray { at the end of this line:
else if(grade >= 59.5 && grade < 63.5){
then I realized that 3) the whole if then stuff isn't valid C code...
not to mention 4) this line isn't valid either ... gpa 2.5; s/b gpa = 2.5; ... oh gheezes... that whole gpa function is jsut rong from the get-go... you're assinging the return value to the function then telling it to return the function... you should be using a local variable to hold the value to return... then return THAT varaible... it's like you've mixed VB syntax with C syntax...
-tg
-
Feb 9th, 2012, 05:15 PM
#4
Thread Starter
New Member
Re: function wont work
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
-
Feb 9th, 2012, 05:20 PM
#5
Thread Starter
New Member
Re: function wont work
Originally Posted by techgnome
a couple of thoughts ... 1) what's the error you're getting? 2) initially I was thinking that you had a stray { at the end of this line:
else if(grade >= 59.5 && grade < 63.5){
then I realized that 3) the whole if then stuff isn't valid C code...
not to mention 4) this line isn't valid either ... gpa 2.5; s/b gpa = 2.5; ... oh gheezes... that whole gpa function is jsut rong from the get-go... you're assinging the return value to the function then telling it to return the function... you should be using a local variable to hold the value to return... then return THAT varaible... it's like you've mixed VB syntax with C syntax...
-tg
ok i fixed those and a few more typing mistakes inside the function but function still not working
#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(grade, gpa);
// 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 grade, double& gpa)
{
if(grade >= 89.5 && grade < 100)
{
gpa = 5.0;
}
elseif(grade >= 84.5 && grade < 88.5)
{
gpa = 4.5;
}
else if(grade) >= 79.5 && grade < 83.5)
{
gpa = 4.0;
}
else if(grade >= 74.5 && grade < 78.5)
{
gpa = 3.5;
}
else if(grade >= 69.5 && grade < 73.5)
{
gpa = 3.0;
}
else if(grade >= 64.5 && grade < 68.5)
{
gpa = 2.5;
}
else if(grade >= 59.5 && grade < 63.5)
{
gpa = 2.0;
}
else if(grade >= 54.5 && grade < 58.5)
{
gpa = 1.5;
}
else if(grade >= 49.5 && grade < 53.5)
{
gpa = 1.0;
}
else if(grade < 49.5)
{
gpa = 0.0;
}
else
{
gpa = -1.0;
}
}
Last edited by something_something3; Feb 9th, 2012 at 05:55 PM.
-
Feb 9th, 2012, 07:55 PM
#6
Re: function wont work
you can't do this:
void gpa(double, double&);
and this:
double gpa;
and this:
void gpa(double grade, double& gpa)
{
and this:
gpa = (anything)
and this:
return gpa
you're using gpa all over the place is it a function? is it a sub? is it a variable? plus your gpa method/function what ever is prototyped as void, which makes it a sub, but then you return from it...
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|