Testing a loop?

Hey guys, why doesn't this function work?

void CreditCard::DoCharge(/*string name, */double amount){

for( int i = 0; i < numwithdraws; i++){
last10withdraws[i+1] = last10withdraws[i];
}
last10withdraws[0] = amount;
numwithdraws++;
}

I declared numwithdraws and last10withdraws[10] array in a parent class.
and I'm trying to test this loop so that I can shift the value of the elements
to the right by 1 each time I go through this function. Because I need to display the last 10 withdraws later on in another function:

void CreditCard::disp(){

for(int c = 0; c < 9; c++){
cout << "you input: " << last10charges[c] << endl;
}
}



int main(){

CreditCard ccrd;
double amount;
//string nme;

for(int i = 0; i < 12 ; i++){
cout<< "Withdraws" << endl;
cin >> amount;
//cout << "Name" << endl;
//cin >> nme;
ccrd.DoCharge(/*nme,*/ amount);
ccrd.disp();
}

system("PAUSE");

}
so this is my main, testing the function, but the function DoCharge doesn't work x.x

I hope you guys could understand my question.
I'm thinking it's the numwithdraws variable since i did not initialize it. But I don't know how to initialize it so that it would only be = 0 once. I know it will always initialize numwithdraws = 0 each time i access the function.
closed account (3CXz8vqX)
Post the class? Use code tags please!
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
#pragma once
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;

class Account
{
private:
	string name;
	long taxID;
	double balance;
protected:
	double last10withdraws[10];
	double last10deposits[10];
	int numdeposits;
	int numwithdraws;
public:
	Account();
	Account(string sname, long staxID, double sbalance);
	void SetName(string sname);
	string GetName();
	void SetTaxID(long staxID);
	long GetTaxID();
	void Setbalance(double sbalance);
	double Getbalance();
	void MakeDeposit(double amount);
	string display(); 
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#include "Account.h"
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;

class CreditCard: public Account
{
private:
	long cardnumber;
	string last10charges[10];
public:
	CreditCard();
	CreditCard(string sname, long staxID, double sbalance);
	void DoCharge(/*string name, */double amount);
	void MakePayment(double amount);
	string display();
	void disp();
};

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
void CreditCard::DoCharge(/*string name, */double amount){

	for( int i = 0; i < numwithdraws; i++){
		last10withdraws[i+1] = last10withdraws[i];
	}
	last10withdraws[0] = amount;
	numwithdraws++;

}


void CreditCard::disp(){
	//stringstream ss;
	//int slot=0;
	for(int c = 0; c < 9; c++){
		cout << "you input: " << last10withdraws[c] /*<< last10withdraws[slot] */<< endl;
		//slot++;
	}
		/*while(c < 10 && slot < 10){
		ss << last10charges[c] << last10withdraws[slot] << endl;
		slot++;
		c++;
	}*/
		//return ss.str();

}
Last edited on
Topic archived. No new replies allowed.