473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problems with a string function in C++

I have written two classes : a String Class based on the book " C++ in
21 days " and a GenericIpClass listed below :

file GenericStringClass.h

// Generic String class

class String
{
public:

// constructors

String();
String(const char *const);
String(const String &);
~String();

// overloaded operators

char & operator[](unsigned short offset);
char operator[](unsigned short offset) const;
String operator+(const String&);
void operator+=(const String&);
String & operator= (const String &);

// General Accessors

unsigned short GetLen() const { return itsLen;}
const char * GetString() const { return itsString;}

//private members

private:
String(unsigned short);

// protected members for use in other classes

protected:
char * itsString;
unsigned short itsLen;
};
// default constructor creates a string of zero bytes

String::String()
{
itsString = new char[1];
itsString[0] = '\0';
itsLen=0;
}
// Creates a String object with predefined size "len"
// Null filled .

String::String(unsigned short len)
{
itsString = new char[len+1];
for ( unsigned short i=0; i<=len; i++)
{
itsString[i]='\0';
}
itsLen=len;
}

// Converts an array of characters into a String object

String::String(const char * const charArray)
{
itsLen=strlen(charArray);
itsString= new char[itsLen+1];
for (unsigned short i=0; i<itsLen; i++)
{
itsString[i]=charArray[i];
}
itsString[itsLen]='\0';
}
// copy constructor

String::String(const String & rhs)
{
itsLen=rhs.GetLen();
itsString = new char[itsLen+1];
for (unsigned short i=0; i<itsLen; i++)
{
itsString[i]=rhs[i];
}
itsString[itsLen]='\0';
}

// destructor frees allocated memory
String::~String()
{
delete [] itsString;
itsLen=0;
}

// operator equals

String& String::operator=(const String & rhs)
{
if (this == &rhs)
return *this;
delete [] itsString;
itsLen=rhs.GetLen();
itsString=new char[itsLen+1];
for (unsigned short i=0; i<itsLen; i++)
{
itsString[i]=rhs[i];
}
itsString[itsLen]='\0';
return *this ;
}
// non constant offset operator
// returns reference to a character so it can be changed
char & String::operator[](unsigned short offset)
{
if(offset > itsLen)
{
return itsString[itsLen-1];
}
else
{
return itsString[offset];
}
}
// constant offset operator
char String::operator[](unsigned short offset) const
{
if(offset > itsLen)
{
return itsString[itsLen-1];
}
else
{
return itsString[offset];
}
}

// Creates new string by adding current to rhs
String String::operator+(const String& rhs)
{
unsigned short totalLen = itsLen + rhs.GetLen();
String temp(totalLen);
for( unsigned short i=0; i<itsLen; i++)
{
temp[i]=itsString[i];
}
for (unsigned short j=0; j<rhs.GetLen(); j++,i++)
{
temp[i]=rhs[j];
printf("This is rhs ....%c \n",(char) rhs[j]);
}
temp[totalLen]='\0';
return temp;
}
// changes current String by adding another istance of the object clas
void String::operator+=(const String &rhs)
{
unsigned short rhsLen = rhs.GetLen();
unsigned short totalLen = itsLen + rhsLen;
String temp(totalLen);
for(unsigned short i=0; i<totalLen ; i++)
{
temp[i]=itsString[i];
}
for(unsigned short j=0; j<rhs.GetLen(); j++, i++)
{
temp[i]=rhs[i-itsLen];
}
temp[totalLen]='\0';
*this=temp;
}

file GenIpClass.cpp

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include "GenericStringClass.h"

// General definitions for the GenericIPClass

#ifndef __IP_STATUS_
enum __IP_STATUS_ {__ACTIVE,__INACTIVE};
#endif

#ifndef LOOPBACK
#define LOOPBACK "127.0.0.1"
#endif

#ifndef _HTTP_PREFIX
String _HTTP_PREFIX("http://");
#endif

#define DOT '.'
// GenericIpAddress Class mainly used for checking a given IP for
validity
// so you don't need to open a socket for nothing also used for
forming
// http address for requesting stats such
http://192.20.4.4/features.html
// with a given ip address of 192.20.4.4
class GenericIpAddress : public String
{
public:

// constructors ..

GenericIpAddress(); // for what reason is that used ??
GenericIpAddress(const char* const);
GenericIpAddress(String &);
~GenericIpAddress();

// Accessors

bool isIpValid();
String & GetIpAddress();

private:
String itsIpAddress;
unsigned short ipLength;

protected:
__IP_STATUS_ itsStatus;
};

// This is constructor exists only if
// you have an empty declaration by mistake
GenericIpAddress::GenericIpAddress()
{
// do nothing ...
}
GenericIpAddress::GenericIpAddress(const char * const GivenIp)
{
// Here we used the overloaded = constructor from the String
class
// GivenIp has to be a const char *

itsIpAddress=GivenIp;
}
GenericIpAddress::~GenericIpAddress()
{
// do nothing !!

}
String & GenericIpAddress::GetIpAddress(void)
{
return itsIpAddress;
}
bool GenericIpAddress::isIpValid(void)
{

String tempString=this->GetIpAddress();
cout << "DEBUG " << endl ;
cout << tempString.GetString() << endl;
cout << "DEBUG " << endl ;
unsigned short ip_length = tempString.GetLen();
unsigned short int m;
m=0;
int k[3];
int ip_part;
if((ip_length < 7 ) || (ip_length > 15))
{
cout << "Returning false ..... " << endl;
return false ;
}
for(unsigned short i=0; i<ip_length; i++)
{
if((tempString[i]>'0') && (tempString[i]<'9'))
{
cout << "inside first if statement " <<
tempString[i] << endl;
k[m]=(int)tempString[i];
m=m+1;
}
if((tempString[i]==DOT))
{
cout << "DEBUGGING .... entering dot if .....
" << endl;
cout << "DEBUGGING .... m is ..... "<< m <<
endl;
/* if((m==0) || (m>3))
{
return false;
} */ // this check goes to
default ??
switch(m)
{
case 1:
{
ip_part=k[0];
m=0;
}
case 2:
{
ip_part=10*k[1]+k[0];
m=0;
}
case 3:
{
ip_part=100*k[2]+10*k[1]+k[2];
m=0;
}
default:
{
cout << "DEBUGGING m=.. " << m
<< " end of DEBUGGING " ;
return false;
}
}
if((ip_part<1) || (ip_part>254))
{
return false;
}
}
else if((tempString[i]!=DOT) || (tempString[i]<'0') ||
(tempString[i]>'9'))
{
cout << "this is tempString ... " <<
tempString[i] << endl;
cout << " DEBUGGING entering the else if
sequence " ;
return false;
}
}
return true;
}

int main(void)
{
GenericIpAddress myIp("127.45.43.132");

if(myIp.isIpValid()==true)
cout << "IP IS ABSOLUTELY VALID "<< endl << endl ;

return 0;
}
The problem is in the isIpValid function . The object myIp has to
create a valid Ip but it doesn't . If you examine the output of the
program you will find out that the function enters both if statements
, not only the if((tempString[i]>'0') && (tempString[i]<'9')) but the
else if((tempString[i]!=DOT) || (tempString[i]<'0') ||
(tempString[i]>'9')) as well which is something strange as i believe
i check every condition . Can you help me ???

(Sorry for my english .... )


Jul 22 '05 #1
2 4749
Andrew escribió:
switch(m)
{
case 1:
{
ip_part=k[0];
m=0;
}
case 2:
{
ip_part=10*k[1]+k[0];
m=0;
}
case 3:
{
ip_part=100*k[2]+10*k[1]+k[2];
m=0;
}
default:
{
cout << "DEBUGGING m=.. " << m
<< " end of DEBUGGING " ;
return false;
}
}
You probably want a break at the end of each case.
else if((tempString[i]!=DOT) || (tempString[i]<'0') || > (tempString[i]>'9'))
You probaly mean:
else if((tempString[i]!=DOT) && ( (tempString[i]<'0') || > (tempString[i]>'9')) )


Regards.
Jul 22 '05 #2

"Julián Albo" <JU********@terra.es> skrev i meddelandet
news:3F***************@terra.es...
Andrew escribió:
switch(m)
{
case 1:
{
ip_part=k[0];
m=0;
}
case 2:
{
ip_part=10*k[1]+k[0];
m=0;
}
case 3:
{
ip_part=100*k[2]+10*k[1]+k[2];
m=0;
}
default:
{
cout << "DEBUGGING m=.. " << m
<< " end of DEBUGGING " ;
return false;
}
}
You probably want a break at the end of each case. else if((tempString[i]!=DOT) || (tempString[i]<'0') || > (tempString[i]>'9'))
You probaly mean:

else if((tempString[i]!=DOT) && ( (tempString[i]<'0') || > (tempString[i]>'9')) )
k[m]=(int)tempString[i];
This will return the ascii value of the digit. You should probarbly use:
k[m] = tempString[i] - '0';
case 2: ip_part=10*k[1]+k[0];
case 3: ip_part=100*k[2]+10*k[1]+k[2];


And add the digits in correct order, and in case 3 the correct digits:

case2: ip_part = 10*k[0]+k[1]
case 3: ip_part=100*k[0]+10*k[1]+k[2];
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: AH | last post by:
I have two functions in my shared library which are declared as follows: void setName(const std::string& str); std::vector<std::string> getInfo(); Since the code is compiled and in shared...
2
by: Jose Meireles | last post by:
Hi everyone I've created a module (in a web app) to hold several generic functions as subroutines. The problem I face is that I've got problems with the call of system functions with wich I...
9
by: Eva | last post by:
Hi, I wanted to know how i can enter values into a specific column of a listview. I have tried the following code but this seems to enter all my values into the first column!!! Can anyone...
9
by: mcbill20 | last post by:
Hello all. I just installed Oracle 10g developer tools on a machine running XP Pro and Office XP. Before this I had just the Oracle 9 client installed. I the previous configuration, I was able to...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
4
by: JC - home | last post by:
Hello.. I've been having some problems for a little while with this which I was sure I would beat...hmmm. Anyway, I have a form with two rich textboxes. One at the top which is to display a...
5
by: BeruthialsCat | last post by:
First go with trying to import xml to a database and whilst i have managed to do what i want i find that the xml files we have here at work are gonna cause me problems. I have a function that...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
1
by: Bob | last post by:
Hi, Hope you can help me with this one. I'm at my wits end. I'm trying to create an intelligent edit-box like the excellent "Customer" one at the URL: ...
8
webroten
by: webroten | last post by:
I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the "Attempted to read or write protected memory"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.