Help resolving errors in C++ program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhivka
    New Member
    • Dec 2007
    • 3

    Help resolving errors in C++ program

    I'm going to try to keep this question within the student posting guidelines. I've been working on a class file, and I'm not sure I've constructed it correctly. I'm getting several errors concerning the function definitions and matching { with }. I've checked my brackets several times and it looks right to me. I've made changes, changed back, and I'm at a loss. I'm using Visual C++ Express 2005 on Windows Vista OS. It may just be something simple I'm overlooking.... and I'll feel stupid in the end lol. But I've been working on this for days and it's becoming a big blur. Any help would be appreciated.

    Code:
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <cstdlib> // to use system("cls") used to "clear screen"
    #include <limits> // to declare numeric limits
    using namespace std;
    
    // class constructor 
    Menu::Menu(void)
    {
    }
    // class destructor 
    Menu::~Menu(void)
    {
    }
    int Menu::getOption() // function to prompt user for interest rate input
    {	
    	std::string reply;
    	// User prompted to enter interest rate
    	// Repeat until user enters a valid response 
    	do{
    		system("cls");
    		cout << endl;
    		cout << endl << "Please choose a term and interest input option: ";
    		cout << endl << "\t\t1.) Enter your own term and interest values.";
    		cout << endl << "\t\t2.) Select from a list of term and interest options.";
    		cout << endl;
    		cout << endl << "Option selected: ";
    		cin >> reply;
    
    		if(validate(reply, "0123456789") == true)
    		{
    			cstr = reply.c_str();
    			menuChoice = atoi(cstr);
    			repeat = false;
    
    			if(menuChoice <= 0)
    			{
    				repeat = true;
    				callError();
    			}
    			else {
    				if(menuChoice >= 3)
    				{
    					repeat = true;
    					callError();
    				}
    				else {
    					repeat = false;
    					}
    			}
    		}
    	}while(repeat == true);
    	return menuChoice;
    
    	if (menuChoice == 1)
    	{			
    		double getInterestRate() // function to prompt user for interest rate input
    		{
    			// initialize variables
    			double interestRate;
    			string reply;
    
    			// User prompted to enter interest rate
    			cout << endl;
    			cout << endl << "Please enter the interest rate (%): ";
    			cin >> interestRate;
    			return interestRate;
    		}
    
    		int getTerm() // function to prompt user for loan term input
    		{
    			// initialize variables
    			int term;
    			string reply;
    
    			// User prompted to enter loan term
    			cout << endl;
    			cout << endl << "Please enter the loan term (years): ";
    			cin >> term;
    			return term;	
    		}
    	}
    	else{
    		if (menuChoice == 2)
    		{
    			do // do while loop to make menu selection 
    			{
    				system("cls");
    				cout << endl;
    				cout << endl << "Please choose term and interest option: ";
    				cout << endl << "\t\t1.) 7 years at 5.35%";
    				cout << endl << "\t\t2.) 15 years at 5.50%";
    				cout << endl << "\t\t3.) 30 years at 5.75%";
    				cout << endl;
    				cout << endl << "Option selected: ";
    				getline(cin, reply);
    			
    
    				if(validate(reply, "0123456789") == true)
    				{
    					cstr = reply.c_str();
    					option = atoi(cstr);
    					repeat = false;
    
    					if(option <= 0)
    					{
    						repeat = true;
    						callError();
    					}
    					else {
    						if(option >= 4)
    						{
    							repeat = true;
    							callError();
    						}
    						else {
    							repeat = false;
    						}
    					}
    				}
    			}while(repeat == true);
    			return option;
    		}
    	}
    	}

    Errors:
    c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\menu.cpp( 31) : error C2660: 'Menu::validate ' : function does not take 2 arguments
    c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\menu.cpp( 59) : error C2601: 'getInterestRat e' : local function definitions are illegal
    c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\menu.cpp( 57): this line contains a '{' which has not yet been matched
    c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\menu.cpp( 72) : error C2601: 'getTerm' : local function definitions are illegal
    c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\menu.cpp( 57): this line contains a '{' which has not yet been matched
    c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\menu.cpp( 100) : error C2660: 'Menu::validate ' : function does not take 2 arguments

    The following is the header file, in case that's where I've made the mistake:

    Code:
    class Menu
    {
    public:
    	// base constructor
    	Menu(void);
    
    	// base destructor
    	~Menu(void);
    
    	// base class methods
    	int option;
    	bool repeat;
    	int menuChoice;
    	
    	int getOption();
    	double getInterestRate();
    	int getTerm();
    	bool validate();
    	const char *cstr;
    	bool loop(void);
    	void callError();
    };
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    Basically all of the functions defined in your header file need to be implemented inside the .cpp file outside of any other function. For example the function
    double getInterestRate () needs to be implemented like getOption()

    ie
    double Menu::getIntere stRate()
    {

    //do code here
    }

    what you're doing currently makes it look like you have never created a C++ class

    Max

    Comment

    • rhivka
      New Member
      • Dec 2007
      • 3

      #3
      We are learning classes. And I had it working with a class that only had the 3 term/interest options and returned option. Well we're also supposed to add a menu where the user can choose to enter their own values for interest and term or choose from the menu of 3 pre-defined values. I wasn't sure how to implement all that in the Menu class.

      Comment

      • mschenkelberg
        New Member
        • Jun 2007
        • 44

        #4
        a switch statement and call the class function based on the user choice?

        Max

        Comment

        • rhivka
          New Member
          • Dec 2007
          • 3

          #5
          I made a few changes trying to clean up the class, but not sure I did it right. Of course it's not compiling yet. This is what I changed and you can tell me how far off base I am : )

          Code:
          #include "stdafx.h"
          #include <string>
          #include <iostream>
          #include <iomanip>
          #include <cstdlib> // to use system("cls") used to "clear screen"
          #include <limits> // to declare numeric limits
          using namespace std;
          
          // class constructor 
          Menu::Menu(void)
          {
          }
          // class destructor 
          Menu::~Menu(void)
          {
          }
          int Menu::getOption() // function to prompt user for interest rate input
          {	
          	std::string reply;
          	do // do while loop to make menu selection 
          			{
          				system("cls");
          				cout << endl;
          				cout << endl << "Please choose term and interest option: ";
          				cout << endl << "\t\t1.) 7 years at 5.35%";
          				cout << endl << "\t\t2.) 15 years at 5.50%";
          				cout << endl << "\t\t3.) 30 years at 5.75%";
          				cout << endl << "\t\t4.) Custom term and interest";
          				cout << endl;
          				cout << endl << "Option selected: ";
          				getline(cin, reply);
          			
          
          				if(validate(reply, "0123456789") == true)
          				{
          					cstr = reply.c_str();
          					option = atoi(cstr);
          					repeat = false;
          
          					if(option <= 0)
          					{
          						repeat = true;
          						callError();
          					}
          					else {
          						if(option >= 5)
          						{
          							repeat = true;
          							callError();
          						}
          						else {
          							repeat = false;
          						}
          					}
          				}
          			}while(repeat == true);
          			return option;
          }
          And put this in the main file:

          Code:
          int getTerm();
          double getInterestRate();
          
          int getInterestTerm() // function to prompt user for interest and term amounts
          {
          	int option = MyMenu.getOption();
          
          	while (option == 4)
          		{
          			getInterestRate();
          			getTerm(); // function to prompt user for loan term input
          			
          		}
          }
          
          double getInterestRate() // function to prompt user for interest rate input
          			{
          				do{
          					// initialize variables
          					double interestRate;
          					string reply;
          
          					// User prompted to enter interest rate
          					cout << endl;
          					cout << endl << "Please enter the interest rate (%): ";
          					getline(cin, reply);
          
          					// Check if input is valid
          					if(validate(reply, "0123456789") == true)
          					{
          						interestRate = atoi(reply.c_str());
          						loop = false;
          						if(interestRate <= 0)
          						{
          							loop = true;
          							callError2();
          						}
          					}
          					else
          					{
          						loop = true;
          						callError2();
          					}
          				}while(loop == true);
          				
          				return interestRate;
          			}
          
          int getTerm() // function to prompt user for loan term input
          			{
          				do{
          					// initialize variables
          					int term;
          					string reply;
          
          					// User prompted to enter loan term
          					cout << endl;
          					cout << endl << "Please enter the loan term (years): ";
          					getline(cin, reply);
          			
          					// Check if input is valid
          					if(validate(reply, "0123456789") == true)
          					{
          						term = atoi(reply.c_str());
          						loop = false;
          						if(term <= 0)
          						{
          							loop = true;
          							callError2();
          						}
          					}
          					else
          					{
          						loop = true;
          						callError2();
          					}
          				}while(loop == true);
          				
          				return term;	
          			}
          These are functions within a function. Am I horrible off here? I could change back to where I was when I first posted and go with the switch statements if that's a better approach.

          Getting errors like:
          error C2659: '=' : function as left operand
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(156) : error C2659: '=' : function as left operand
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(162) : error C2659: '=' : function as left operand
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(165) : warning C4805: '==' : unsafe mix of type 'bool (__cdecl *)(void)' and type 'bool' in operation
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(165) : error C2446: '==' : no conversion from 'int' to 'bool (__cdecl *)(void)'
          Conversion from integral type to pointer type requires reinterpret_cas t, C-style cast or function-style cast
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(165) : error C2040: '==' : 'bool (__cdecl *)(void)' differs in levels of indirection from 'int'
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(167) : error C2065: 'interestRate' : undeclared identifier
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(186) : error C2659: '=' : function as left operand
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(189) : error C2659: '=' : function as left operand
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(195) : error C2659: '=' : function as left operand
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(198) : warning C4805: '==' : unsafe mix of type 'bool (__cdecl *)(void)' and type 'bool' in operation
          c:\users\sunni\ documents\visua l studio 2005\projects\s unnimortgagecal culatorr3\sunni mortgagecalcula torr3\sunnimort gagecalculatorr 3.cpp(198) : error C2446: '==' : no conversion from 'int' to 'bool (__cdecl *)(void)'

          which I don't completely understand because I've used the same code in different programs without error.
          Last edited by rhivka; Dec 4 '07, 05:39 AM. Reason: explain embedded functions

          Comment

          Working...