function wont work-VBForums
Results 1 to 6 of 6

Thread: function wont work

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    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
    }

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,013

    Re: function wont work

    In what way is it not working properly?
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Check out the work in progress: Vortex - C++ 3D Game engine for windows and linux - (Development blog - Leave a comment!)

  3. #3
    PowerPoster
    Join Date
    May 2002
    Posts
    24,066

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Re: function wont work

    Quote Originally Posted by techgnome View Post
    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 04:55 PM.

  6. #6
    PowerPoster
    Join Date
    May 2002
    Posts
    24,066

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.