-2

For some reason I am getting error C2448 from microsoft visual studios. I am trying to call a function but it is not wanting to work for me. Does anyone see what is wrong? I am trying to call 3 functions during the while statement with an if inside of it however it I thought that was how to call a function however it isn't working for me. Any input is appreciated.

#include <string>
#include <iostream>

using namespace std;

//int check (cbal && scharge);
//int deposit (cbal && scharge);
//int endt (cbal && scharge && loopend);

int main()
{
float cbal;
float scharge;
float bal;
char selection;
int loopend;
loopend = 1;
cout << "Transactions will take the form of a letter followed by a dollar amount. " <<
    "Valid letters are “C” for a check, “D” for a deposit, and “E” for the ending " <<
    "transaction(use zero on this transaction). Press <Enter> after each line of input!" << endl;
cout << "Please enter inital balance: ";
cin >> bal;
bal = cbal;
while(loopend == 1)
{
    cout << "Please enter a transaction" << endl;
    cin >> selection;
    if (selection == 'C' || selection == 'c')
    {
        int check (float cbal, float scharge);
    }
    else if (selection == 'D' || selection == 'd')
    {
        int deposit (float cbal, float scharge);
    }
    else if (selection == 'E' || selection == 'e')
    {
        int endt (float cbal,float scharge, int loopend);
    }
    else
    {
        cout << "Please enter a valid transaction.";
    }
}
return 0;
}

int check (float cbal, float scharge)
{
int transaction;
bool flag;
scharge = scharge + .15;
cout << "What is the check amount?" << endl;
cin >> transaction;
cbal = cbal - transaction;
cout << "Transaction ammount: " << transaction << endl
    << "Current Balance: " << cbal << endl
    << "Service Charge Check: $0.15" << endl;
if (cbal < 500 && flag == false)
{
    scharge = scharge + 5;
    flag = true;
    cout << "Service Charge Below $500: $5.00";
}
cout << "Total Service Charges: " << scharge;
return (cbal);
return (scharge);
}

int deposit(float cbal, float scharge)
{
int transaction;
scharge = scharge + .10;
cout << "What is the deposit amount?" << endl;
cin >> transaction;
cbal = cbal + transaction;
cout << "Deposit amount: " << transaction << endl
    << "Current Balance: " << cbal << endl
    << "Service Charge Deposit: $0.10" << endl
    << "Total Service Charges: " << scharge;
return (cbal);
return (scharge);
}

