0

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.

3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by kenji
0

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;
0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.