Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 10-19-2006, 12:30 PM   #1 (permalink)
blackmars0
Completely friggin' lost.
 
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
blackmars0 is on a distinguished road
Send a message via MSN to blackmars0
Weird logic error... I need another set of eyes.

Can anyone see a logic error in the init (char [],char [], int) function?? This is in C++.

I've tried setting the null byte earlier in the string, and using diagnostic printf's, and nothing's worked. Using the early null byte, I still get the error at the bottom, and using the printf's, it prints two instances of the string, the second is one x shorter than the first.

I get compile warnings as follows
Code:
ld: 0711-224 WARNING: Duplicate symbol: .strcpyFPcPCc
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
I'll post everything.

Code:
#include <stdio.h>
#include <string.h>
#include "Account.h"


//initialize any blank account objects
void Account::init() {
	this->balance = 0;
	strcpy(this->accountNumber, "000000000000000");
	strcpy(this->customer, ",,,;");
}

//intialize objects with specified values
void Account::init(char c[], char num[], int b) {
	int i=0,q,p;
	q=strlen(c);
	//clean c
	//convert tab and newline to whitespace
	for (i=0;i<=q;i++) {
		if (c[i] == '\t' || c[i] == '\n') {
			c[i] = ' ';
		}
	}
	//remove leading whitespace
	while (c[0]==' ') {	
		for (i=0; i<=q; i++) {
			c[i] = c[i+1];
		}
	}
	//remove double whitespace
	i=0;
	while (c[i]!='\0') {
		q=i;
		q=strlen(c);
		if(c[i] == ' ' && c[i+1] == ' ' || c[i+1] == '\0') { 
			for (p=i;p<=q;p++) {
				c[p] = c[p+1];
			}
		i--;
		}	
		i++;		
	}
	
	//cleaning finished
	
	q=strlen(c);
	if (q > 250) {
		strncpy(this->customer,c,251);
		this->customer[250] = '\0';
		}
	
	
	else if (q <=250);
		strcpy(this->customer,c);
	
	this->balance = b;
	strcpy(this->accountNumber,num);
}


void Account::changeAccountNumber(char num[]) {
	strcpy(this->accountNumber, num);
}

void Account::changeBalance(int b) {
	this->balance = b;
}

void Account::getLastName(char st[]) {
	int n,t,i=0;
	
	while (this->customer[i] != ',') {
		i++;
	}
	st[i]='\0';
	i--;
	
	for (t=0;t<=i;t++) {
		st[t] = this->customer[t];
	}
}

void Account::getFirstName(char st[]) {
	int n,t,i,p,c=0;
	p=strlen(this->customer);
	for (i=0;i<=p;i++) {
		if (this->customer[i] == ',' && c == 1 && c != 2) {
			t = i;
			c++;
		}
		else if (this->customer[i] == ',' && c != 2) {
			p = i;
			c = 2;
		}
		else if (this->customer[i] == ',');
			c++;
		
	}
	t++;
	
	for (i=t;i<p;i++) {
		st[i] = this->customer[i];
	}
}

void Account::getCity(char st[]) {
	int n,t,i,p,c=0;
	p=strlen(this->customer);
	for (i=0;i<=p;i++) {

		if (this->customer[i] == ',' && c == 2) {
			t = i;
			c++;
		}
		else if (this->customer[i] == ',' && c == 3) {
			p = i;
			c++;
		}
		else if (this->customer[i] == ',');
			c++;
	}
	t++;
	
	for (i=t;i<p;i++) {
		st[i] = this->customer[i];
	}
}

void Account::getPhoneNumber(char st[]) {
	int n,t,i,p,c=0;
	p=strlen(this->customer);
	for (i=0;i<=p;i++) {

		if (this->customer[i] == ',' && c == 3) {
			t = i;
			c++;
		}
		else if (this->customer[i] == ';' ) {
			p = i;
			c++;
		}
		else if (this->customer[i] == ',');
			c++;
	}
	t++;	
		
	for (i=t;i<p;i++) {
		st[i] = this->customer[i];
	}
}

const char* Account::getCustomer() const {
	return this->customer;
}



const char* Account::getAccountNumber() const {
	return this->accountNumber;
}

int Account::getBalance() const {
	return this->balance;
}

Account::Account(char c[], char num[], int b) { init(c, num, b); }		
Account::Account() {init();}

This is the code that passes stuff in.
Code:
int main()
{
	Account one;
	if (test1(one))
	{
		char d[] = "   xxxxx  xxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx"
				   "xxxxxxxxxxxxxxxxxxxxxxxxx";
		char num[] = "aw0000000000000";
		Account two(d, num, 5347);
		Account three;
		three.init(d, num, 5347);
		if (initTest(two,three))
		{
			 printf("Failed init test\n"
					"fix your init function\n");
		}
		else
		{
			test2(two);
		}
	}
	return 0;
}
int initTest(Account& a, Account& b)
{
	int result = 1;
	if(!strcmp(a.getCustomer(), b.getCustomer()))
	{
		if (!strcmp(a.getAccountNumber(), b.getAccountNumber()))
		{
			if (a.getBalance() == b.getBalance())
			{
				result = 0;
			}
		}
	}
	return result;
}

