Web Hosting Talk







View Full Version : More C++ help...function issues...


ThatScriptGuy
10-18-2006, 05:36 PM
I've been doing great in my programming class for awhile now, but finally I'm stuck and forced to come ask for help again :)

// lab12.cpp

#include <iostream>
#include <string>
#include "Checkbook.h"
#include <iomanip>
#include <fstream>

using namespace std;

void printhead();
void printmenu();
void processmenu(char menu);
void balance();
void invalidinput();

int main()
{
Checkbook acct; // Object

system("clear"); //Clear the screen on first menu to make it better looking
printhead(); //Function to print the menu and receive menu input
//Testing...
acct.Deposit(500);
cout << acct.CurrentBalance();
return 0;
}

void printhead()
{
cout << "****************************************" << endl;
cout << "* Personal Checkbook Balancing Program *" << endl;
cout << "****************************************" << endl << endl;
cout << "Please select a menu item by typing a character" << endl
<< "in parentheses and pressing the return key." << endl << endl;
printmenu();
}

void printmenu()
{
char menu;
cout << "--- MENU ---" << endl
<< "(B) Balance" << endl
<< "(D) Deposit" << endl
<< "(W) Withdrawal" << endl
<< "(S) Service Charge" << endl
<< "(Q) Quit" << endl << endl;
cout << "Your Choice ===> ";
cin >> menu;
processmenu(menu); //Here we call the function to process the user input
}

void processmenu(char menu)
{

if (menu == 'B' || menu == 'b')
balance();
else if (menu == 'D' || menu == 'd')
cout << "Deposit\n";
else if (menu == 'W' || menu == 'w')
cout << "Withdrawal\n";
else if (menu == 'S' || menu == 's')
cout << "Service Charge\n";
else if (menu == 'Q' || menu == 'q')
cout << "Quitting\n";
else //Invalid choice - Throw error and repeat menu
invalidinput();
}

void balance()
{
// cout << acct.CurrentBalance();
}

void invalidinput()
{
cout << endl;
cout << ""; //Change font color to red for more visibility
cout << "You have entered an invalid menu selection!\n\n";
cout << ""; //Change font color back to white
printmenu(); //Print the menu again with error still visible
}

In the balance function, if I uncomment the cout line, I can't compile with the following error:
error: request for member `CurrentBalance' in `acct', which is
of non-aggregate type `int ()(const char*) throw ()'

I have no idea what this means...Obviously it's because I'm calling that from within the function - Up towards the top, where I call it, it works perfectly fine..but when called from within the balance function, it just won't compile....What am I missing here???
Thanks,
Kevin

Renard Fin
10-18-2006, 06:49 PM
I don't know a lot about the C++ but could this be just because of variable scope ?

ThatScriptGuy
10-18-2006, 06:51 PM
That's exactly what I'm thinking, but I'm not sure how to go about fixing it...
From within the main function, it works fine...but when called from another function, it doesn't...and I'm stumped..

evilrabbi
10-18-2006, 07:15 PM
Your creating the instance of Checkbook in main() and tring to use it in balance(). The scope of acct is local to main() thus it can't be used outside of it.
The easiest way I can tell you to fix it is like this.

setup main like this

int main()
{
Checkbook acct; // Object
char menuHoldl // hold var for menu option -- new
system("clear"); //Clear the screen on first menu to make it better looking
printhead(); //Function to print the menu and receive menu input
processmenu(menuHold,acct); // pass menu option and instance of checkbook

//Testing...
acct.Deposit(500);
cout << acct.CurrentBalance();
return 0;
}
change the processmenu and blance function to accept Checkbook. then when you call balance pass that object to it. hope this helps

ThatScriptGuy
10-18-2006, 07:22 PM
I feel like an idiot - You're completely right.....

So now, I need to change the printmenu function to return a char for the menu choice and then call process menu with the choice and the acct object.......
Makes sense now..
Thanks for that

evilrabbi
10-18-2006, 07:23 PM
np everyone makes mistakes. It's always the small things that will get you when it comes to programming.. btw what year are you?

Renard Fin
10-18-2006, 07:54 PM
Would it not be better to pass it by ref ?

processmenu(menuHold,&acct); // pass menu option and instance of checkbook

(err not sure for the & char ... I know for a pointer declaration in C++ it is *var but no idea for reference :P )

ThatScriptGuy
10-18-2006, 08:02 PM
Not calling you wrong here Renard, but just for clarification (I really don't know..)
What would be the benefit of passing by reference in this situation? The acct is never used again in the whole main function, so I'm just passing it from function to function (There is a balance function, withdrawal, service charge, etc.... so I'm just calling it like withdrawal(acct) and withdrawal is defined as void withdrawal(Checkbook acct);....)

Also, evilrabbi - I am only a sophomore (technically still a freshman by hours) but I was able to clep out of programming I because the syntax is so similar to PHP....Before that I had never touched C++ so this year is hitting me kind of hard...
Kevin

evilrabbi
10-18-2006, 08:23 PM
Not calling you wrong here Renard, but just for clarification (I really don't know..)
What would be the benefit of passing by reference in this situation? The acct is never used again in the whole main function, so I'm just passing it from function to function (There is a balance function, withdrawal, service charge, etc.... so I'm just calling it like withdrawal(acct) and withdrawal is defined as void withdrawal(Checkbook acct);....)

Also, evilrabbi - I am only a sophomore (technically still a freshman by hours) but I was able to clep out of programming I because the syntax is so similar to PHP....Before that I had never touched C++ so this year is hitting me kind of hard...
Kevin

Congrads on being able to clep out of programming I :). btw Renard passing by reference would be a bad idea. The balance funtion's only purpose is to print the balance not modify the object. Only bad could come of passing it by reference.

Also I looked at your code again and if the only place your using balance() is in the proccessmenu function you really don't need balance. Your already passing the Checkbook object to it so why not just cut out balance() and use it there.

ThatScriptGuy
10-18-2006, 08:26 PM
Because the balance function does a lot more than just print out the balance to the screen. It also writes to multiple files (logs) and performs a few calculations...To me, it's just cleaner to have everything completely separated based on what exactly it does...
Kevin