help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; i have a program here and it wont work i think there is a problem in the array Code: #include<stdio.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    help

    i have a program here and it wont work i think there is a problem in the array
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<iostream>
    #include<string.h>
    using namespace std;
    class Dog
    {
          int age,weight;
          char color[11];
          public:
                 
                 void setValues(int, int, char[]);
                 int getAge(){return(age);}
                 int getWeight(){return(weight);}  
                char getcolor(){return(color[]);}  // problem here am i returning this array right
                void bark(){cout<< "woof!"<<endl;}    
    };
    void Dog::setValues(int age, int weight, char color[])
    {
         this -> age = age;
         this -> weight = weight;
         strcpy(this -> color,color);
    };
    main()
    {
          Dog fido, pooch;
          fido.setValues(3,15,"brown");
          
          pooch.setValues(4,18,"gray");
         cout<<"pooch is a" << pooch.getAge()<<endl;
         cout<<"year old " << pooch.getcolor()<<endl;
         cout<<"dog who weighs" << pooch.getWeight()<<endl;
         cout<<"pounds";
          
          pooch.bark();
          getch();
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    char getcolor(){return(color[]);}  // problem here am i returning this array right
    No, you are not returning an array, the return type is char, and if you did want to return an array you would use "return color;" not "return color[];"
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Help I failed my driving test can you tell me how to pass I don't know why I can't pass

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by whiteflags View Post
    Help I failed my driving test can you tell me how to pass I don't know why I can't pass
    You ran over your driving instructors dog, run the test again and you should pass, he doesn't have a dog any more
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Quote Originally Posted by ಠ_ಠ View Post
    Quote Originally Posted by whiteflags View Post
    Help I failed my driving test can you tell me how to pass I don't know why I can't pass
    You ran over your driving instructors dog, run the test again and you should pass, he doesn't have a dog any more
    vBulletin needs a "like" button.

    To the OP: Your code is wide open to buffer overflows. Consider using strncpy instead of strcpy in your Dog::setValues() method.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by PehJota View Post
    vBulletin needs a "like" button.

    To the OP: Your code is wide open to buffer overflows. Consider using strncpy instead of strcpy in your Dog::setValues() method.
    Or, rather, C++ strings!

    And mouse: After so many posts I'd expect you'd know at least how to make up a proper title...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21