Well I'm working with structures and need some help with a program.

Now if the user chooses to deposit more, I need it to add to the last amount entered... But I'm not sure how to go about doing so.

#include <iostream>

using namespace std;
struct the
{
    int num;
};
int main()
{
    char g;
    the account = {10};
    the check;

do
{
     cout<<"your account balance is $"<< account.num<<endl;

    cout<<"enter amount you wish to deposite..."<<endl;
    cin >>check.num;
    cout <<" total balance is now "<< check.num + account.num<<endl;
    cout <<" would you like to deposite more? y for yes, n for no"<<endl;
    cin >> g;
    cout << endl;
}

while (g=='y');


    return 0;
}

Dani AI

Generated

A short, practical summary and fixes based on the thread: correctly pointed out that the running balance must be updated each time a deposit is entered, and suggested wrapping account logic into functions. later confirmed a simple update-based fix worked. The for-loop attempt, however, has a few concrete bugs and pitfalls worth calling out and fixing.

Problems in the for-loop version: the code uses the loop index after the loop ends (out-of-range access), it computes the total by re-using a single element instead of summing all entries, and it relies on a compiler extension for variable-length arrays (non-standard in C++). Use a dynamic container to store N inputs, or better yet, avoid storing individual checks if you only need the total.

If you do store the amounts, use std::vector and sum with std::accumulate for clarity and safety:

#include <vector>
#include <numeric>

std::vector<long long> checks(n);
for (size_t i = 0; i < checks.size(); ++i) {
  std::cin >> checks[i];
}
long long total = std::accumulate(checks.begin(), checks.end(), 0LL);

See std::vector and std::accumulate.

If you do not need to remember each check, update the account balance as each deposit is entered (no array required). Validate inputs (no negative amounts), handle upper/lower-case answers for y/n, and avoid floating-point types for money — use integer cents (e.g., long long) to prevent rounding errors. Rename your types to meaningful names (Account, Check) instead of cryptic ones like the, and consider small helper functions or member methods to encapsulate deposit/withdraw logic and validation.

Recommended Answers

All 5 Replies

Well I'm working with structures and need some help with a program.

Now if the user chooses to deposit more, I need it to add to the last amount entered... But I'm not sure how to go about doing so.

#include <iostream>

using namespace std;
struct the
{
    int num;
};
int main()
{
    char g;
    the account = {10};
    the check;

do
{
     cout<<"your account balance is $"<< account.num<<endl;

    cout<<"enter amount you wish to deposite..."<<endl;
    cin >>check.num;
    cout <<" total balance is now "<< check.num + account.num<<endl;
    cout <<" would you like to deposite more? y for yes, n for no"<<endl;
    cin >> g;
    cout << endl;
}

while (g=='y');


    return 0;
}

Your struct is called "the"? Fascinating...
Anyway, you can add to it in much the same way you accessed it to deposit initially, only:
account.num = account.num + check.num;

Well scratch that, sorry, I was working on this for the past couple hours and figured do while loop would be to difficult.
So I decided to try a For loop instead. Which I got it to work where it asks for how many checks would like to be deposited, but I also need to get the total amount of checks deposited and I'm unsure how to do that.

#include <iostream>

using namespace std;
struct the
{
    int balance;
    int deposite;
};
int main()
{
    int num;
    cout <<"how many checks will you be depositing?" <<endl;
    cin >> num;
    const int checkdep=num;
    the check[checkdep];
    int index;

    for (index =0; index <checkdep; index++)
        {
        cout <<"enter the amount deposited for check number "<< (index +1) <<endl;
        cin >> check[index].deposite;
        }
        double total;
    total = check[index].deposite + check[index].deposite;
    cout<< total;
    return 0;
}

Okay, I'm sorry, but I can't take it. Don't name your struct "the". Other programmers will think you're weird and unstable.

Anyway hehe to answer your question, just make a variable inside your struct to store that information... so if you have an int count inside "THE CHECK" heh, you could just do check.count++

The way this is usually done is with an "account" type that has a "balance" member and "deposit" and "withdraw" methods.

Are you familiar with functions? I think it would be advisable to create deposit and withdraw functions that handle the manipulation of the account(s) for you.:

struct acct {
  int balance;
  //...
};

void deposit(acct &, int);     //a function for performing deposits
void withdraw(acct &, int);    //a function for performing withdrawals

const int MIN_BALANCE = 5;

int main() {
  //...appropriate code...
  return 0;
}

void deposit(acct &theAcct, int amount) {
  theAcct.balance += amount;
}

void withdraw (acct &theAcct, int amount) {
  const int MAX_WITHDRAWAL = theAcct.balance-MIN_BALANCE;
  if (amount < MAX_WITHDRAWAL) {
    theAcct.balance -= amount;
  }
}

Thank you all for your help! I took greywolf's advice and added the account.num = account.num + check.num;

and ended up not using the for loop because I'm not familiar enough.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.