dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Help to clear confusion

  1. #1
    Join Date
    Mar 2019
    Posts
    6

    Help to clear confusion

    Hi there. New confused beginner here. I was hoping some one could open my eyes on these errors . i know the code is not complete but it was working until i added the Class and what i thought would be simple has proved i'm mistaken. Any insight would would be help full. PS. i'm not really up on the terminology but I am getting there, and i hope this is readable.
    Thanks for looking .




    Code:
    //main.cpp
    #include <iostream>
    #include <fstream>
    #include "Data Class2.hpp"
    using namespace std;
    
    
    //class Data;
    
    int main(int argc, const char * argv[])
    {
       
        Data now;
        // Get a Name
        std::cout << "Enter a name\n";
        std::string name;
        std::cin >> name;
        now.getName() = name;                    //no error here
    /std::cout<< name << "\n";
        
        //get the age
        std::cout << "Enter your age\n";
        int age;
        std::cin >> age;
        now.getAge() = age;                //Expression is not assignable
    /std::cout<< age << "\n";
        
        //get the weight
        std::cout << "Enter your weight\n";
        float weight ;
        std::cin >> weight;
        now.getWeight() = weight;        //Expression is not assignable    
        //std::cout<< weight << "\n";
        
            {
            
            //Write to file
                std::ofstream outf("/users/Sample.dat");
                if (! outf)
                {
                    std::cout << "Culd not open file"<< std::endl;
                    
                }
            outf << name << "\n";
            outf << age << "\n";
            outf << weight << "\n";
    }
    
        {
         std::ifstream inf("/users/Sample.dat");
        if (! inf)
        {
            std::cout << "Could not open file"<< std::endl;
            
        }
         while (inf)
            {
                // read stuff from the file into a string and print it
                std::string strInput;
                inf >> strInput;
                std::cout << strInput << std::endl;
               
            
             }
        
         }
        
        
        
    return 0;
        
    }
    Code:
    //Data Class2.hpp
    
    #ifndef Data_Class2_hpp
    #define Data_Class2_hpp
    
    #include <stdio.h>
    #include <iostream>
    #include <string>
    
    
    class Data
    {
    public:
        
        std::string getName();
        void setName(std::string x);
        int getAge();
        void setAge(int x);
        float getWeight();
        void setWeight(float x);
    private:
        std::string NAME;
        int AGE;
        float WEIGHT;
        
    };
    #endif /* Data_Class2_hpp */
    Code:
    //Data Class.cpp
    
    #include "Data Class2.hpp"
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    class Data;
    std::string NAME;
    int AGE;
    float WEIGHT;
    
    
        std::string getName()       {return NAME;}
        void Data::setName(string x) {NAME = x;}
        int Data::getAge()           {return AGE;}
        void Data::setAge(int x)     {AGE = x;}
        float Data::getWeight()      {return  WEIGHT;}
        void Data::setWeight(float x) {WEIGHT = x;}
    Last edited by 2kaud; March 10th, 2019 at 07:37 AM. Reason: Fixed code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,611

    Re: Help to clear confusion

    The issue is with your implementation of your data class. As the class is defined in data_class2.hpp, you just want the body definitions in data_class.cpp. Consider:

    Code:
    #include "Data_Class2.hpp"
    #include <string>
    using namespace std;
    
    string Data::getName() {return NAME;}
    This implements the class function getName(). The other class functions are implemented similarly. The class function name is preceded by the classname and the scope resolution ::


    Note that it's not good practice to use UPPERCASE for variable names. UPPERCASE names are usually used just for macro definitions. Variable names usually start with lowercase and have words seperated either by an initial capital or _.

    Also note that the class function definitions can also be implemented directly in data_classs2.hpp and you don't need a separate file for them.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

  3. #3
    Join Date
    Mar 2019
    Posts
    6

    Re: Help to clear confusion

    Thank you for your insight. i will give it a try.

  4. #4
    Join Date
    Mar 2019
    Posts
    6

    Re: Help to clear confusion

    I have moved the class into main.cpp. Unfortunately it does not help with my errors .

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,611

    Re: Help to clear confusion

    Post the code for your new main.cpp - and the errors.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,611

    Re: Help to clear confusion

    From main() in post #1

    Code:
    now.getName() = name;                    //no error here
    when setting data for a class instance, you need to use the relevant setter, not the getter. This should be:

    Code:
    now.setName(name);
    and the same for when setting the other data.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

  7. #7
    Join Date
    Mar 2019
    Posts
    6

    Re: Help to clear confusion

    The errors should be in bold if I did it right . I think the syntax is right but as I said I might be missing something.


    Code:
    #include <iostream>
    #include <fstream>
    //#include "Data Class2.hpp"
    using namespace std;
    
    
    class Data
    {
    public:
        
        std::string getName() {return NAME;}
        void setName(std::string x){NAME = x;}
        int getAge() {return AGE;}
        void setAge(int x) {AGE = x;}
        float getWeight() {return  WEIGHT;}
        void setWeight(float x) {WEIGHT = x;}
        
        
    private:
        std::string NAME;
        int AGE;
        float WEIGHT;
        
    };
    
    int main(int argc, const char * argv[])
    {
       
        Data now;
        // Get a Name
        std::cout << "Enter a name\n";
        std::string name;
        std::cin >> name;
        now.getName() = name;
       //std::cout<< name << "\n";
        
        //get the age
        std::cout << "Enter your age\n";
        int age;
        std::cin >> age;
        now.getAge() = age;                   Expression is not assignable
       //std::cout<< age << "\n";
        
        //get the weight
        std::cout << "Enter your weight\n";
        float weight ;
        std::cin >> weight;
        now.getWeight() = weight;               Expression is not assignable
      //std::cout<< weight << "\n";
        
            {
            
            //Write to file
                std::ofstream outf("/users/kirbyroy/Sample.dat");
                if (! outf)
                {
                    std::cout << "Culd not open file"<< std::endl;
                    
                }
            outf << name << "\n";
            outf << age << "\n";
            outf << weight << "\n";
            }
        
        {
         std::ifstream inf("/users/kirbyroy/Sample.dat");
        if (! inf)
        {
            std::cout << "Could not open file"<< std::endl;
            
        }
         while (inf)
            {
                // read stuff from the file into a string and print it
                std::string strInput;
                inf >> strInput;
                std::cout << strInput << std::endl;
               
            
             }
        
         }
        
    return 0;
        
    }
    Last edited by 2kaud; March 10th, 2019 at 11:31 AM.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,611

    Re: Help to clear confusion

    Yes - see my post #6 re use of .setxxx() to set data.

    Also rather than passing a string by value, it is better to pass by const reference.

    Code:
    void setName(std::string x){NAME = x;}
    becomes
    Code:
    void setName(const std::string& x){NAME = x;}
    You'll learn why later - but get into the habit now.
    Last edited by 2kaud; March 10th, 2019 at 11:35 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

  9. #9
    Join Date
    Mar 2019
    Posts
    6

    Re: Help to clear confusion

    thanks again.Ive been looking at that for 3 days now. I figured it was something simple. i think im the one thats simple.
    Thank you for your time and for setting me straight.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,611

    Re: Help to clear confusion

    Note for your file i/o. If name contains a space, then << will insert into the stream as expected. However if you try to input with >> then extraction will only read a string upto the space. The next >> will then extract from the space. Thus

    << "first second";

    will write first second

    but

    std::string nam;

    >> nam;

    will only extract first.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.9)

  11. #11
    Join Date
    Feb 2017
    Posts
    406

    Re: Help to clear confusion

    Quote Originally Posted by Dragwood View Post
    open my eyes on these errors .
    One common newbie mistake is to enter too much code in one chunk.

    It's much better to start with a minimal but working program. Essentially just "Hello world". Then add code in small working increments. In your case it could be just one variable with a getter and a setter. When it works for sure you continue with the next, make it work and then the next etcetera. In the end you have a working program because it has been working all the time from start to finish.

    As a newbie the increments should be very small, probably just a line of code at the time. As you get more experienced you can add more code in each go without risking a pile of non-working code you cannot figure out.

  12. #12
    Join Date
    Mar 2019
    Posts
    6

    Re: Help to clear confusion

    Thats kind of what Ive been doing but then when it does work I get a little big headed and then I think I can do this and that
    and your exactly right . Lot of confusion . Patience is not one of my strong suits.

  13. #13
    Join Date
    Feb 2017
    Posts
    406

    Re: Help to clear confusion

    Quote Originally Posted by Dragwood View Post
    Patience is not one of my strong suits.
    The method I mentioned actually has a name. It's called Stepwise Refinement and was invented/popularized by a guy called N. Wirth already in the early 1970's. It's essentially the mother of all program development methods.

    Stepwise Refinement has an additional aspect I didn't mention that may appeal to the impatient. . In essence you start by producing a mock-up of the whole program (in incremental steps). Then you fill in the details (also in incremental steps).

    In practice these steps aren't clinically separated, you do a little of both at the same time as you see fit (by say combining the object oriented and the functional programming paradigms) but the message is clear. You should have a demonstrable mock-up at a very early stage in the development process and when you do things you do them incrementally in small working steps.
    Last edited by wolle; March 11th, 2019 at 04:08 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)