1 Replies - 1590 Views - Last Post: 03 March 2009 - 10:35 PM Rate Topic: -----

#1 f20c1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-February 09

Run time check failure help?

Post icon  Posted 03 March 2009 - 07:20 PM

Hi everyone. I need some help. I've been working on this project for the last two weeks and its due tomorrow. I had it all finished and working then I added some comments and now i'm getting a Run time check faliure number 2 when I end the program with my do while loop by pressing 'n'.

The specific error says "Run-Time Check Faliure #2 stack around the variable 'amount' was corrupted." I tired checking over my code but i don't notice anything wrong. This didn't happen before I added my comments. Can anyone help me out? I know the code is kinda long but any help would be appriciated!

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

bool getAcctNo(int& value);
bool validAcctNumber(int acctNum, int size);
bool checkAccountUsed(int acctNum, double check[]);
bool saveAccountUsed(int acctNum, double save[]);
void newCheckAcct(int acctNum, double check[]);
void newSaveAcct(int acctNum, double save[]);
void depositCheck(int acctNum, double check[], double amount);
void depositSave(int acctNum, double save[], double amount);
bool withdrawCheck(int acctNum, double check[], double amount);
bool withdrawSave(int acctNum, double save[], double amount);
void balanceCheck(int value, double check[]);
void balanceSave(int value, double save[]);