int test1(Account& in)
{
	int result = 1;
	if(strcmp(in.getCustomer(), ",,,;"))
	{
		printf("Failed test one-a, your getCustomer returned %s\n"
				"it should have returned ,,,;\n"
				"fix your default constructor\n", in.getCustomer());
		result = 0;
	}
	else if (strcmp(in.getAccountNumber(),"000000000000000"))
	{
		printf("Failed test one-b, your getAccountNumber returned %s\n"
				"it should have returned 000000000000000\n"
				"fix your default constructor\n",in.getAccountNumber());
		result = 0;
	}
	else if (in.getBalance() != 0)
	{
		printf("Failed test one-d, your getBalance returned %d\n"
				"it should have returned 0\n"
				"fix your default constructor\n", in.getBalance());
		result = 0;
	}
	return result;
}

void test2(Account& in)
{
	char d[] = "xxxxx xxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx"
			   "xxxxxxxxxxxxxxxxxxxxxxxxx";
	char num[] = "aw0000000000000";
	if (strcmp(in.getCustomer(), d))
	{
		printf("Failed test two-a, your getCustomer returned %s\n"
				"it should have returned %s\n"
				"fix your Account(char c[], char num[], int b) constructor"
				, in.getCustomer(), d);
	}
	else if (strcmp(in.getAccountNumber(), num))
	{
		printf("Failed test two-b, your getAccountNumber returned %s\n"
				"it should have returned %s\n"
				"fix your Account(char c[], char num[], int b) constructor"
				, in.getAccountNumber(), num);
	}
	else if (in.getBalance() != 5347)
	{
		printf("Failed test two-d, your getBalance returned %d\n"
				"it should have returned 5347\n"
				"fix your Account(char c[], char num[], int b) constructor"
				, in.getBalance());
	}
	else
	{
		strcpy(d, "James,Harry,St.   Louis,416-555-5555;");
		Account j(d, num, 1234);
		strcpy(d, "James,Harry,St. Louis,416-555-5555;");
		if (strcmp(j.getCustomer(), d))
		{
		printf("Failed test two-e, your getCustomer returned %s\n"
				"it should have returned %s\n"
				"fix your Account(char c[], char num[], int b) constructor"
				, j.getCustomer(), d);
		}
		else
		{
			test3(in);
		}
	}
}

It fails at test 2-a. Here's the error:

Code:
Failed test two-a, your getCustomer returned xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxaw0000000000000
it should have returned xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
fix your Account(char c[], char num[], int b) constructor

Anyone?? I've been working on this way too long...
blackmars0 is offline   Reply With Quote
Old 10-20-2006, 08:03 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
I dont have the time to read through it all, but one thing stood out:
Code:
    q=strlen(c);
    //clean c
    //convert tab and newline to whitespace
    for (i=0;i<=q;i++) {
Shouldn't that be:
Code:
    q=strlen(c);
    //clean c
    //convert tab and newline to whitespace
    for (i=0;i< q;i++) {
Since strlen() returns the length of the string hence char tmp[] = "ab" has length 2, but at index tmp[2] you will have a NULL placed instead of 'b', and I suspect similar off by one errors might be hidden...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-20-2006, 01:07 PM   #3 (permalink)
blackmars0
Completely friggin' lost.
 
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
blackmars0 is on a distinguished road
Send a message via MSN to blackmars0
Hmm, I'll give that a look, thanks.
blackmars0 is offline   Reply With Quote
Old 10-23-2006, 07:44 AM   #4 (permalink)
blackmars0
Completely friggin' lost.
 
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
blackmars0 is on a distinguished road
Send a message via MSN to blackmars0
Quote:
Originally Posted by redhead View Post
Since strlen() returns the length of the string hence char tmp[] = "ab" has length 2, but at index tmp[2] you will have a NULL placed instead of 'b', and I suspect similar off by one errors might be hidden...
Actually, wouldn't tmp[2] = '\0'? because arrays start at 0, not 1. so tmp[0]='a', tmp[1]='b', and tmp[2]='\0', does it not??
blackmars0 is offline   Reply With Quote
Old 10-23-2006, 07:54 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
NULL is '\0', but that dosn't change the fact that a part like:
Code:
for (p=i;p<=q;p++) {
    c[p] = c[p+1];
}
Will attempt to copy past the NULL (or '\0') index of the array...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenGL.dll Mr.Anderson Platform/API C++ 3 08-13-2004 10:07 AM
what language is this? sde All Other Coding Languages 10 05-25-2004 03:57 PM


All times are GMT -8. The time now is 09:36 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting