C++ task

Discussion in 'Homework Help' started by xxxyyyba, Mar 21, 2015.

  1. xxxyyyba

    Thread Starter Member

    Aug 7, 2012
    249
    2
    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 (Text):
    1.  
    2. #include <iostream>
    3. #include <string.h>
    4.  
    5. using namespace std;
    6.  
    7. class Word{
    8. private:
    9. char *content;
    10. int length;
    11. public:
    12. Word();
    13. Word(char *);
    14. ~Word();
    15. void print_content(void);
    16. int check_character(char);
    17. friend int check_number(Word,Word);
    18. };
    19.  
    20.  
    21. Word::Word(){
    22. }
    23.  
    24. Word::Word(char *n){
    25. length=strlen(n);
    26. content=new char [length];
    27. for(int i=0;i<length;i++){
    28. content[I]=n[I];
    29. }
    30. }
    31.  
    32.  
    33. Word::~Word(){
    34. delete content;
    35. }
    36.  
    37.  
    38. void Word::print_content(void){
    39. for(int i=0;i<length;i++){
    40. cout<<""<<content[I];
    41. }
    42. }
    43.  
    44.  
    45. int Word::check_character(char a){
    46. int position=0;
    47. for(int i=0;i<length;i++){
    48. if(content[I]==a){
    49. position=i+1;
    50. }
    51. }
    52. if(position>0){
    53. return position;
    54. }
    55. else return 0;
    56. }
    57.  
    58.  
    59. int check_number(Word n,Word m){
    60. int counter_n=0;
    61. int counter_m=0;
    62. for(int i=1;i<n.length;i++){
    63. if((n.content[i-1]=='1')&&(n.content[I]=='0')){
    64. counter_n=counter_n+1;
    65. }
    66. }
    67. for(int i=1;i<m.length;i++){
    68. if((m.content[i-1]=='1')&&(m.content[I]=='0')){
    69. counter_m=counter_m+1;
    70. }
    71. }
    72. if(counter_n>counter_m){
    73. return counter_n;
    74. }
    75. else if(counter_n<counter_m){
    76. return counter_m;
    77. }
    78. else return 0;
    79. }
    80.  
    81.  
    82. int main()
    83. {
    84. char characters1[]="qwerty10",*p1,*p2;
    85. char characters2[]="10allab10outcirc10uits";
    86. p1=characters1;
    87. p2=characters2;
    88. Word first(p1);
    89. Word second(p2);
    90. cout<<""<<first.check_character('h')<<endl;
    91. cout<<""<<second.check_character('z')<<endl;
    92. //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
    93. //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.
    94. //What's reason for this strange behaviour?
    95. first.print_content();
    96. second.print_content();
    97. }
    98.  
    Mderators note : Please use code tags for pieces of code
     
    Last edited by a moderator: Mar 21, 2015
  2. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Formatting your code so that it is readable goes a long way in getting someone to look at it.

    Code (Text):
    1.  
    2. #include <iostream>
    3. #include <string.h>
    4.  
    5. using namespace std;
    6.  
    7. class Word
    8. {
    9.    private:
    10.       char *content;
    11.       int length;
    12.    public:
    13.       Word();
    14.       Word(char *);
    15.       ~Word();
    16.       void print_content(void);
    17.       int check_character(char);
    18.       friend int check_number(Word,Word);
    19. };
    20.  
    21.  
    22. Word::Word()
    23. {
    24. }
    25.  
    26. Word::Word(char *n)
    27. {
    28.    length=strlen(n);
    29.    content=new char [length];
    30.  
    31.    for(int i=0; i<length; i++)
    32.    {
    33.       content[i]=n[i];
    34.    }
    35. }
    36.  
    37.  
    38. Word::~Word()
    39. {
    40.    delete content;
    41. }
    42.  
    43.  
    44. void Word::print_content(void)
    45. {
    46.    for(int i=0; i<length; i++)
    47.    {
    48.       cout<<""<<content[i];
    49.    }
    50. }
    51.  
    52. int Word::check_character(char a)
    53. {
    54.    int position=0;
    55.  
    56.    for(int i=0; i<length; i++)
    57.    {
    58.       if(content[i] == a)
    59.       {
    60.          position = i+1;
    61.       }
    62.    }
    63.    if(position > 0)
    64.    {
    65.       return position;
    66.    }
    67.    else
    68.       return 0;
    69. }
    70.  
    71. int check_number(Word n, Word m)
    72. {
    73.    int counter_n=0;
    74.    int counter_m=0;
    75.  
    76.    for(int i=1; i<n.length; i++)
    77.    {
    78.       if( (n.content[i-1]=='1') && (n.content[i]=='0') )
    79.       {
    80.          counter_n = counter_n + 1;
    81.       }
    82.    }
    83.  
    84.    for(int i=1; i<m.length; i++)
    85.    {
    86.       if( (m.content[i-1]=='1') && (m.content[i]=='0') )
    87.       {
    88.          counter_m = counter_m + 1;
    89.       }
    90.    }
    91.  
    92.    if(counter_n > counter_m)
    93.    {
    94.       return counter_n;
    95.    }
    96.    else
    97.       if(counter_n < counter_m)
    98.       {
    99.          return counter_m;
    100.       }
    101.       else
    102.          return 0;
    103. }
    104.  
    105. int main()
    106. {
    107.    char characters1[]="qwerty10",*p1,*p2;
    108.    char characters2[]="10allab10outcirc10uits";
    109.    p1=characters1;
    110.    p2=characters2;
    111.  
    112.    Word first(p1);
    113.    Word second(p2);
    114.  
    115.    cout<<""<<first.check_character('h')<<endl;
    116.    cout<<""<<second.check_character('z')<<endl;
    117.  
    118.    //cout<<""<<check_number(first,second)<<endl;
    119.    // This function for some reason makes other functions to work incorrectly, if you call it here
    120.    // you will see that first.print_content() and second.print_content() don't give us correct result.
    121.    // Or if function firstly first.check_character('r') is first called, second.check_character('j') second called
    122.    // and then check_number(first,second), then two firsly called functions don't work.
    123.    // What's reason for this strange behaviour?
    124.  
    125.    first.print_content();
    126.    second.print_content();
    127. }
    128.  
    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 (Text):
    1.  
    2. int check_number(Word n, Word m)
    3. {
    4.    int counter_n=0;
    5.    int counter_m=0;
    6.  
    7.    // Count occurrences of '10' in n
    8.    for(int i=1; i<n.length; i++)
    9.       if( (n.content[i-1]=='1') && (n.content[i]=='0') )
    10.          counter_n++;
    11.  
    12.    // Count occurrences of '10' in m
    13.    for(int i=1; i<m.length; i++)
    14.       if( (m.content[i-1]=='1') && (m.content[i]=='0') )
    15.          counter_m++;
    16.  
    17.    // Return the larger counter (or zero if equal)
    18.    if(counter_n > counter_m)
    19.       return counter_n;
    20.    if(counter_n < counter_m)
    21.       return counter_m;
    22.    return 0;
    23. }
    24.  
    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.
     
    xxxyyyba likes this.
  3. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    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++?
     
    xxxyyyba likes this.
  4. xxxyyyba

    Thread Starter Member

    Aug 7, 2012
    249
    2
    Hi.
    I solved it. When class has pointers, copy constructor must be defined.
     
Loading...