Search code examples
c++non-member-functions

C++ free function


I have the following code

#include "stdafx.h"
#include <iostream> 
using namespace std;
#include "graderec.h"

int main( )
{
    GradeRecord studentAnn("45-2791", 14, 49);
    GradeRecord studentBob("67-5803",25, 50);

    int bobsUnits;
    int bobsGradePoints;
    int annsUnits = 4;
    int annsGradePoints = 16;

    cout << "Ann's Grade Information:" << endl;
    studentAnn.writeGradeInfo();

    cout << endl;

    cout << "Bob's Grade Information:" << endl;
    studentBob.writeGradeInfo();

    cout << endl;

    cout << "Enter Bob's units: ";
    cin >> bobsUnits;

    cout << "Enter Bob's grade points: ";
    cin >> bobsGradePoints;

    cout << endl;

    cout << "Bob's Grade Information:" << endl;
    studentBob.updateGradeInfo(bobsUnits, bobsGradePoints);
    studentBob.writeGradeInfo();

    cout << endl;

    cout << "Ann's Grade Information:" << endl;
    studentAnn.updateGradeInfo(annsUnits, annsGradePoints);
    studentAnn.writeGradeInfo();

    system("PAUSE");
    return 0;

}

void asterisks()
{ 
    cout << "************************************************************************" << endl;

} 

I need to use a free function to display about 60 asterisks where I have cout << endl. I followed the example that I was giving but can't get it to work.

The code below is the example that I was given on how a free function looks.

void companyBanner()
{
  cout << *************************** << endl;
  cout << **     Tech Guys LLC     ** << endl;
  cout << *************************** << endl; 
  cout << endl;
}

Updatea: Got it working, thanks for the help everyone. I rewrote the free function and added asterisks() above the main again and it worked. Must have been something in the free function that was causing it to not work.


Solution

  • You should call the function you defined otherwise it will never be executed.

    You should also place either a declaration or the whole definition of the function before you call it for the first time.

    String literals should be enclosed in double quotes ".