register bug

This is a discussion on register bug within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm having a little bit of trouble with a register I'm making. I'm having an error in the ...

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    register bug

    Hey guys, I'm having a little bit of trouble with a register I'm making. I'm having an error in the build. Also as I'm very new to c++ if you see anything that I can improve on, such as syntax, etc. please tell me as I wish to form good habits. btw, I'm using code blocks if it makes any difference.
    Heres head.hpp
    Code:
    #include <iostream>
    
    class My_reg
    {
        public:
            My_reg(float amount);    //enters amount
            ~My_reg();
            void set_tax();      // declares tax func
            void start_up() const {sub = amount;}
            float sub_total() const {return sub;}   // returns sub_total amount
            float get_total() const {return sub;}  // return sub after tax
            float get_change(float cash);      // declares func to get change
        private:
            float sub;
            float tax;
            float cash;
            float change;
    };
    source
    Code:
    #include "head.hpp"
    
    My_reg::My_reg(float amount)
    {
        sub = amount;
        float tax = 0.05;
    }
    My_reg:: ~My_reg()
    {
    }
    void My_reg:: set_tax()
    {
        sub = sub + sub * tax;   //adds tax
    }
    float My_reg:: get_change(float cash)
    {
        change = (cash - sub);   /// This is supposed to take how much they gave
        return change;               // and subtract from sub, this is after sub has been through set_tax
    }
    int main()
    {
    	std::cout << "Please enter the amount:";
    	std::cin >> amount;
    	My_reg reg(amount);
    	start_up();
    	std::cout << "Your sub-total is:" << reg.sub_total();
    	reg.set_tax();
    	std::cout << "/nYour total is:" << reg.get_total();
    	std::cout << "/n Please enter the amount paid:";
    	std::cin >> cash;
    	std::cout << "Your change is:" << reg.get_change();
    	std::cout << "/n/tThank you and have a wonderful day";
    
    
    	return 0;
    }
    Thank you for any help && || tips.

    Joe
    Last edited by avgprogamerjoe; 07-09-2007 at 07:31 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What error do you get? Copy all the errors (or if there are a lot copy the first dozen or so) and paste them here.

    >> float tax = 0.05 ;
    The first thing I see is that you try to initialize the member variable tax to be 0.05 inside the class definition. You probably want that to be a static const member, so if you know what that is, do that. You can also make it a const that is not a member of the class. Otherwise, you'll have to initialize it in the constructor.

    >> (cash - sub_total)= change;
    Another thing I see is this. When assigning values, the variable that gets the new value should be on the left.

    Generally, when working on projects like this you want to compile often as you go, rather than writing a bunch of code at once and then compiling. That makes it easier to identify the problems and fix them.

    One last tip, unless you've been instructed otherwise, you should prefer double rather than float. It has better precision and is the default floating point type in C++.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    It would help us if you could paste the compiler's error messages.

    Quote Originally Posted by avgprogamerjoe View Post
    Hey guys, I'm having a little bit of trouble with a register I'm making. I'm having an error in the build. Also as I'm very new to c++ if you see anything that I can improve on, such as syntax, etc. please tell me as I wish to form good habits. btw, I'm using code blocks if it makes any difference.
    Heres head.hpp
    Code:
    #include <iostream>
    
    class My_reg
    {
        public:
            My_reg(float amount);    //enters amount
            ~My_reg();
            void tax();      // declares tax func
            void start_up() {sub_total = amount}
            float sub_total() {return sub_total}   // returns sub_total amount
            float get_total() {return sub_total}  // return sub after tax
            float get_change(float cash);      // declares func to get change
        private:
            float sub_total;
            float tax = 0.05 ;
            float cash;
            float change;
    };
    main.cpp
    Code:
    #include "head.hpp"
    
    My_reg::My_reg(float amount)
    {
        sub_total = amount;
    }
    My_reg:: ~My_reg()
    {
    }
    My_reg:: tax()
    {
        sub_total = sub_total + sub_total * tax;   //adds tax
    }
    My_reg:: get_change(float cash)
    {
        (cash - sub_total)= change;
        return change;
    }
    int main()
    {
    	std::cout << "Please enter the amount:";
    	std::cin << amount;
    	My_reg reg(amount);
    	start_up();
    	std::cout << "Your sub-total is:" << reg.sub_total();
    	reg.tax();
    	std::cout << "/nYour total is:" << reg.get_total();
    	std::cout << "/n Please enter the amount paid:";
    	std::cin << cash;
    	std::cout << "Your change is:" << reg.get_change();
    	std::cout << "/n/tThank you and have a wonderful day";
    
    
    	return 0;
    }
    Thank you for any help && || tips.

    Joe
    The line highlighted in bold red is an error - you need to initialise data members in the class's constructor.

    The line highlighted in bold blue is also an error - what are you trying to do here?

    Also, check your operators after cin - the "Get input from" operator is >>

    There may be other errors too, these are just the ones i spotted from skim-reading..

  4. #4
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    Thank you for your help. I fixed the tax var so it's declared in constructor, as well is sub. Also it seemed to help with the debug that i changed the vars to not match their function ID. Is this correct?
    Also I added a few more things and debugged it to about here. // I edited the code above to save space.


    Thanks again,

    Joe
    One last tip, unless you've been instructed otherwise, you should prefer double rather than float. It has better precision and is the default floating point type in C++.
    No, I'm not restricted to anything as I'm a hobbyist.(Though I want too keep good habits, and not set myself up later) But I have a book that I'm going by, and on this little "cheat sheet" it gave me, it says that a double is 8 bytes, and stores2.2e-308 to 1.8e-308. I guess that I thought that was a large number, but apparently not. Could you maybe explain what that means? Thanks
    Last edited by avgprogamerjoe; 07-09-2007 at 07:35 PM.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    i noticed that your sub_total get_total fucntions do the same thing, you only need one of them

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No, I'm not restricted to anything as I'm a hobbyist.(Though I want too keep good habits, and not set myself up later) But I have a book that I'm going by, and on this little "cheat sheet" it gave me, it says that a double is 8 bytes, and stores2.2e-308 to 1.8e-308. I guess that I thought that was a large number, but apparently not. Could you maybe explain what that means? Thanks
    Wikipedia's article on Floating Points.
    double is essentially a better float - it has more range, and better precision. It also takes up more space, but the extra space needed is insignificant.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    1.
    Code:
        float tax = 0.05;
    You're declaring a variable here that is different to the one in your class. It is destroyed when the constructor finishes and the value 0.05 is lost. You need to take away the word 'float' there.
    However you should be using "constructor initialiser lists" anyway, so google that.

    2. Don't declare or define a destructor if you aren't going to put code in it. The default destructor that the compiler generates for you will do the same thing.

    3.
    Code:
        sub_total = sub_total + sub_total * tax;   //adds tax
    This is the same as
    Code:
        sub_total *= (1 + tax);   //adds tax
    Which I'd certainly go for.

    4. The member variable 'change' doesn't need to be a class member variable because you don't use it outside of the one function. Declare it locally instead if necessary. However get_change could just return the result of the calculation as a one-liner with no need to declare any extra variables.

    5. sub_total and get_total are identical. Is there any point in declaring both?

    6.
    Code:
            void start_up() const {sub = amount;}
    That won't compile because there is no identifier called 'amount' that is visible to that function.

    7.
    Code:
    	start_up();
    Wont compile either as there is no global function with that name. You can remove this line.
    Last edited by iMalc; 07-10-2007 at 01:27 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    // I edited the code above to save space.
    I am sorry, I guess the way i put it was unclear, I edited the first post. The one with the qoute contains my old code, and I do believe that you based your judgment off that.

    Once again sorry,
    Joe
    P.S. I will continue to edit first post as I try to fix more bugs.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Post new code in a new post rather than editing the first one. It makes following the thread easier.

    All of iMalc's comments apply to the current code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. register variables
    By BEN10 in forum C Programming
    Replies: 9
    Last Post: 04-17-2009, 07:20 AM
  2. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. register file
    By axon in forum Tech Board
    Replies: 0
    Last Post: 11-20-2003, 08:07 AM
  5. difference between register int and normal int
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-25-2003, 03:01 PM

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