473,703 Members | 3,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GenericStringCl ass.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+=(cons t 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(c harArray);
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.GetL en();
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::operato r=(const String & rhs)
{
if (this == &rhs)
return *this;
delete [] itsString;
itsLen=rhs.GetL en();
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::operato r[](unsigned short offset)
{
if(offset > itsLen)
{
return itsString[itsLen-1];
}
else
{
return itsString[offset];
}
}
// constant offset operator
char String::operato r[](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::operato r+(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::operato r+=(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 "GenericStringC lass.h"

// General definitions for the GenericIPClass

#ifndef __IP_STATUS_
enum __IP_STATUS_ {__ACTIVE,__INA CTIVE};
#endif

#ifndef LOOPBACK
#define LOOPBACK "127.0.0.1"
#endif

#ifndef _HTTP_PREFIX
String _HTTP_PREFIX("h ttp://");
#endif

#define DOT '.'
// GenericIpAddres s 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 GenericIpAddres s : public String
{
public:

// constructors ..

GenericIpAddres s(); // for what reason is that used ??
GenericIpAddres s(const char* const);
GenericIpAddres s(String &);
~GenericIpAddre ss();

// 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
GenericIpAddres s::GenericIpAdd ress()
{
// do nothing ...
}
GenericIpAddres s::GenericIpAdd ress(const char * const GivenIp)
{
// Here we used the overloaded = constructor from the String
class
// GivenIp has to be a const char *

itsIpAddress=Gi venIp;
}
GenericIpAddres s::~GenericIpAd dress()
{
// do nothing !!

}
String & GenericIpAddres s::GetIpAddress (void)
{
return itsIpAddress;
}
bool GenericIpAddres s::isIpValid(vo id)
{

String tempString=this->GetIpAddress() ;
cout << "DEBUG " << endl ;
cout << tempString.GetS tring() << endl;
cout << "DEBUG " << endl ;
unsigned short ip_length = tempString.GetL en();
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)tempStrin g[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)
{
GenericIpAddres s myIp("127.45.43 .132");

if(myIp.isIpVal id()==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 4779
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********@ter ra.es> skrev i meddelandet
news:3F******** *******@terra.e s...
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)tempStrin g[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
4418
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 library. Every thing is great if the customer of my shared library is using the same compiler version (and same STL version). I am wondering if the customer changes their compiler version (or STL version) which may have same interface
2
1468
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 don't have problems if creating the same function within the webform class. as an example here follows a soubroutine within a module Imports System.Web.UI
9
6119
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 please help me out on this?? hers my code so far.....
9
17026
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 access any of the Oracle tables on another machine but now I am having problems. Unfortunately, I don't remember the correct syntax for the ODBC connect string and I am hoping that is my whole problem. I am trying to connect to an Oracle 9...
9
2036
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 function I'm trying to write. I'm writing a function that takes a string, and returns an array of strings which are the result of splitting the input on whitespace and parentheses (but the parentheses should also be included in the array as...
4
1735
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 concatenated summary, and one at the botton for making a new entry. When items are added in from the bottom box, I do this via a class
5
2474
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 imports any aml file, at the moment its importing to a hardcoded (name only) table but i can manage to work out how to take the xml file name and use that for the table name. My problem is some of the xml files do not follow the structural...
16
2533
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
1
4951
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: http://munich.schwarz-interactive.de/autocomplete.aspx
8
4893
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" error. Here's my Structure declaration: <StructLayout(LayoutKind.Sequential, charset:=CharSet.Ansi)> _ Public Structure CONFIG_PARM <MarshalAs(UnmanagedType.LPStr)> Public address1 As String <MarshalAs(UnmanagedType.LPStr)> Public...
0
8749
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9002
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7853
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2057
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.