开发者

I don't understand these compile errors

开发者 https://www.devze.com 2023-02-19 03:13 出处:网络
I can not compile, this program is giving me errors. I have already spend 3 days on it. C:\\unisa\\COS1511\\test.cpp

I can not compile, this program is giving me errors. I have already spend 3 days on it.

C:\unisa\COS1511\test.cpp

[Warning] In function `float calcAllowedPerChild(float)':

error C:\unisa\COS1511\test.cpp:26

invalid operands of types float ()(float) and const float to binary operator<

#include <iostream>
using namespace std;

const float maxPerUnit = 20000.00;
//minPerChild include a standard gift consisting of a bath towel and facecloft
const float minPerChild = 100.00;
const float maxPerChild = 180.00;

//Depending on the amount the child may also get one or more of the fallowing:
const float TOOTHBRUSH = 29.95;
const float HIGHLIGHTERS = 25.99;
const float CRAYONS = 17.95;
const float NOTEBOOK = 12.95;
const float PEN = 9.99;

//You must add the function calcAllowebPerchild () here

float calcAllowedPerChild ( float nrChildren)

{

float allowedPerChild = maxPerUnit / nrChildren;

if ( allowedPerChild > maxPerChild || calcAllowedPerChild < minPerChild)

    return maxPerChild;
    else
    return minPerChild;

} 

int main ()
{

    float amtGift; //Allowed amount per child
    float amtSpendPerChild = 开发者_如何转开发0.00; //actual amount spend per child
    float totalSpend = 0.00; //total spend for one orphanage
    float totalAll = 0.00; //total spend for all 4 orphanages
    int nrChildren;        //number of chldren per orphanage

    cout << "Enter the number of children: " << endl;
    cin >> nrChildren;
    amtGift = calcAllowedPerChild(nrChildren);
    cout.setf(ios::fixed);
    cout.precision(2);
    cout << endl << " Allowable amount per child    :R" << amtGift;
    cout << endl << endl;

    return 0;
}


You have a ; at the end of your calcAllowedPerChild function.

Functions should look like:

void foo() {
    ... code ...
}

or:

void foo()
{
    ... code ...
}

depending on your coding style.

Edit - you also need to correct your spelling (maxPerChald) and declare calcAllowedPerChild:

float calcAllowedPerChild = maxPerUnit / nrChildren;

as well as changing the name of either your calcAllowedPerChild function or your calcAllowedPerChild variable. You can't have them both have the same name.


float calcAllowedPerChild ( float nrChildren);
                                          // ^ : Error - reomove it.

Function protoypes end with a ; but not function definitions. Also take a look at the if statement condition -

if ( calcAllowedPerChild > maxPerChild || calcAllowedPerChild < minPerChild)

Since it is a homework, I will give you a hint upon the next error.

Hint : Every statement should end with a ;. And now check inside your calcAllowerPerChild function.

if ( allowedPerChild > maxPerChild || calcAllowedPerChild < minPerChild)
                                     //^^^^^^^^^^^^^^^^^ Error: Probably you
                                     // meant allowedPerChild. Change it.


There should not be a ; at the end of this function:

float calcAllowedPerChild ( float nrChildren);

Fix that and see what happens.


I recommend checking your semicolons - ensure that function declarations do not look like prototypes.


There are a number of problems with calcAllowedPerChild - it should be:

float calcAllowedPerChild (float nrChildren)
{    
    float allowedPerChild = maxPerUnit / nrChildren;

    if (allowedPerChild > maxPerChild || allowedPerChild < minPerChild)
        return maxPerChild;
    else
        return minPerChild;
}

You also have a typo here:

const float maxPerChald = 180.00;

it should be:

const float maxPerChild = 180.00;


This line:

float calcAllowedPerChild ( float nrChildren);

should not have the semicolon at the end. It should be:

float calcAllowedPerChild ( float nrChildren)


Um, yeah, you have an extra ';' in your function definition:

float calcAllowedPerChild ( float nrChildren);

Just get rid of the semicolon at the end of the line there. That's what the compiler was trying to tell you when it said you have an extra semicolon on that line.


Remove the semicolon ; at the end of float calcAllowedPerChild ( float nrChildren); Function definitions do not end with semicolons.


Is this typo in your code?

const float maxPerChald = 180.00;

Remove the semicolon after this line:

float calcAllowedPerChild ( float nrChildren);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号