Hey guys, its past midnite so I think the programming ghoulies have gotten to me but I am having a very strange problem with a constructor Account(char c[], char num[], int b); Everything runs fine until I reach the for loop below, which for a very strange and mind boggling reason does not execute and my program just crashes without any explanations and to make it run I need to include the printf("strlen(num): %d\n", j); statement.

Yes, I know I am using printf in C++ but thats what the prof wants so thats what the prof gets :)

int j = strlen(num) + 1;
/*
Copying to accountNumber
*/	
	//printf("Copying to accountNumber\n");
	printf("strlen(num): %d\n", j);
	for(int i = 0; i < j; i++)
	{
		accountNumber[i] = num[i];		
		//printf("accountNumber[%d] :: %c\n", i, num[i]);
	}
	accountNumber[15] = '\0';
/*
End copy to accoutNumber
*/

I have tried this on the Visual Studio compiler and the program crashes but with borland 5.0 it seems to work so if you have any insight into why this is happening please tell.

Recommended Answers

All 5 Replies

how is accountNumber declared?

delete line 12 because the previous loop copies the null terminator into the array.

accountNumber is declared in a header file for the Account class.

class Account{
	//Declaring class member variable
	char customer[251];			//251 including null byte
	char accountNumber[16];		//16 including null byte
	int balance;

You'd need to post the whole program, because the bug is somewhere else and this is just where it all went wrong.

Its a fairly long so I have attached it.

Using strcpy seems to fix it but I'm still curious as to why the for loop is causing trouble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.