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;
}

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;

Edited 5 Years Ago by Greywolf333: n/a

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;
  }
}

Edited 5 Years Ago by Fbody: n/a

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.

This question has already been answered. Start a new discussion instead.