I'm writing a program that uses functions that I create.It hasn't been much trouble until I got to a point where I am supposed to create a function that calculates the total of everything bought with sales tax. the problem is I can't get the total which has already been calculated to go to the next function. If you are kind of lost on what I'm saying just look at the code.

#include <iostream.h>

void get_total (float &total);
void calculate_total (float total);

int main ()
{
    float total;

    get_total (total);
    calculate_total (total);

return 0;
}

void get_total (float &total)
{
    char choice;
    const int T = 0;
    const int X = 0;

    do
    {
        cout << "S = sandwhich\n";
        cout << "C = chips\n";
        cout << "B = brownie\n";
        cout << "R = regular drink\n";
        cout << "L = large drink\n";
        cout << "X = cancel sale\n";
        cout << "T = total sale\n";
        cin >> choice;

        if (choice == 'T')
        {break;}

        switch (choice)
        {
        case 'S':
            total = total + 3.50;
            break;
        case 'C':
            total = total + .75;
            break;
        case 'B':
            total = total + .75;
            break;
        case 'R' :
            total = total + .50;
            break;
        case 'L':
            total = total + 1.00;
            break;
        case 'X':
            total = 0;
            break;
        default:
            cout << "illegal entry\n";
            break;
        }
        cout << "the total sale without tax is $" << total << endl;
    }
    while (choice != 'T'); 
}

void calculate_total (total)
{
    cout << total;
}

I am required to use a separate function to calculate the total with tax, right now i'm just trying to get the total to output in it and after I can do that I'll find the total with tax. I've been getting two error messages along with a bunch of warnings they are:

warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
there are 5 of them but I'm not to worried about them.
and:
error C2065: 'total' : undeclared identifier
error C2448: '<Unknown>' : function-style initializer appears to be a function definition

thanx to anyone who helps :)

Dani AI

Generated

Summary and fixes for later readers (from the original thread by ): the two immediate issues were (1) a function definition that omitted the parameter type, which makes the compiler see the name total as undeclared and confuses the parser, and (2) using total before it had a defined value. Both cause the errors shown and undefined behaviour at runtime.

Key points and practical fixes:

  • Make the function definition match its declaration exactly (include the parameter type). A missing type in the definition is the direct cause of the C2065/C2448 messages.
  • Initialize the accumulator before it’s used. Passing an uninitialized total by reference into the input routine lets garbage get added. Set it explicitly (for example, to zero) or change the input function to return the total instead of writing it by reference.
  • The C4244 warnings come from assigning double literals into floats. Either add the f suffix to literals (e.g. 0.75f) or use double consistently. For financial totals, double is usually a better choice than float.
  • Remove the odd const int T = 0; const int X = 0; declarations in the input routine — they serve no purpose and can confuse readers.

Style and robustness improvements:

  • Use the modern header (<iostream>) and qualify names with std:: or pull in only what’s needed.
  • Handle case-insensitive input with a character-to-upper helper (or std::toupper) so both s and S work.
  • Prefer returning a value from get_total for clarity (small types are cheap to return), or use const reference for read-only parameters; only use non-const references when the function must modify the caller’s variable.

Quick checklist to resolve the original errors:

  • Match prototype and definition types.
  • Initialize total before use.
  • Decide float vs double and adjust literals.
  • Remove useless constants and harden input handling.

(Notes: correctly pointed out the missing type and initialization; ’s reference suggestion is valid when modification is intended, but passing a small numeric by value or returning it is often simpler.)

Recommended Answers

All 4 Replies

See what's missing in the function definition?

void calculate_total ([b]float [/b]total);
 /*...*/
 void calculate_total (total)
 {
 	cout << total;
 }

At some point you'll probably want to initialize your total.

float total = 0;

A float constant would be 3.50F, whereas a double constant would be 3.50.

void calculate_total (total)
{
cout << total;
}

has to be

void calculate_total (float &total)
{
cout << total;
}

about the warnings:
your compiler interprets literals like 0.75 as double.
K.

void calculate_total (total)
  {
  cout << total;
  }

has to be

void calculate_total (float &total)
  {
  cout << total;
  }

Bzzzzzzzt!

Bzzzzzzzt!

guess there is an "&" too many :sad:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.