int endt (float cbal, float scharge, int loopend)
{
int transaction;
cout << "Enter transaction amount: ";
cin >> transaction;
if (transaction == 0)
{
    cout << "Transaction: End" << endl
        << "Current Balance: " << cbal << endl
        << "Total Service Charges: " << scharge << endl;
    cbal = cbal - scharge;
    cout << "Final Balance: " << cbal;
    loopend = 2;
}
else
    cout << "Error: 0 was not the transaction amount";
return (cbal);
return (scharge);
return (loopend);
}
Nickolas
  • 11
  • 3
  • 7
    What do you think `&&` does ? Do you have [a good C++ book](http://stackoverflow.com/q/388242/253056) that you can easily refer to ? – Paul R Dec 05 '16 at 19:07
  • && is the logical AND operator. Not sure what you are trying to do, but it certainly does not belong where you are using it. – OldProgrammer Dec 05 '16 at 19:10
  • 1
    As others are pointing out, `int endt (cbal && scharge && loopend)` doesn't make any sense. What do you think that means? – David Schwartz Dec 05 '16 at 19:10
  • i thought that it meant that those variables would be able to be used in the other function while keeping their data value – Nickolas Dec 05 '16 at 19:20
  • If you tell us what computer languages you do have experience with, maybe we can tell you what your mistake is. Right now I have no idea what made you think `int check (cbal && scharge)` is a good function declaration. – Mr Lister Dec 05 '16 at 19:26

2 Answers2

1

If you want do declare a function with two parameters, you should use a , instead of && you have written in your function declarations. In your case it should look like this:

int deposit(float cbal, float scharge)
{
     //some code
}

However, you have declared cbal and scharge as global variables, so your functions don't even have to take anything as a parameter, you can write only:

int deposit()
{
     //some code
}

In addition, your function do not return anything, do you really want to make them int, not void?

EDIT: I think I know what you want to do. If you declare your variables as local in main loop and then pass them to void function, there will be created copies of your variables and their values will change only in that function, in main loop they won't change. Then you might want to pass the references to them instead of those variables to make their value change also outside the void function. Then you have to define it as:

void deposit (float& cbal, float& scharge)
{
     //some code
}

But then if you call the function, you pass the parameters normally:

deposit(cbal, scharge);
Maras
  • 982
  • 9
  • 15
  • added the return on the functions and changed them to the way you had it but im still getting the errors – Nickolas Dec 05 '16 at 19:31
  • When you call a function, you shouldn't write variable's types again. Delete "float" and try again. – Maras Dec 05 '16 at 19:45
  • Also you can't return two values from a function, it will only return the first one you've written. – Maras Dec 05 '16 at 19:48
  • If your function have to change value of more than one variable, use references to variables – Maras Dec 05 '16 at 19:50
  • so on the check function im trying to send the scharge and cbal to check from main and get them both back, same goes for deposit. in endt im trying to get scharge and cbal from main and return loopend scharge and cbal – Nickolas Dec 05 '16 at 20:21
0

So since you told me exactly what your functions should do I'm posting the correct form of your code:

#include <string>
#include <iostream>

using namespace std;

//at first you have to at least declare the functions, you can also define them here or at the end of the code (as I did)

void check(float&, float&);
void deposit(float&, float&);
void endt(float&, float&, int&);

int main()
{
    float cbal;
    float scharge;
    float bal;
    char selection;
    int loopend = 1; //You can declare a variable with a value
    cout << "Transactions will take the form of a letter followed by a dollar amount. " <<
        "Valid letters are “C” for a check, “D” for a deposit, and “E” for the ending " <<
        "transaction(use zero on this transaction). Press <Enter> after each line of input!" << endl;
    cout << "Please enter inital balance: ";
    cin >> bal;
    cbal = bal; //Probably you wanted to assign the bal value to cbal, not bal = cbal
    while (loopend == 1)
    {
        cout << "Please enter a transaction" << endl;
        cin >> selection;
        if (selection == 'C' || selection == 'c')
        {
            check(cbal, scharge);
        }
        else if (selection == 'D' || selection == 'd')
        {
            deposit(cbal, scharge);
        }
        else if (selection == 'E' || selection == 'e')
        {
            endt(cbal, scharge, loopend);
        }
        else
        {
            cout << "Please enter a valid transaction.";
        }
    }
    return 0;
}

void check(float& cbal, float& scharge)
{
    int transaction;
    bool flag = false; //probably you wanted that to be false at the beggining(?)
    scharge += .15F; //shorter form of scharge = scharge + .15;
    cout << "What is the check amount?" << endl;
    cin >> transaction;
    cbal -= transaction; //same shorter form
    cout << "Transaction ammount: " << transaction << endl
        << "Current Balance: " << cbal << endl
        << "Service Charge Check: $0.15" << endl;
    if (cbal < 500 && flag == false)
    {
        scharge = scharge + 5;
        flag = true;
        cout << "Service Charge Below $500: $5.00";
    }
    cout << "Total Service Charges: " << scharge;
}

void deposit(float& cbal, float& scharge)
{
    int transaction;
    scharge += .10F;
    cout << "What is the deposit amount?" << endl;
    cin >> transaction;
    cbal += transaction;
    cout << "Deposit amount: " << transaction << endl
        << "Current Balance: " << cbal << endl
        << "Service Charge Deposit: $0.10" << endl
        << "Total Service Charges: " << scharge;
}

void endt(float& cbal, float& scharge, int& loopend)
{
    int transaction;
    cout << "Enter transaction amount: ";
    cin >> transaction;
    if (transaction == 0)
    {
        cout << "Transaction: End" << endl
            << "Current Balance: " << cbal << endl
            << "Total Service Charges: " << scharge << endl;
        cbal -= scharge;
        cout << "Final Balance: " << cbal;
        loopend = 2;
    }
    else
        cout << "Error: 0 was not the transaction amount";
}

You can also use global variables:

#include <string>
#include <iostream>

using namespace std;

void check();
void deposit();
void endt();

float cbal, scharge, bal;
int loopend = 1;

int main()
{
    char selection;
    cout << "Transactions will take the form of a letter followed by a dollar amount. " <<
        "Valid letters are “C” for a check, “D” for a deposit, and “E” for the ending " <<
        "transaction(use zero on this transaction). Press <Enter> after each line of input!" << endl;
    cout << "Please enter inital balance: ";
    cin >> bal;
    cbal = bal; //Probably you wanted to assign the bal value to cbal, not bal = cbal
    while (loopend == 1)
    {
        cout << "Please enter a transaction" << endl;
        cin >> selection;
        if (selection == 'C' || selection == 'c')
        {
            check();
        }
        else if (selection == 'D' || selection == 'd')
        {
            deposit();
        }
        else if (selection == 'E' || selection == 'e')
        {
            endt();
        }
        else
        {
            cout << "Please enter a valid transaction.";
        }
    }
    return 0;
}

void check()
{
    int transaction;
    bool flag = false; //probably you wanted that to be false at the beggining(?)
    scharge += .15F; //shorter form of scharge = scharge + .15;
    cout << "What is the check amount?" << endl;
    cin >> transaction;
    cbal -= transaction; //same shorter form
    cout << "Transaction ammount: " << transaction << endl
        << "Current Balance: " << cbal << endl
        << "Service Charge Check: $0.15" << endl;
    if (cbal < 500 && flag == false)
    {
        scharge = scharge + 5;
        flag = true;
        cout << "Service Charge Below $500: $5.00";
    }
    cout << "Total Service Charges: " << scharge;
}

void deposit()
{
    int transaction;
    scharge += .10F;
    cout << "What is the deposit amount?" << endl;
    cin >> transaction;
    cbal += transaction;
    cout << "Deposit amount: " << transaction << endl
        << "Current Balance: " << cbal << endl
        << "Service Charge Deposit: $0.10" << endl
        << "Total Service Charges: " << scharge;
}

void endt()
{
    int transaction;
    cout << "Enter transaction amount: ";
    cin >> transaction;
    if (transaction == 0)
    {
        cout << "Transaction: End" << endl
            << "Current Balance: " << cbal << endl
            << "Total Service Charges: " << scharge << endl;
        cbal -= scharge;
        cout << "Final Balance: " << cbal;
        loopend = 2;
    }
    else
        cout << "Error: 0 was not the transaction amount";
}

I hope that solves your problem.

Maras
  • 982
  • 9
  • 15
  • it did however i only wanted the if statement to happen once if conditions were correct however i think i figured that out – Nickolas Dec 06 '16 at 04:43