473,242 Members | 1,497 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,242 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 4734
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.