Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help needed quickly

I'm tryin to teach myself C++ and I'm stuck on a program. Here is the question:the progam will control two program defined functions: main and calcAverage. Before calling the calcAverage function to calculate the average, have the main function validate the input data. To be valid, the test score must be greater or equal to zero. if one or more of the scores are not valid the program should display an appropiate error message. I have 6 errors they are as follows (I don't know how to correct them)
line 33- average undeclared identifier
line 37- percision is not a member of basic ostream
line 44- ave undeclared identifier
line 54- calcAverage illegal use of type 'void'
line 54- calcaverage redefine: differnt type of modifier
line 8- :see declaration of calcAverage
line 54- unexpected token'{' follwing declaration of calcAverage

Thanks for any help received



#include
using namespace std;


//function prototype
void calcAverage(float, float&, float);

int main()
{
float num1 = 0.0;
float num2 = 0.0;
float num3 = 0.0;
float sum = 0.0;
float ave = 0.0;

//enter input data
cout << "Enter the first number 1: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;

//calculate the sum
sum = (num1 + num2 + num3);

//calculate the average
ave = (num1 + num2 + num3) / 3;

//validate input data
if (avgerage >= 0)
{
//valid data
cout << fixed;
cout.percision(0);
}
else //invalid data
cout << "Input error" << endl;
//end if

//display average
cout << "Average: " << avg << endl;


return 0;
} //end of main function



//*****program-defined functions*****
void calcAverage
{
//calculate average of sum and divide by three
sum = num1, 2, 3;
ave = sum / 3;
} //end of calcAverage

Comments

  • BenGreenBenGreen Member Posts: 7
    Please look at some of the comments I have made in your code.

    : #include
    : using namespace std;
    :
    :
    : //function prototype
    : void calcAverage(float, float&, float);
    :
    : int main()
    : {
    : float num1 = 0.0;
    : float num2 = 0.0;
    : float num3 = 0.0;
    : float sum = 0.0;
    : float ave = 0.0;
    :
    : //enter input data
    : cout << "Enter the first number 1: ";
    : cin >> num1;
    : cout << "Enter the second number: ";
    : cin >> num2;
    : cout << "Enter the third number: ";
    : cin >> num3;
    :
    : //calculate the sum
    : sum = (num1 + num2 + num3);
    :
    : //calculate the average
    : ave = (num1 + num2 + num3) / 3;
    :

    /* the identifier in the if statement 'avgerage' has not been declared */

    : //validate input data
    : if (avgerage >= 0)
    : {
    : //valid data
    : cout << fixed;

    /* I think that here you mean precision? */

    : cout.percision(0);
    : }
    : else //invalid data
    : cout << "Input error" << endl;
    : //end if
    :
    : //display average
    : cout << "Average: " << avg << endl;
    :
    :
    : return 0;
    : } //end of main function
    :
    :
    :
    : //*****program-defined functions*****

    /* you have not put arguments after 'calcAverage', should look more like this: calcAverage(float num1, float num2, float num3)
    */

    : void calcAverage
    : {
    : //calculate average of sum and divide by three

    /* this makes no sense, do you mean: sum = num1 + num2 + num3; ? */

    : sum = num1, 2, 3;

    /* if ave is supposed to be a global variable then declare it before main() */

    : ave = sum / 3;
    : } //end of calcAverage
    :
    :

  • magnus702magnus702 Member Posts: 3
    I debuged your code and it works now.
    You see I am not kind a person who puts only comments when you need help!
    So here it is.
    I hope this will help you.

    #include
    #include
    using namespace std;


    //function prototype
    float calcAverage(float num1, float num2, float num3);

    int main()
    {
    float num1 = 0.0;
    float num2 = 0.0;
    float num3 = 0.0;
    float ave = 0.0;

    //enter input data
    cout << "Enter the first number 1: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;
    cout << "Enter the third number: ";
    cin >> num3;


    //calculate the average
    ave = calcAverage(num1, num2, num3);

    /*
    //validate input data THIS PART YOU DON't NEED
    if (ave >= 0)
    {
    //valid data
    cout << fixed;
    cout.precision(0);
    }
    else //invalid data
    cout << "Input error" << endl;
    //end if
    */


    //display average
    cout << "Average: " << ave << endl;


    getch ();
    } //end of main function



    //*****program-defined functions*****
    float calcAverage(float num1, float num2, float num3)
    {
    //formula which sets precision:
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2); //here you can set precision (now is two decimals)

    //function will return this
    return (num1 + num2 + num3) / 3;

    }


  • WaltPWaltP Member Posts: 335
    : I debuged your code and it works now.
    : You see I am not kind a person who puts only comments when you need help!
    : So here it is.
    : I hope this will help you.

    No, you're the type of person that does the homework for others without telling them what they did wrong so they can learn.

    The idea behind being "a person who puts only comments" is so the one with the problem can learn how to fix their problem, think for themselves, and learn how to code. Simply fixing their code does little good. [italic]You[/italic] deserve the passing grade, not them.

    End of lecture....

    ----------------
    Walt


  • Justin BibJustin Bib USAMember Posts: 0

    ___ ( http://forcoder.org ) free video tutorials and ebooks about || C C++ Java Assembly Perl PL/SQL Visual Basic Swift Objective-C MATLAB Scratch Delphi Python PHP C# Ruby Visual Basic .NET Go JavaScript R Logo VBScript ABAP F# Erlang Awk Julia Dart Clojure Ada Kotlin Prolog Lisp D Scala Hack Apex Rust SAS LabVIEW Fortran Crystal ML Alice Scheme FoxPro COBOL Transact-SQL Lua Bash ||

Sign In or Register to comment.