int main() {

	char choice; //User input of which option they would like to perform
	char response; //User input of if they would like to continue running the program
	const int MAXACCT = 50; //Maximun number of checking and savings account
	int value; //Account Number the user wishes to use
	double amount; //The amount of money the user wishes to withdraw/deposite
	double check[MAXACCT];
	double save[MAXACCT};


	//A for loop to initilize all the checking and savings accounts to -1	
	for (int i = 0; i <= MAXACCT; i++){
		check[i] = -1;
		save[i] = -1;




	//A do while loop to execute the options the user can choose from
	//and contines to run the program until the user wishes to stop
	do{

	//Options the user can perform

cout << "Press c to create a new checking account" << endl;
cout << "Press s to create a new savings account" << endl;
cout << "Press D to make a deposite in a checking account" << endl;
cout << "Press d to make a deposite in a savings account" << endl;
cout << "Press W to make a withdrawal from a checking account" << endl;
cout << "Press w to make a withdrawal from a savings account" << endl;
cout << "Press B to see the blanace of a checking account" << endl;
cout << "Press b to see the balance of a savings account" << endl;
cin >> choice;
cin.ignore(1000,'\n');  // ignore rest of chars on line


switch(choice){

	//Case 'c' allows the user to create a new checking account
	//given that they provide a valid account number that isn't
	//already taken
	case 'c':
		
		cout << "Which account number do you wish to have?" << endl;
		
		//Runs getAccNo to ask the user to input the account they want to create
		//If they account number is invalid an error message is
		//displayed and the program asks the user if they would like to perform 
		//another task
		if (getAcctNo(value) == false)
			break;
		
		//If the account number the user iputs is valid checkAccountUsed is run
		//to check if the account number is alreay in use. If it is an error message
		//is displayed and the program asks the user if they would like to perform
		//another task.

		if(checkAccountUsed(value, check) == true){
			cout << "That account number is already being used" << endl;
			break;
		}
		
		//If the account number is valid and not in use then newCheckAcct is run
		//and the account is created with a 0 dollar balance.
		
		newCheckAcct(value, check);

	break;

	//Case 's' allows the user to create a new checking account
	//given that they provide a valid account number that isn't
	//already taken

	case 's': 
		cout << "Which account number do you wish to have?" << endl;

		//If the account number the user iputs is valid checkAccountUsed is run
		//to check if the account number is alreay in use. If it is an error message
		//is displayed and the program asks the user if they would like to perform
		//another task.

		if (getAcctNo(value) == false)
			break;

		//If the account number the user iputs is valid saveAccountUsed is run
		//to check if the account number is alreay in use. If it is an error message
		//is displayed and the program asks the user if they would like to perform
		//another task.

		if(saveAccountUsed(value, save) == true){
			cout << "That account number is already being used" << endl;
			break;
		}

		//If the account number is valid and not in use then newCheckAcct is run
		//and the account is created with a 0 dollar balance.

		newSaveAcct(value, save);
		
		break;

	//Case 'D' allows the user to deposite a positve amount of money into
	//a valid checking account.

	case 'D':
		
		//Runs getAcctNo which asks the user to input the account which 
		//they wish to deposite money into and checks if it is a valild 
		//account number. If it isn't, an error message is displayed.
		
		if (getAcctNo(value) == false)
			break;
		
		//If the accout number the user inputs is valid then checkAccountUsed 
		//is run to verify that the account the user wants to input money into
		//is acutally in use. If it isn't, then an error message is displayed.

		if (checkAccountUsed(value, check) == false){
			cout << "The checking account number you have chosen is not in use" << endl;
			break;
		}
		
		//Promts the user for the amount of money and set it to amount.

		cout << "Enter how much you want to deposite" << endl;
		cin >> amount; 
		
		//Checks to see if the amount inputed is positve. If not then an error 
		//message is displayed.
		
		if (amount < 0){
			cout << "You must deposite a positive amount" << endl;
			break;
		}

	//Updates the checking account with the amount deposited.

	depositCheck(value, check, amount);

	break;

	//Case 'd' allows the user to deposite a positve amount of money into
	//a valid savings account.

	case 'd':
	
		//Runs getAcctNo which asks the user to input the account which 
		//they wish to deposite money into and checks if it is a valild 
		//account number. If it isn't, an error message is displayed.
		
		if (getAcctNo(value) == false)
			break;

		//If the accout number the user inputs is valid then saveAccountUsed 
		//is run to verify that the account the user wants to input money into
		//is acutally in use. If it isn't, then an error message is displayed.

		if(saveAccountUsed(value, save) == false){
			cout << "The savings account number you have chosen is not in use" << endl;
			break;
		}

		//Promts the user for the amount of money and set it to amount.

		cout << "Enter how much you want to deposite" << endl;
		cin >> amount;

		//Checks to see if the amount inputed is positve. If not then an error 
		//message is displayed.

		if (amount < 0){
			cout << "You must deposite a positive amount" << endl;
			break;
		}
		
		//Updates the savings account with the amount deposited.

		depositSave(value, save, amount);

		break;

		
	//Case 'W' allows the user to withdraw a positve amount of money from
	//a valid checking account.
			
	case 'W':

		//Runs getAcctNo which asks the user to input the account which 
		//they wish to deposite money into and checks if it is a valild 
		//account number. If it isn't, an error message is displayed.

		if (getAcctNo(value) == false)
			break;

		//If the accout number the user inputs is valid then checkAccountUsed 
		//is run to verify that the account the user wants to withdraw money from
		//is acutally in use. If it isn't, then an error message is displayed.

		if (checkAccountUsed(value, check) == false){
			cout << "The checking account number you have chosen is not in use" << endl;
			break;
		}

		//Promts the user for the amount of money and set it to amount.

		cout << "Enter how much you want to withdraw" << endl;
		cin >> amount;

		//Checks to see if the amount inputed is positve. If not then an error 
		//message is displayed.

		if (amount < 0){
		cout << "You must withdraw a positive amount" << endl;
		break;
		}
		
		//Checks to see if there is enough money in the account to withdraw from.
		//If there is then the checking account is updated. If not then an error
		//message is displayed.

		withdrawCheck(value, check, amount);

		break;
	
	//Case 'w' allows the user to withdraw a positve amount of money from
	//a valid savings account.
			
	case 'w':

		//Runs getAcctNo which asks the user to input the account which 
		//they wish to deposite money into and checks if it is a valild 
		//account number. If it isn't, an error message is displayed.

		if (getAcctNo(value) == false)
			break;

		//If the accout number the user inputs is valid then saveAccountUsed 
		//is run to verify that the account the user wants to withdraw money from
		//is acutally in use. If it isn't, then an error message is displayed.

		if (saveAccountUsed(value, save) == false){
			cout << "The checking account number you have chosen is not in use" << endl;
			break;
		}

		//Promts the user for the amount of money and set it to amount.

		cout << "Enter how much you want to withdraw" << endl;
		cin >> amount;

		//Checks to see if the amount inputed is positve. If not then an error 
		//message is displayed.

		if (amount < 0){
			cout << "You must withdraw a positive amount" << endl;
			break;
		}

		//Checks to see if there is enough money in the account to withdraw from.
		//If there is then the savings account is updated. If not an error message 
		//is displayed.

		withdrawSave(value, save, amount);

		break;

	//Case 'B' allows the user to see the balance of a checking account. 

	case 'B':

		//Runs getAcctNo which asks the user to input the account which 
		//they wish to deposite money into and checks if it is a valild 
		//account number. If it isn't, an error message is displayed.

		if (getAcctNo(value) == false)
			break;

		
		//If the accout number the user inputs is valid then checkAccountUsed 
		//is run to verify that the account the user wants to withdraw money from
		//is acutally in use. If it isn't, then an error message is displayed.

		if (checkAccountUsed(value, check) == false){
		cout << "The checking account number you have chosen is not in use" << endl;
		break;
		}

		//Displays the balance of the checking account
		
		balanceCheck(value, check);
		
		break;

	//Case 'b' allows the user to see the balance of a checking account. 

	case 'b':

		//Runs getAcctNo which asks the user to input the account which 
		//they wish to deposite money into and checks if it is a valild 
		//account number. If it isn't, an error message is displayed.

		if (getAcctNo(value) == false)
			break;

		//If the accout number the user inputs is valid then saveAccountUsed 
		//is run to verify that the account the user wants to withdraw money from
		//is acutally in use. If it isn't, then an error message is displayed.

		if (saveAccountUsed(value, check) == false){
		cout << "The savings account number you have chosen is not in use" << endl;
		break;
		}

		//Displays the balance of the savings account
		
		balanceSave(value, check);
		
		break;
		
}

//Asks the user if they would like to continue to run the program. If not then n
//the program exits.

cout << "Would you like to make perform another action? (y/n)" << endl;
cin >> response;
	
	}while (response == 'y' || response == 'Y'); 

}


//Promts the user to enter an account and checks to see if the 
//number is valid(contains only digits).

bool getAcctNo(int& value){

	char inputStr[25]; //The array the account number string will be strored in
	int length; //The length of the account number
	const int size; //Size of the account number
	const int MAXACCT = 50 //Maximum number of checking or savings account

	size = 49;
	
	//Promts the user for an account number and stores it in a cstring
	
	cout << "Enter an account number" << endl;

	cin.getline(inputStr, 25);
	
	//calculates the length of the account number
	length = strlen(inputStr);


	//Runs a look to check that each element of the cstring is a digit.
	for(int i = 0; i < length; i++){
		if (isdigit(inputStr[i]) == false){
			cout << "Your Account Number can only contain digits" << endl;
			return false;
		}
	}

	//Converts the cstring to an interger value given that all the elements 
	//of the cstring were digits.

	value = atoi(inputStr);

	//Varifies that the account number isn't larger than 
	//the number of spots in the array.

	if (validAcctNumber(value, size) == false)
		return false;
	
	return true;
}

//This fucntion varifies that the account number entered isn't larger than 
//the checking or savings array, given that the account number is valid. 
//If the account number is larger than the array an error message is displayed.

bool validAcctNumber(int value, int size){

	if (value > size){
		cout << "The Account Number you have chosen is too large " << endl;
		cout << "Account number need to be between 0" << MAXACCT << endl;
		return false;
	}
	
	return true;
}

//This function checks if the checking account number entered is already in use. 
//If it is then true is returned. Otherwise false is returned.

bool checkAccountUsed(int value, double check[]){

	if (check[value] == -1)
			return false;

	return true;
}

//This function checks if the savings account number entered is already in use. 
//If it is then true is returned. Otherwise false is returned. 
bool saveAccountUsed(int value, double save[]){

	if (save[value] == -1)
		return false;

	return true;
}

//Creates a new checking account with a balance of 0 dollars for a given 
//account number.

void newCheckAcct(int value, double check[]){
	
	cout << "Account was sucessfully created" << endl;
	check[value] = 0;
}

//Creates a new savings account with a balance of 0 dollars for a given
//account number.

void newSaveAcct(int value, double save[]){
	cout << "Account was sucessfully created" << endl;
	save[value] = 0;
}

//Updates the balance of a checking account with the amount to be
//deposited.

void depositCheck(int value, double check[], double amount){

	check[value] = check[value] + amount;

	cout << "Your new balance is " << check[value] << endl;
}

//Updates the balance of a savings account with the amount to be
//deposited.

void depositSave(int value, double save[], double amount){

	save[value] = save[value] + amount;

	cout << "You new balance is " << save[value] << endl;
}

//Updates the balance of a checking account with the amount to be
//withdrawn if there is enough money in the account. If not then
//an error message is displayed.

bool withdrawCheck(int value, double check[], double amount) {
	
	if (check[value] < amount){
		cout << "You do not have enough funds to withdraw this amount" << endl;
		return false;
	}
	
	check[value] = check[value] - amount;
	cout << "Your new balance is " << check[value] << endl;
	return true;

}

//Updates the balance of a savings account with the amount to be
//withdrawn if there is enough money in the account. If not then
//an error message is displayed.

bool withdrawSave(int value, double save[], double amount){

	if (save[value] < amount){
		cout << "You do now have enough funds to withdraw this amount" << endl;
		return false;
	}

	save[value] = save[value] - amount;
	cout << "Your new balance is " << save[value] << endl;
	return true;
}

//Displays the current balance of a checking account.

void balanceCheck(int value, double check[]){

	cout << "The current balance of the checking account is" << endl;
	cout << check[value] << endl;
}

//Displays the current balance of a savings account.
void balanceSave(int value, double save[]){

	cout << "The current balance of the savings account is" << endl;
	cout << save[value] << endl;
}

This post has been edited by f20c1: 03 March 2009 - 07:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Run time check failure help?

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Run time check failure help?

Posted 03 March 2009 - 10:35 PM

You are missing a "}" at the bottom of the for() loop that initializes the arrays:
	//A for loop to initilize all the checking and savings accounts to -1	
	for (int i = 0; i <= MAXACCT; i++){
		check[i] = -1;
		save[i] = -1;
	}   <<<<<<<<<<<<<<<<<<<<<


Also for (int i = 0; i <= MAXACCT; i++) is the cause of the runtime error. Legal array indicies range from zero to the array length minus one. Your for() loop accesses one location beyond the array size. Change it to
for (int i = 0; i < MAXACCT; i++)

This post has been edited by n8wxs: 03 March 2009 - 10:36 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1