string subscript out of range error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Gregory Wong,6910, Project 3, Math374, 01, Spring 2011

#include <iostream>
#include <string>
#include "account.h"
#include "CheckAccount.h"
#include "SavingAccount.h"
using namespace std;


int main()
{
	Check Check(1000.0);
	Saving Saving(2000.0);
	double credit=0.0;
	double debit=0.0;
	double interest = 0.1;
	double cost = 2.0;
	std::string command;
	std::string secCommand;

 std::cout<<"which account would you like to access?"<<"\n";
 std::cout<<"Checkings"<<"\n";
 std::cout<<"Savings"<<"\n";
	getline (cin,command);

if (command != " ")// checkings if command is blank or containing a string
{
	switch (command[0])
	{
	case'C'://if C is the first letter of the string then enter checkings
	case'c':
	  
		 while(secCommand !="Exit"||"exit") // while secCommand is not equal to exit loop
		 {
			 std::cout<<"Enter command as shown:"<<endl;
	 		 std::cout<<"Deposit"<<endl;
			 std::cout<<"Withdraw\t(There will be a fee deducted from your balance)"<<endl;
			 std::cout<<"Show Balance"<<endl;
			 std::cout<<"Exit"<<endl;
			 getline (cin,secCommand);

			 switch (secCommand[0])// switch statement to select correct command
			{
			case 'D':// if d is the first letter of the string then deposit 
			case 'd':
				std::cout<<"Enter amount that you will like to deposit:"<<endl;
				std::cin>>credit;
				Check.creditBalance(credit);
				Check.print();
				std::cout<<"\n";
				break;
			case 'W':// if w is the first letter of the string then withdraw
			case 'w': 
				std::cout<<"Enter amount that you will like to withdraw:"<<endl;
				std::cin>>debit;
				Check.setfee(cost);
				Check.debitBalance(debit);
				Check.print();
				std::cout<<"\n";
				break;
			case 'S':// if s is the first letter of the string then show balance 
			case 's':
				Check.print();
				std::cout<<"\n";
				break;
			default:
				secCommand = "Exit";
				break;
			}
		}
	  break;
	case'S'://if S is the first letter of the string then enter savings
	case's':

		while(secCommand!="Exit"||"exit") 
			 {
				 std::cout<<"Enter command as shown:"<<endl;
	 			 std::cout<<"Deposit"<<endl;
				 std::cout<<"Withdraw"<<endl;
				 std::cout<<"Calculate Interest"<<endl;
				 std::cout<<"Show Balance"<<endl;
				 std::cout<<"Exit"<<endl;
				 getline (cin,secCommand);

			if(secCommand!=" ")
			{
 				 switch (secCommand[0])
				{
				case 'D':
				case 'd':
					std::cout<<"Enter amount that you will like to deposit:"<<endl;
					std::cin>>credit;
					Saving.creditBalance(credit);
					Saving.print();
					std::cout<<"\n";
					break;
				case 'W':
				case 'w': 
					std::cout<<"Enter amount that you will like to withdraw:"<<endl;
					std::cin>>debit;
					Saving.debitBalance(debit);
					Saving.print();
					std::cout<<"\n";
					break;
				case 'C':
				case 'c': 
					std::cout<<"Your Balance including interest:"<<endl;
					Saving.modifyInterestRate(interest);
					Saving.calculateInterest();
					Saving.print();
					std::cout<<"\n";
					break;
				case 'S':
				case 's':
					Saving.print();
					std::cout<<"\n";
					break;
				default:
					secCommand = "Exit";
					break;
				}
			 }
			else 
				secCommand ="Exit";
		}
	
		break;
	}

	 

}
else 
	{
	return main();
	}

	system("pause");
 return 0;
}


i keep getting a error after runtime, right after i enter an amount to deposit/withdraw the error interrupts the runtime.

if there is any other errors please dont hesitate to mention it.

also how would i use a vector of account pointers to savings and checkings? (i do have savings and checkings class in seperate headers (entire program is 3 headers and 4 cpps) ) i want to implement polymorphism
We'd need to see the code for the "Check" class. When you IDE gives you this error, there is probably the option to debug the code and go to where the error is occurring. That would be helpful too. Also, this shouldn't compile since you have, on line 13/14, variables with the same name as a class.

If you want polymorphism, make the vector and vector of pointers to the base class type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include"CheckAccount.h"
#include<iostream>
using namespace std;

Check::Check(double balance) //checking class
 	: Account(balance)
	{
			if ( balance >= 0 )     //checking if balance is greater than 0                                    
		{
		setBalance(balance);
		}	
	else// if balance is less than 0, balance is set to 0
		{
		balance = 0;
		cerr << "Initial Savings Balance lower than $0.0"<< endl;
		}

	}

void  Check::creditBalance(double deposit)// credit balance function
{
	checkingbalance=Account::credit(deposit);
}
void  Check::debitBalance(double withdraw)// debit balance function
{
	if (Account::debit(withdraw) == true)// checking if debit is true 
	{
		addfee(fee);
		setBalance(balance);
		getBalance();
	}
	else  
		setBalance(balance);
		getBalance();
}
void Check::addfee(double fee)// add fee with debit balance function
{
		Account::debit(fee);
}
void Check::setfee(double cost) // set fee function
{
        fee = cost;
}
void Check::print()// print function 
{
	cout<<"Checking Balance: $"<<getBalance()<<endl;
}

double Check::setBalance(double balance)// set balance function
{
	return checkingbalance=balance;
}
double Check::getBalance()// return balance function
{
	return checkingbalance;
}

check class
is nearly the same as the savings class also just a slight difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include"account.h"
#include<iostream>
using namespace std;

Account::Account(double entryBalance)// account class
{
	 if ( entryBalance >= 0.0 )// set balance
        balance = entryBalance;
     else
		 {
			cout << "Initial balance is less than $0.0" << endl;
			balance = 0.0;
		 }
}

double Account::getBalance() const // return balance
{
	return balance;
}

double Account::credit(double deposit)// credit function 
{
	if (deposit >= 0)// checking if deposit is greater than 0
		return balance= balance+deposit;
	else 
		{
			cerr<<"Invalid amount to credit entered"<<endl;
			return balance;
		}
}

bool Account::debit( double withdraw )    // debit function                 
{
      if ( withdraw > balance )// checking if withdraw is greater than balance
		  {
				cout << "Withdraw amount exceeded account balance." << endl;
				return false;
		  }
      else 
		  {
				balance= balance - withdraw;                
				return true;
		  }
}

void Account::print()// print function 
{
	cout<<"Balance: $"<<getBalance();
}


it complied fine as it was when i added those switch cases in the error popped up
Topic archived. No new replies allowed.