C++ task

Thread Starter

xxxyyyba

Joined Aug 7, 2012
289
Hi!
Here is my task:

Write class Word which has:
*pointer on array of characters
*constructors and destructors
*function to read word
*function to check if character which is passed to it as argument occurs in word and return position of occurance
*function to check which of two words has more occurances of number 10 and to return that number of occurances

Here is my solution. I compiled it without errors but it doesn't work as it should. See comments

Code:
#include <iostream>
#include <string.h>

using namespace std;

class Word{
private:
char *content;
int length;
public:
Word();
Word(char *);
~Word();
void print_content(void);
int check_character(char);
friend int check_number(Word,Word);
};


Word::Word(){
}

Word::Word(char *n){
length=strlen(n);
content=new char [length];
for(int i=0;i<length;i++){
content[I]=n[I];
}
}


Word::~Word(){
delete content;
}


void Word::print_content(void){
for(int i=0;i<length;i++){
cout<<""<<content[I];
}
}


int Word::check_character(char a){
int position=0;
for(int i=0;i<length;i++){
if(content[I]==a){
position=i+1;
}
}
if(position>0){
return position;
}
else return 0;
}


int check_number(Word n,Word m){
int counter_n=0;
int counter_m=0;
for(int i=1;i<n.length;i++){
if((n.content[i-1]=='1')&&(n.content[I]=='0')){
counter_n=counter_n+1;
}
}
for(int i=1;i<m.length;i++){
if((m.content[i-1]=='1')&&(m.content[I]=='0')){
counter_m=counter_m+1;
}
}
if(counter_n>counter_m){
return counter_n;
}
else if(counter_n<counter_m){
return counter_m;
}
else return 0;
}


int main()
{
char characters1[]="qwerty10",*p1,*p2;
char characters2[]="10allab10outcirc10uits";
p1=characters1;
p2=characters2;
Word first(p1);
Word second(p2);
cout<<""<<first.check_character('h')<<endl;
cout<<""<<second.check_character('z')<<endl;
//cout<<""<<check_number(first,second)<<endl; //This function for some reason makes other functions to work incorrectly, if you call it here you will see that first.print_content() and
//second.print_content() don't give us correct result. Or if function firstly first.check_character('r') is first called, second.check_character('j') second called and then check_number(first,second), then two firsly called functions don't work.
//What's reason for this strange behaviour?
first.print_content();
second.print_content();
}
Mderators note : Please use code tags for pieces of code
 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
30,062
Formatting your code so that it is readable goes a long way in getting someone to look at it.

Code:
#include <iostream>
#include <string.h>

using namespace std;

class Word
{
   private:
      char *content;
      int length;
   public:
      Word();
      Word(char *);
      ~Word();
      void print_content(void);
      int check_character(char);
      friend int check_number(Word,Word);
};


Word::Word()
{
}

Word::Word(char *n)
{
   length=strlen(n);
   content=new char [length];

   for(int i=0; i<length; i++)
   {
      content[i]=n[i];
   }
}


Word::~Word()
{
   delete content;
}


void Word::print_content(void)
{
   for(int i=0; i<length; i++)
   {
      cout<<""<<content[i];
   }
}

int Word::check_character(char a)
{
   int position=0;

   for(int i=0; i<length; i++)
   {
      if(content[i] == a)
      {
         position = i+1;
      }
   }
   if(position > 0)
   {
      return position;
   }
   else
      return 0;
}

int check_number(Word n, Word m)
{
   int counter_n=0;
   int counter_m=0;

   for(int i=1; i<n.length; i++)
   {
      if( (n.content[i-1]=='1') && (n.content[i]=='0') )
      {
         counter_n = counter_n + 1;
      }
   }

   for(int i=1; i<m.length; i++)
   {
      if( (m.content[i-1]=='1') && (m.content[i]=='0') )
      {
         counter_m = counter_m + 1;
      }
   }

   if(counter_n > counter_m)
   {
      return counter_n;
   }
   else
      if(counter_n < counter_m)
      {
         return counter_m;
      }
      else
         return 0;
}

int main()
{
   char characters1[]="qwerty10",*p1,*p2;
   char characters2[]="10allab10outcirc10uits";
   p1=characters1;
   p2=characters2;

   Word first(p1);
   Word second(p2);

   cout<<""<<first.check_character('h')<<endl;
   cout<<""<<second.check_character('z')<<endl;

   //cout<<""<<check_number(first,second)<<endl;
   // This function for some reason makes other functions to work incorrectly, if you call it here
   // you will see that first.print_content() and second.print_content() don't give us correct result.
   // Or if function firstly first.check_character('r') is first called, second.check_character('j') second called
   // and then check_number(first,second), then two firsly called functions don't work.
   // What's reason for this strange behaviour?

   first.print_content();
   second.print_content();
}
Do you see how much more readable that is. I didn't do anything except changed the formatting (and corrected some 'I's to 'i's where the auto-correct feature had thought it knew what you wanted to type).

It would also help if you gave some sample input and output. I don't know what you mean by "then two firsly called functions don't work." Are you saying that the calls to check_character() don't work even though they come before the call to check_number()?

Please provide the code as it was run and what the output of that code was. Then provide the modified code snippet and what the output when that code was run.

I think you are not doing what was intended because you are hardcoding your functions for a very specific case. I imagine the intent was for you to write a generic function that leveraged the other functions.

The code you have could be written quite a bit cleaner without altering the logic (right or wrong) at all. For instance, your check_number() code could be written as:

Code:
int check_number(Word n, Word m)
{
   int counter_n=0;
   int counter_m=0;

   // Count occurrences of '10' in n
   for(int i=1; i<n.length; i++)
      if( (n.content[i-1]=='1') && (n.content[i]=='0') )
         counter_n++;

   // Count occurrences of '10' in m
   for(int i=1; i<m.length; i++)
      if( (m.content[i-1]=='1') && (m.content[i]=='0') )
         counter_m++;

   // Return the larger counter (or zero if equal)
   if(counter_n > counter_m)
      return counter_n;
   if(counter_n < counter_m)
      return counter_m;
   return 0;
}
Hopefully you also see how just a couple comments aid the readability of the code.

Now, I don't know C++, but I see that in one constructor you allocate memory for the array 'content' and in the other you don't. But in your destructor you delete 'content' regardless of whether memory was allocated for it or not. I'm guessing that if it's not allocated that 'content' would be an uninitialized pointer and deleting it would be bad.
 

WBahn

Joined Mar 31, 2012
30,062
I think your basic problem might be in your constructor.

Walk through the code by hand assuming that the string that was passed was "Hi" and see if you can spot the problem.

Hint: How are strings delimited in C/C++?
 
Top