473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help resolving errors in C++ program

3
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "stdafx.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <cstdlib> // to use system("cls") used to "clear screen"
  6. #include <limits> // to declare numeric limits
  7. using namespace std;
  8.  
  9. // class constructor 
  10. Menu::Menu(void)
  11. {
  12. }
  13. // class destructor 
  14. Menu::~Menu(void)
  15. {
  16. }
  17. int Menu::getOption() // function to prompt user for interest rate input
  18. {    
  19.     std::string reply;
  20.     // User prompted to enter interest rate
  21.     // Repeat until user enters a valid response 
  22.     do{
  23.         system("cls");
  24.         cout << endl;
  25.         cout << endl << "Please choose a term and interest input option: ";
  26.         cout << endl << "\t\t1.) Enter your own term and interest values.";
  27.         cout << endl << "\t\t2.) Select from a list of term and interest options.";
  28.         cout << endl;
  29.         cout << endl << "Option selected: ";
  30.         cin >> reply;
  31.  
  32.         if(validate(reply, "0123456789") == true)
  33.         {
  34.             cstr = reply.c_str();
  35.             menuChoice = atoi(cstr);
  36.             repeat = false;
  37.  
  38.             if(menuChoice <= 0)
  39.             {
  40.                 repeat = true;
  41.                 callError();
  42.             }
  43.             else {
  44.                 if(menuChoice >= 3)
  45.                 {
  46.                     repeat = true;
  47.                     callError();
  48.                 }
  49.                 else {
  50.                     repeat = false;
  51.                     }
  52.             }
  53.         }
  54.     }while(repeat == true);
  55.     return menuChoice;
  56.  
  57.     if (menuChoice == 1)
  58.     {            
  59.         double getInterestRate() // function to prompt user for interest rate input
  60.         {
  61.             // initialize variables
  62.             double interestRate;
  63.             string reply;
  64.  
  65.             // User prompted to enter interest rate
  66.             cout << endl;
  67.             cout << endl << "Please enter the interest rate (%): ";
  68.             cin >> interestRate;
  69.             return interestRate;
  70.         }
  71.  
  72.         int getTerm() // function to prompt user for loan term input
  73.         {
  74.             // initialize variables
  75.             int term;
  76.             string reply;
  77.  
  78.             // User prompted to enter loan term
  79.             cout << endl;
  80.             cout << endl << "Please enter the loan term (years): ";
  81.             cin >> term;
  82.             return term;    
  83.         }
  84.     }
  85.     else{
  86.         if (menuChoice == 2)
  87.         {
  88.             do // do while loop to make menu selection 
  89.             {
  90.                 system("cls");
  91.                 cout << endl;
  92.                 cout << endl << "Please choose term and interest option: ";
  93.                 cout << endl << "\t\t1.) 7 years at 5.35%";
  94.                 cout << endl << "\t\t2.) 15 years at 5.50%";
  95.                 cout << endl << "\t\t3.) 30 years at 5.75%";
  96.                 cout << endl;
  97.                 cout << endl << "Option selected: ";
  98.                 getline(cin, reply);
  99.  
  100.  
  101.                 if(validate(reply, "0123456789") == true)
  102.                 {
  103.                     cstr = reply.c_str();
  104.                     option = atoi(cstr);
  105.                     repeat = false;
  106.  
  107.                     if(option <= 0)
  108.                     {
  109.                         repeat = true;
  110.                         callError();
  111.                     }
  112.                     else {
  113.                         if(option >= 4)
  114.                         {
  115.                             repeat = true;
  116.                             callError();
  117.                         }
  118.                         else {
  119.                             repeat = false;
  120.                         }
  121.                     }
  122.                 }
  123.             }while(repeat == true);
  124.             return option;
  125.         }
  126.     }
  127.     }
  128.  

Errors:
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\menu.cpp(31) : error C2660: 'Menu::validate' : function does not take 2 arguments
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\menu.cpp(59) : error C2601: 'getInterestRate' : local function definitions are illegal
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\menu.cpp(57): this line contains a '{' which has not yet been matched
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\menu.cpp(72) : error C2601: 'getTerm' : local function definitions are illegal
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\menu.cpp(57): this line contains a '{' which has not yet been matched
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\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:

Expand|Select|Wrap|Line Numbers
  1. class Menu
  2. {
  3. public:
  4.     // base constructor
  5.     Menu(void);
  6.  
  7.     // base destructor
  8.     ~Menu(void);
  9.  
  10.     // base class methods
  11.     int option;
  12.     bool repeat;
  13.     int menuChoice;
  14.  
  15.     int getOption();
  16.     double getInterestRate();
  17.     int getTerm();
  18.     bool validate();
  19.     const char *cstr;
  20.     bool loop(void);
  21.     void callError();
  22. };
  23.  
Dec 3 '07 #1
4 2789
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::getInterestRate()
{

//do code here
}

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

Max
Dec 3 '07 #2
rhivka
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.
Dec 3 '07 #3
a switch statement and call the class function based on the user choice?

Max
Dec 4 '07 #4
rhivka
3
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 : )

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cstdlib> // to use system("cls") used to "clear screen"
  6. #include <limits> // to declare numeric limits
  7. using namespace std;
  8.  
  9. // class constructor 
  10. Menu::Menu(void)
  11. {
  12. }
  13. // class destructor 
  14. Menu::~Menu(void)
  15. {
  16. }
  17. int Menu::getOption() // function to prompt user for interest rate input
  18. {    
  19.     std::string reply;
  20.     do // do while loop to make menu selection 
  21.             {
  22.                 system("cls");
  23.                 cout << endl;
  24.                 cout << endl << "Please choose term and interest option: ";
  25.                 cout << endl << "\t\t1.) 7 years at 5.35%";
  26.                 cout << endl << "\t\t2.) 15 years at 5.50%";
  27.                 cout << endl << "\t\t3.) 30 years at 5.75%";
  28.                 cout << endl << "\t\t4.) Custom term and interest";
  29.                 cout << endl;
  30.                 cout << endl << "Option selected: ";
  31.                 getline(cin, reply);
  32.  
  33.  
  34.                 if(validate(reply, "0123456789") == true)
  35.                 {
  36.                     cstr = reply.c_str();
  37.                     option = atoi(cstr);
  38.                     repeat = false;
  39.  
  40.                     if(option <= 0)
  41.                     {
  42.                         repeat = true;
  43.                         callError();
  44.                     }
  45.                     else {
  46.                         if(option >= 5)
  47.                         {
  48.                             repeat = true;
  49.                             callError();
  50.                         }
  51.                         else {
  52.                             repeat = false;
  53.                         }
  54.                     }
  55.                 }
  56.             }while(repeat == true);
  57.             return option;
  58. }
  59.  
And put this in the main file:

Expand|Select|Wrap|Line Numbers
  1. int getTerm();
  2. double getInterestRate();
  3.  
  4. int getInterestTerm() // function to prompt user for interest and term amounts
  5. {
  6.     int option = MyMenu.getOption();
  7.  
  8.     while (option == 4)
  9.         {
  10.             getInterestRate();
  11.             getTerm(); // function to prompt user for loan term input
  12.  
  13.         }
  14. }
  15.  
  16. double getInterestRate() // function to prompt user for interest rate input
  17.             {
  18.                 do{
  19.                     // initialize variables
  20.                     double interestRate;
  21.                     string reply;
  22.  
  23.                     // User prompted to enter interest rate
  24.                     cout << endl;
  25.                     cout << endl << "Please enter the interest rate (%): ";
  26.                     getline(cin, reply);
  27.  
  28.                     // Check if input is valid
  29.                     if(validate(reply, "0123456789") == true)
  30.                     {
  31.                         interestRate = atoi(reply.c_str());
  32.                         loop = false;
  33.                         if(interestRate <= 0)
  34.                         {
  35.                             loop = true;
  36.                             callError2();
  37.                         }
  38.                     }
  39.                     else
  40.                     {
  41.                         loop = true;
  42.                         callError2();
  43.                     }
  44.                 }while(loop == true);
  45.  
  46.                 return interestRate;
  47.             }
  48.  
  49. int getTerm() // function to prompt user for loan term input
  50.             {
  51.                 do{
  52.                     // initialize variables
  53.                     int term;
  54.                     string reply;
  55.  
  56.                     // User prompted to enter loan term
  57.                     cout << endl;
  58.                     cout << endl << "Please enter the loan term (years): ";
  59.                     getline(cin, reply);
  60.  
  61.                     // Check if input is valid
  62.                     if(validate(reply, "0123456789") == true)
  63.                     {
  64.                         term = atoi(reply.c_str());
  65.                         loop = false;
  66.                         if(term <= 0)
  67.                         {
  68.                             loop = true;
  69.                             callError2();
  70.                         }
  71.                     }
  72.                     else
  73.                     {
  74.                         loop = true;
  75.                         callError2();
  76.                     }
  77.                 }while(loop == true);
  78.  
  79.                 return term;    
  80.             }
  81.  
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\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(156) : error C2659: '=' : function as left operand
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(162) : error C2659: '=' : function as left operand
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(165) : warning C4805: '==' : unsafe mix of type 'bool (__cdecl *)(void)' and type 'bool' in operation
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(165) : error C2446: '==' : no conversion from 'int' to 'bool (__cdecl *)(void)'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(165) : error C2040: '==' : 'bool (__cdecl *)(void)' differs in levels of indirection from 'int'
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(167) : error C2065: 'interestRate' : undeclared identifier
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(186) : error C2659: '=' : function as left operand
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(189) : error C2659: '=' : function as left operand
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(195) : error C2659: '=' : function as left operand
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.cpp(198) : warning C4805: '==' : unsafe mix of type 'bool (__cdecl *)(void)' and type 'bool' in operation
c:\users\sunni\documents\visual studio 2005\projects\sunnimortgagecalculatorr3\sunnimortg agecalculatorr3\sunnimortgagecalculatorr3.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.
Dec 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
19
by: Thue Tuxen Sørensen | last post by:
Hi everybody ! I´m maintaining a large intranet (approx 10000 concurrent users) running on one IIS box and one DB box with sqlserver 2000. Currently there is 2,5 GB Ram, 1 1400 mhz cpu and 2...
1
by: Vineeth | last post by:
Hi, I am using xerces2.6.0 and am developing a program for converting an xml document to a text file. My program is extending the DefaultHandler. The first problem I am facing is that even...
3
by: JGBNS via DotNetMonster.com | last post by:
Hi, I am new to this forumand I apologize as i am not a .net programmer but we have a program being developed by a .net programmer. Nowwe have run into an ftp snag and I think it is part ftp and...
2
by: Kavitha Rao | last post by:
Hi, I am getting the following errors while trying to run this snippet in Microsoft Visual C++.Can't seem to print the crc value stored. /* +++Date last modified: 05-Jul-1997 */ /* Crc - 32...
5
by: fjlpf | last post by:
Hi I am a beginner of programming. These days, I try to develop a ftp program. But I find it is harder than I have thought. I want to post my code, then concentrate on resolving the...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.