Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Update a value inside a class using a function

Update a value inside a class using a function

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++questionannouncement
9 Posts 4 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alex1363
    wrote on last edited by
    #1

    I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:

    #include
    #include
    #include
    #include
    #include
    
    using namespace std;
    
    class account
    {
        int acno;
        char name\[50\];
        int deposit;
    
    public:
        void create\_account(int accno)
        {
            acno = accno;
            cout<<"\\nEnter The Name of The account Holder : ";
            cin>>name;
            cout<<"\\nEnter The amount: ";
            cin>>deposit;
            cout<<"\\n\\n\\n === Account Created... ===";
        }   
    
        void show\_account()
        {
            cout<<"\\nAccount No. : "<
    
    K L J 3 Replies Last reply
    0
    • A alex1363

      I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:

      #include
      #include
      #include
      #include
      #include
      
      using namespace std;
      
      class account
      {
          int acno;
          char name\[50\];
          int deposit;
      
      public:
          void create\_account(int accno)
          {
              acno = accno;
              cout<<"\\nEnter The Name of The account Holder : ";
              cin>>name;
              cout<<"\\nEnter The amount: ";
              cin>>deposit;
              cout<<"\\n\\n\\n === Account Created... ===";
          }   
      
          void show\_account()
          {
              cout<<"\\nAccount No. : "<
      
      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      Works for me. Even compiles using g++ -Wall -Wextra with no warnings, so :thumbsup: on that. One issue I do have is that the program cannot handle input for a full name. e.g. Bill works fine, but Bill Smith causes the program to loop indefinitely. You may wan to fix that. One suggestion would be to use getline() rather that std::cin.

      Keep Calm and Carry On

      A 1 Reply Last reply
      0
      • A alex1363

        I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:

        #include
        #include
        #include
        #include
        #include
        
        using namespace std;
        
        class account
        {
            int acno;
            char name\[50\];
            int deposit;
        
        public:
            void create\_account(int accno)
            {
                acno = accno;
                cout<<"\\nEnter The Name of The account Holder : ";
                cin>>name;
                cout<<"\\nEnter The amount: ";
                cin>>deposit;
                cout<<"\\n\\n\\n === Account Created... ===";
            }   
        
            void show\_account()
            {
                cout<<"\\nAccount No. : "<
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        In the deposit_amount function you should break out of the loop after updating the record. So change it to:

        while(File.read((char \*) &ac, (int)sizeof(ac)))
        {
            if(ac.retacno()==n)
            {
                ac.show\_account();
        
                    cout<<"\\n\\nEnter The amount to be deposited: ";
                    cin>>amt;
                    ac.dep(amt);
        
                int pos = -1 \* (int)sizeof(ac);
                File.seekp(pos,ios::cur);
                File.write((char\*)&ac, (int)sizeof(ac));
                cout<<"\\n\\n\\t Record Updated";
                break; // \*\*\*do not need to read any further records
            }
        }
        

        In the display_all function change the while statement to:

        while(!inFile.read((char \*) &ac, sizeof(ac)).eof()) // break if end of file
        
        A 1 Reply Last reply
        0
        • K k5054

          Works for me. Even compiles using g++ -Wall -Wextra with no warnings, so :thumbsup: on that. One issue I do have is that the program cannot handle input for a full name. e.g. Bill works fine, but Bill Smith causes the program to loop indefinitely. You may wan to fix that. One suggestion would be to use getline() rather that std::cin.

          Keep Calm and Carry On

          A Offline
          A Offline
          alex1363
          wrote on last edited by
          #4

          Thanks for your feedback. I am running it using vs-code and it is compiled without any issue. If I run it for the first time I pressed 1, then I entered 100 (Account number), Mark (Name) and 200 (Balance). Then, I pressed 3 to see the table that works fine (I can see all information for this account on the table):

          =============================================
          A/c no. NAME Balance

          100 Mark 200

          Then I pressed 2 to add a deposit and I entered 350. Now I expect this value is added to the previous balance (200) and the new balance should be 550 now. However, after pressing 3, I still see the above table and the balance has not been updated. Do you know where the problem could be? Thanks

          1 Reply Last reply
          0
          • L Lost User

            In the deposit_amount function you should break out of the loop after updating the record. So change it to:

            while(File.read((char \*) &ac, (int)sizeof(ac)))
            {
                if(ac.retacno()==n)
                {
                    ac.show\_account();
            
                        cout<<"\\n\\nEnter The amount to be deposited: ";
                        cin>>amt;
                        ac.dep(amt);
            
                    int pos = -1 \* (int)sizeof(ac);
                    File.seekp(pos,ios::cur);
                    File.write((char\*)&ac, (int)sizeof(ac));
                    cout<<"\\n\\n\\t Record Updated";
                    break; // \*\*\*do not need to read any further records
                }
            }
            

            In the display_all function change the while statement to:

            while(!inFile.read((char \*) &ac, sizeof(ac)).eof()) // break if end of file
            
            A Offline
            A Offline
            alex1363
            wrote on last edited by
            #5

            Thank you so much for your response. After breaking the while as you suggested, it is working fine now :thumbsup:

            L 1 Reply Last reply
            0
            • A alex1363

              I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:

              #include
              #include
              #include
              #include
              #include
              
              using namespace std;
              
              class account
              {
                  int acno;
                  char name\[50\];
                  int deposit;
              
              public:
                  void create\_account(int accno)
                  {
                      acno = accno;
                      cout<<"\\nEnter The Name of The account Holder : ";
                      cin>>name;
                      cout<<"\\nEnter The amount: ";
                      cin>>deposit;
                      cout<<"\\n\\n\\n === Account Created... ===";
                  }   
              
                  void show\_account()
                  {
                      cout<<"\\nAccount No. : "<
              
              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              In deposit_amount() try putting a 'break' after you write to cout. Should do that even if it doesn't fix the problem.

              1 Reply Last reply
              0
              • A alex1363

                Thank you so much for your response. After breaking the while as you suggested, it is working fine now :thumbsup:

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You are welcome. But, as k5054 suggested, there are a few areas that cpuld be tidied up. For example I would read all accounts into memory at the beginning of the program, and only write out new or updated entries. That way you will always have the most up to date information available.

                A 1 Reply Last reply
                0
                • L Lost User

                  You are welcome. But, as k5054 suggested, there are a few areas that cpuld be tidied up. For example I would read all accounts into memory at the beginning of the program, and only write out new or updated entries. That way you will always have the most up to date information available.

                  A Offline
                  A Offline
                  alex1363
                  wrote on last edited by
                  #8

                  I will keep it mind for sure!

                  K 1 Reply Last reply
                  0
                  • A alex1363

                    I will keep it mind for sure!

                    K Offline
                    K Offline
                    k5054
                    wrote on last edited by
                    #9

                    As you're using C++, you might consider using a std::map for the internal data structure. e.g std::map. If you do that, then you can access an account via its account number, without searching for it

                    std::map accounts;
                    accounts[account_no].dep(amount); // add amount to an account;

                    Keep Calm and Carry On

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups