Thread: Automatic output after variable change?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Automatic output after variable change?

    Ok so firstly let me say that I'm a complete novice with coding and C++.
    Right then, I have an assignment and one of the tasks asks me to automatically print value of my variable (at least, it think its called a variable?) every time i change it. I have no idea how to do this. I think I have to use some sort of boolean thing and the bit of the program that will make the printing happen will be in my class file, I think. But I have no idea how to implement this.

    I'm probably making no sense but guidance would be really appreciated.

    The program I'm making is a small bank account program. I have a class file and a main program file.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think the easiest way to answer that question would be to see your code so we can see how your instructor thinks you should do it.
    As for printing values, you use std::cout.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The obvious way to print out the value of a variable when you change it is to configure a debugger to do that. That is not a C++ programming solution though.

    I suspect the OP has been asked for a class design with a private member, to ensure that the only means of changing that member is in a public member function, and the implementation of that member functions prints out the value after changing it.

    The OP needs to read this link. If he can't understand what he's trying to ask, he can't expect anyone else to understand his question.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Quote Originally Posted by Elysia View Post
    I think the easiest way to answer that question would be to see your code so we can see how your instructor thinks you should do it.
    As for printing values, you use std::cout.
    Thank you for your reply, I'll try and explain my coding to you.

    This is my header file, the relevant lines are probably 114-122.
    [C++] // BankAccount.h // BankAccount class definition #include <string> class Ba - Pastebin.com
    There I have two methods to deposit and withdraw from accounts.

    As I understand it, I can just simply add this line there at 116 and 121:

    Code:
     std::cout << balance << std::endl;
    This will show my balance after every change to it. The only issue is, I only want it to output my balance after a change to only 1 instance of the balance. In my program file i have 4 different accounts (BankAccount account1, account2), (CurrentAccount account1) and (DepositAccount account1). The only one of these accounts I want to have automatically output the result after each change for is the DepositAccount account1.

    Heres my program file if it helps: [C++] #include <iostream> #include "BankAccount.h" int main() { CurrentAccou - Pastebin.com

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So you need a boolean flag in the class to indicate whether to print dalance changes automatically.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to know when a value of a variable change?
    By Hannibal2010 in forum C Programming
    Replies: 12
    Last Post: 11-25-2011, 02:16 PM
  2. Automatic variable
    By George2 in forum C Programming
    Replies: 9
    Last Post: 11-09-2007, 07:38 PM
  3. Malloc and Automatic variable deallocation
    By FireHead in forum C Programming
    Replies: 25
    Last Post: 08-20-2006, 06:06 AM
  4. Difference between Automatic and Static variable
    By viciousv322 in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2005, 04:03 AM