Thread: Where am I going wrong?

  1. #16
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    But a lot of those books are not in C++ 20!
    Quote Originally Posted by Salem View Post

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So?

    You're struggling with fundamentals.

    Get a solid few months experience of basic C++ before you start worrying about the latest toys in C11/14/17/20/23.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by Salem View Post
    So?

    You're struggling with fundamentals.

    Get a solid few months experience of basic C++ before you start worrying about the latest toys in C11/14/17/20/23.
    But I have been doing cpp for two years! And all studies show all takes to become good at something takes 30 hours of time spent on it.

    Plus, I am confused with the order and why it isn't working when I believe I read it right...a simple problem I can do. Something more advanced? Probably struggle...

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But I have been doing cpp for two years!
    Based on how you couldn't figure out you needed member variables when the error messages told you as much, I doubt it.

    Looking at your post history, it looks like you dabble every couple of months and then forget everything.
    Where am I going wrong?-screenshot-2022-10-28-17-30-51-search-results-c-board-jpg
    And most of them are the same basic question.


    > And all studies show all takes to become good at something takes 30 hours of time spent on it.
    Try 10,000 hours.
    OK, 10,000 hours is bunk, but competence isn't achieved in a week.
    30 hours of C++ is enough to tell the difference between a C++ program and a hole in the ground.

    Like I said, your current book is crippling your ability to think.
    You're just mindlessly copying the text, but you don't know "why" things are the way they are, and you're at a complete loss when it doesn't work.

    Given a problem statement like this:
    "Create a class called Dog with attributes age, weight and color. Add suitable set/get methods".
    You should be able to produce that class without resorting to looking at pretty cartoon pictures.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Salem View Post
    30 hours of C++ is enough to tell the difference between a C++ program and a hole in the ground.
    30 hours should be long enough to learn how to write a basic class. But maybe OP has only one hour to spend on C++ each month? So they're only about 24 hours in to the 30 that are required to learn how to write a class.

  6. #21
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by Salem View Post
    Given a problem statement like this:
    "Create a class called Dog with attributes age, weight and color. Add suitable set/get methods".
    You should be able to produce that class without resorting to looking at pretty cartoon pictures.
    Bae please!

    Does code go something like this:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class Dog{
    
           int age;
           int weight;
           string color;
    
            Yeah, the other stuff seems hard.

  7. #22
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by christop View Post
    30 hours should be long enough to learn how to write a basic class. But maybe OP has only one hour to spend on C++ each month? So they're only about 24 hours in to the 30 that are required to learn how to write a class.
    Where did I go wrong with the first example? Member variables are age, etc, member functions are what?

  8. #23
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by solidusMGS View Post
    Where did I go wrong with the first example? Member variables are age, etc, member functions are what?
    Your first example (in post #1) didn't have member variables, and the member functions tried to access those non-existent member variables. This was pointed out in post #3.

  9. #24
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by christop View Post
    Your first example (in post #1) didn't have member variables, and the member functions tried to access those non-existent member variables. This was pointed out in post #3.
    But I followed the book letter-by-letter!

  10. #25
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by solidusMGS View Post
    But I followed the book letter-by-letter!
    But which step says to remove the member variables? Note that you were already asked about that in post #13.

    Here's what I get when I follow the steps letter-by-letter (I didn't implement main since some of the steps were cut off). This code compiles without warnings (even with the -Wall -Wextra -Wpedantic compiler flags):

    Code:
    // step 1
    #include <string>
    #include <iostream>
    using namespace std;
    
    // step 2
    class Dog
    {
    // step 3
    private:
            int age, weight;
            string color;
    // step 4
    public:
    // step 5
            void bark() { cout << "WOOF!" <<endl; }
    
    // step 6
            // void setAge(int yrs) { age = yrs; }
            // void setWeight(int lbs) { weight = lbs; }
            // void setColor(string hue) { color = hue; }
    // multiple step 2 (replace the three setter methods (above) with a single combined setter prototype)
            void setValues(int, int, string);
    
    // step 7
            int getAge() { return age; }
            int getWeight() { return weight; }
            string getColor() { return color; }
    };
    
    // multiple step 3
    void Dog::setValues(int age, int weight, string color)
    {
    // multiple step 4
            this->age = age;
            this->weight = weight;
            this->color = color;
    }

  11. #26
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    That's what I thought, but I read in the text, it said to leave the setters original and to just add the new setters. Maybe I'm just crazy and stupid.

    See you down at Arizona Bay!
    Quote Originally Posted by christop View Post
    But which step says to remove the member variables? Note that you were already asked about that in post #13.

    Here's what I get when I follow the steps letter-by-letter (I didn't implement main since some of the steps were cut off). This code compiles without warnings (even with the -Wall -Wextra -Wpedantic compiler flags):

    Code:
    // step 1
    #include <string>
    #include <iostream>
    using namespace std;
    
    // step 2
    class Dog
    {
    // step 3
    private:
            int age, weight;
            string color;
    // step 4
    public:
    // step 5
            void bark() { cout << "WOOF!" <<endl; }
    
    // step 6
            // void setAge(int yrs) { age = yrs; }
            // void setWeight(int lbs) { weight = lbs; }
            // void setColor(string hue) { color = hue; }
    // multiple step 2 (replace the three setter methods (above) with a single combined setter prototype)
            void setValues(int, int, string);
    
    // step 7
            int getAge() { return age; }
            int getWeight() { return weight; }
            string getColor() { return color; }
    };
    
    // multiple step 3
    void Dog::setValues(int age, int weight, string color)
    {
    // multiple step 4
            this->age = age;
            this->weight = weight;
            this->color = color;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread