简体   繁体   中英

undefined reference to functions (c++)

I searched this issue before asking and I found that everyone was using .h files for their functions, whereas my teacher has taught us to have main and the functions in one file.

I just want to figure out why it is undefined. I understand there could be other issues lurking in this code, but I really can not figure out what I am doing wrong.

Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

void start();
void process();
void check();
void deposit();

const float dserv = 0.10;
const float cserv = 0.15;
const float fiveserv = 5.0;
const float oserv = 10.0;

int main()
{
float balance;
float amount;
bool fivehun;
bool endcheck;
float servcharge;
float servchargetotal;
char type;

start();
while(endcheck != true)
{process();}
cout<<"Current balance: $"<<balance<<endl;
cout<<"Total service charges: $"<<servchargetotal<<endl;
cout<<"Final balance: $"<<
system("pause");
return 0;
}

void start(float balance)
{
    balance = 0;
    cout<<fixed<<showpoint<<setprecision(2);
    cout<<"Transactions will take the form of a letter followed by a dollar ";
    cout<<"amount. Valid letters are “C” for a check, “D” for a deposit, and";
    cout<<"“E” for the ending transaction (use zero on this transaction).";
    cout<<"Press <Enter> after each line of input";
    cout<<"Enter the beginning balance:"<<endl;
    cin>>balance;
}

void process(float balance, float amount, char type, bool endcheck)
{
    cout<<"Enter a transaction:"<<endl;
    cin>>type>>amount;
    if (type = "C"||"c")
    {check();
    endcheck = false;}
    else if(type = "D"||"d")
    {deposit();
    endcheck = false;}
    else if(type= "E"||"e")
    {endcheck = true;}
}

void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
    balance = balance - amount;
    servcharge = cserv;
    if (balance<500.00)
    {fivehun = true;}
    else
    {fivehun = false;}

    cout<<"Transaction: Check in amount of $"<<amount<<endl;
    cout<<"Current balance: $"<<balance<<endl;
    cout<<"Service charge: Check - $"<<cserv<<endl;
    if (fivehun == true)
    {cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
     servcharge = (fiveserv+servcharge);}
    cout<<"Total service charges: $"<<servcharge<<endl;
    servchargetotal = servchargetotal + servcharge;
}

void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
    balance = balance + amount;
    servcharge = dserv;
    if (balance<500.00)
    {fivehun = true;}
    else
    {fivehun = false;}

    cout<<"Transaction: Deposit in amount of $"<<amount<<endl;
    cout<<"Current balance: $"<<balance<<endl;
    cout<<"Service charge: Check - $"<<cserv<<endl;
    if (fivehun == true)
    {cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
     servcharge = (fiveserv+cserv);}
    cout<<"Total service charges: $"<<servcharge<<endl;
    servchargetotal = servchargetotal + servcharge;
}

If there is anywhere I can elaborate, I will try to edit/comment whatever I can.

You declare a function void start(); . And you call it with start(); .

However you never provided the body for this function. The function you defined as void start(float balance) is a different function . In C++ there may be several functions with the same name but different parameter lists; these are different functions.

You have a similar problem with process() and so on.


Your functions are supposed to be passing and returning the variables that they use, but you didn't actually write functions to do that. The balance in void start(float balance) is a different variable to the float balance inside main() ; the changes you make inside this function will not affect float balance; in main .

To fix this particular function, it should be:

float start();

in both the prototype and the function definition; and you should have float balance; inside the function, and then end with return balance; . Then you call in main like so: balance = start();

Your course materials should cover passing values to functions and returning values from functions, you will need to consult them to fix your other functions.

Check your start function as said MM You should get something like this:

float start() {
    // ...
    float balance;
    cin >> balance;
    return balance;
}


int main() {
    float n;
    n = start();
    // ...
    return 0;
}

And check all functions step by step because you have a problem in next process function: in if statement must be comparison operator if (type == "C" || type == "c") instead assignment type = "C"||"c" .

You define the following functions:

void start();
void process();
void check();
void deposit();

You implement:

void start(float balance)
void process(float balance, float amount, char type, bool endcheck)
void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)

The definitions must match the implementations.

Alternately you can get rid of the definitions if you put the implementations ahead of their first use (in main ) in the file.

The function calls match the definitions, but when the compiler (the linker, actually) goes looking for a function implementation that looks like void start() it finds void start(float balance) and concludes these are not a close enough match.

Please use this code. This will help you.

#include <iostream>
#include <iomanip>

using namespace std;

void start(float balance);
void process(float balance, float amount, char type, bool endcheck);
void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal);
void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal);

const float dserv = 0.10;
const float cserv = 0.15;
const float fiveserv = 5.0;
const float oserv = 10.0;

int main()
{
float balance;
float amount;
bool fivehun;
bool endcheck;
float servcharge;
float servchargetotal;
char type;

start(balance);
while(endcheck != true)
{process(balance, amount, fivehun, servcharge, servchargetotal);}
cout<<"Current balance: $"<<balance<<endl;
cout<<"Total service charges: $"<<servchargetotal<<endl;
cout<<"Final balance: $"<<
system("pause");
return 0;
}

void start(float balance)
{
balance = 0;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Transactions will take the form of a letter followed by a dollar ";
cout<<"amount. Valid letters are “C” for a check, “D” for a deposit, and";
cout<<"“E” for the ending transaction (use zero on this transaction).";
cout<<"Press <Enter> after each line of input";
cout<<"Enter the beginning balance:"<<endl;
cin>>balance;
}

void process(float balance, float amount, char type, bool endcheck)
{
cout<<"Enter a transaction:"<<endl;
cin>>type>>amount;
if (type = "C"||"c")
{check(balance, amount, ...);
endcheck = false;}
else if(type = "D"||"d")
{deposit(balance, amount, ...);
endcheck = false;}
else if(type= "E"||"e")
{endcheck = true;}
}

void check(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
balance = balance - amount;
servcharge = cserv;
if (balance<500.00)
{fivehun = true;}
else
{fivehun = false;}

cout<<"Transaction: Check in amount of $"<<amount<<endl;
cout<<"Current balance: $"<<balance<<endl;
cout<<"Service charge: Check - $"<<cserv<<endl;
if (fivehun == true)
{cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
 servcharge = (fiveserv+servcharge);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}

void deposit(float balance, float amount, bool fivehun, float servcharge, float& servchargetotal)
{
balance = balance + amount;
servcharge = dserv;
if (balance<500.00)
{fivehun = true;}
else
{fivehun = false;}

cout<<"Transaction: Deposit in amount of $"<<amount<<endl;
cout<<"Current balance: $"<<balance<<endl;
cout<<"Service charge: Check - $"<<cserv<<endl;
if (fivehun == true)
{cout<<"Service charge: Below $500 - $"<<fiveserv<<endl;
 servcharge = (fiveserv+cserv);}
cout<<"Total service charges: $"<<servcharge<<endl;
servchargetotal = servchargetotal + servcharge;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM