Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is now getting a stack overflow error why?

#include <iostream>
#include <string>
using namespace std;
class Employee {
	const static int MAX_HOURS = 100;
	string firstName;
	string lastName;
	int hours[MAX_HOURS];
	int numhours;
	int wadges;
	// First Name
	string getFirstName() { return firstName; }
	void setFirstName(string name) { firstName = name; }
	void retrieveFirstName() {
		string temp;
		cout << "First Name: ";
		cin >> temp;
	// Last Name
	string getLastName() { return lastName;	}
	void setLastName(string name) {	lastName = name; }
	void retrieveLastName() {
		string temp;
		cout << "Last Name: ";
		cin >> temp;
	// Num hours
	int getNumhours() { return numhours; }
	void setNumhours(int hours) {
		if (hours >= 0 && hours <= MAX_HOURS) {
			numhours = hours;
		} else { 
			numhours = 0;
	void retrieveNumhours() {
		cout << "How many hours? ";
		int temphours;
		cin >> temphours;
	// wadges
	int getwadges() {return wadges; }
	void setwadges(int twadges) {
	void retrievewadges() {
	int twadges;
	cout << "What is your hourly wadge? " ;
	cin >> twadges;

	void retrieve() {
	double calwadges() {
	double total = 0;
	for (int i = 0; i < getNumhours(); i ++) {
		total += hours[i];	
	return calwadges();

int main() {
	Employee name;
	cout << "Grosspay: " << name . calwadges() << endl;
	return 0;
Updated 18-Nov-10 14:42pm
Anthony Mushrow 18-Nov-10 20:45pm    
You should use the <pre> tag when posting large code segments as to maintain the formatting as well as to get some fancy colourising.

Because for some reason your calling the same function from within itself creating an infinite loop.

double calwadges() {
    double total = 0;
    for (int i = 0; i < getNumhours(); i ++) {
        total += hours[i];
    //Calling this function from within itself, which will then call itself
    //again and so on
    return calwadges();

The stack is basically a list of all the functions you've come through to get the current point in the code, so your stack might at some point be something like: main() -> myfunction() -> myotherfunction()
There is only a limited amount of space available to store this 'list' so when you have infinite recursion (a function calling itself forever) then you eventually run out of space to store that function call which is your stack overflow error.
This is a very simplified explanation so if your interested you should do a quick search on how the stack works and what its purpose is.
Share this answer
EDITH GINGRAS 18-Nov-10 20:47pm    
how do I fix this to run properly? should I take it out at the bottom in the last section or remove the return but if i remove the return I get needs a return how do I get around this?
qontary 18-Nov-10 21:14pm    
You're calculating the total but I don't see anywhere you return it. I think what you meant is this -> return total;
EDITH GINGRAS 18-Nov-10 21:34pm    
I tried ti chang this and got these errors and when I do it the other way and put the } in I get return error expecting statement so what is wrong here?
int calwadges() {
int total = twadges * numhours;
return total; //gives error s below

1>c:\users\edith\documents\visual studio 2010\projects\wk11\wk11\wk11.cpp(73): error C2059: syntax error : 'return'
1>c:\users\edith\documents\visual studio 2010\projects\wk11\wk11\wk11.cpp(73): error C2238: unexpected token(s) preceding ';'

but if worded
return calwadges(); without the } before return statement it will run but it gives wrong answer input 5hr'6 wadges should produce 30 but I get 681something if I put in the } I get error at return expecting a statement.
CPallini 19-Nov-10 5:13am    
The wrong answer isn't a surprise to me, since your setwadges method has empty body. BTW if the hourly rate is constant, your way to compute the total is a bit silly.
return calwadges() ---------> is the reason for this error.

Hope u wrote this by mistake.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900