Making an Account Number

Hi,

I was wondering is someone could help me with this problem. I am making a bank program and I need to assign each new customer an account number. The account number starts at 0011228x.

So first account number will be 00112281
Account 2 will be 00112282

and so on.

The problem I am having is if I make it an int, it does not accept the two 00 at start. If I make it a string it does not increment by 1. Any ideas how I can solve this?

Thanks

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
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

std::string new_account_number1()
{
    static int n = 112280 ;
    ++n ;

    std::ostringstream stm ;
    stm << std::setw(8) << std::setfill('0') << n ;
    return stm.str() ;
}

std::string new_account_number2()
{
    static int n = 112280 ;
    ++n ;

    std::string str = "00000000" + std::to_string(n) ;
    return str.substr( str.size() - 8 ) ;
}

int main()
{
    for( int i = 0 ; i < 5 ; ++i )
        std::cout << new_account_number1() << ' ' << new_account_number2() << '\n' ;
}

http://coliru.stacked-crooked.com/a/204fc258066b62cb
Cheers mate I will take a look at this code and try and work out what is happening as I have not encountered static before.

If I am having any problems I will post back.
Hi,

I am not able to get this to run.

Error message = E:\C++ Programs\Creating Account Number\Creates bank account number.cpp In function `std::string new_account_number2()':
Error message2 = 21 E:\C++ Programs\Creating Account Number\Creates bank account number.cpp `to_string' is not a member of `std'
> `to_string' is not a member of `std'

Requires C++11 (library).

C++98:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

std::string new_account_number()
{
    static int n = 112280 ;
    ++n ;

    std::ostringstream stm ;
    stm << std::setw(8) << std::setfill('0') << n ;
    return stm.str() ;
}

int main()
{
    for( int i = 0 ; i < 5 ; ++i )
        std::cout << new_account_number() << '\n' ;
}

http://coliru.stacked-crooked.com/a/9bf4fc55fd7084cb
I think that is a bit beyond me at the moment mate but really appreciate you trying to help.

However, you gave me another idea and here is my code:

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
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

class Customer
{
	public:
		Customer (); 
		void set_name ();
		string get_name () const;
		void set_account_number ();
		string get_account_number () const;
		
	private:
		static int number_of_accounts_created;
		string customer_fullname;
		string account_number;
		double balance;
		double deposit;
		double withdrawl;
		double interest;
};

Customer::Customer ()
{
    number_of_accounts_created++;               
}

void Customer::set_name()
{
	string first_name, last_name;
	cout << "Please enter new customer's first name: ";
	cin >> first_name;
	cout << "Please enter new customer's last name: ";
	cin >> last_name;
	customer_fullname = first_name + " " + last_name;	
}

string Customer::get_name() const
{
	return customer_fullname;
}

void Customer::set_account_number ()
{
	int last_six_digits = 112280 + number_of_accounts_created;
	ostringstream convert;
	convert << last_six_digits;
	string first_two_digits = "00";
	account_number = first_two_digits + convert.str();
}

string Customer::get_account_number () const
{
	return account_number;
}

const int MAX = 1000;
Customer new_customer [MAX];

int main ()
{
    new_customer [0].set_name();
	new_customer [0].set_account_number();
	system ("CLS");
	cout << "Customer's Name: " << new_customer [0].get_name() << endl;
	cout << "Account number is: " << new_customer [0].get_account_number() << endl;
	
    system ("PAUSE");
    return 0;
}


Basically what I have done is every time an object is created "number_of_accounts_created" is incremented by 1 and then I use the member function "set_account_number" to create the new account number.

However, the problem I now have is it won't compile and I get the following message:

" [Linker error] undefined reference to `Customer::number_of_accounts_created' "

Do you know how I can fix this?

edit: I added my destructor as well but that still getting same message

edit 2: I realised I had not initialised the variable so got it working now.
Last edited on
> [Linker error] undefined reference to
> `Customer::number_of_accounts_created'

Defining Customer::number_of_accounts_created will make that error go away.


> Basically what I have done is every time an object is created
>"number_of_accounts_created" is incremented by 1 and then I use the
> member function "set_account_number" to create the new account number.

What would happen if an anonymous temporary object is created and destroyed?
Topic archived. No new replies allowed.