Home > Software design >  For loop and Arrays [C Simple ATM system]
For loop and Arrays [C Simple ATM system]

Time:11-27

So I am trying to create a ATM system that lets user to input value such as Account number, account name and amount. But I can't figure out what exactly I have to do

int AccNum[2];
string AccName[2];
float AccBal[2];

cout << "********** ENTER ACCOUNT **********"<<endl;
    
for(int num = 0; num < 2; num  ){
    cout << "Enter Account number: ";
    cin >> AccNum[num];
    
    for(int name = num; name < 2; name  ){
        cout << "Enter Account Name: ";
        getline(cin, AccName[name]);
        
        for(int bal = name; bal < 2; bal  ){
            cout << "Enter Amount: ";
            cin >> AccBal[bal];
        }
    }
}

I have tried something like this but it does not give the result that I want. The ideal result would be

********** ENTER ACCOUNT **********
Enter Account number: 1231232
Enter Account name: James white
Enter amount: 1000

it will run 2 times so there would be 2 accounts after this loop that will have a result like this

********** THE ACCOUNT IS **********
Account number: 1231232
Account name: James white
Balance: 1000

CodePudding user response:

So the code you wrote is nearly good. Too many loops in my opinion

int AccNum[2];
string AccName[2];
float AccBal[2];

cout << "********** ENTER ACCOUNT **********"<<endl;
    
for(int num = 0; num < 2; num  ){ // lets call this loop a. happens 2 times
    cout << "Enter Account number: ";
    cin >> AccNum[num];
    
    for(int name = num; name < 2; name  ){ // loop b happens 2 times * loop a times
        cout << "Enter Account Name: ";
        getline(cin, AccName[name]);
        
        for(int bal = name; bal < 2; bal  ){ loop c = 2 * a * b = 8
            cout << "Enter Amount: ";
            cin >> AccBal[bal];
        }
    }
}

Direct fix:

int main()
{
    int AccNum[2];
    string AccName[2];
    float AccBal[2];
    cout << "********** ENTER ACCOUNT **********" << endl;
    for(int num = 0; num < 2; num  ){
        cout << "Enter Account number: ";
        cin >> AccNum[num];
        cout << "Enter Account Name: ";
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        getline(cin, AccName[num]);
        cout << "Enter Amount: ";
        cin >> AccBal[num];
    }

    for(int i = 0; i < 2; i  )
        cout << "********** ENTER ACCOUNT **********" << endl
             << "Account number: " << AccNum[i] << endl
             << "Account name: " << AccName[i] << endl
             << "Balance: " << AccBal[i] << endl << endl;
}

But if you want to expand it a little bit:

#include <iostream>
#include <vector>
using namespace std;

struct Account
{
    int Number;
    string Name;
    float Balance;

    Account(int num, string nam, float bal) : Number(num), Name(nam), Balance(bal) {}
    Account() {}

    void getData()
    {
        cout << "Enter Account number: ";
        cin >> Number;
        cout << "Enter Account Name: ";
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        getline(cin, Name);
        cout << "Enter Amount: ";
        cin >> Balance;
    }

    void printAccount()
    {
        cout << "********** ENTER ACCOUNT **********" << endl
             << "Account number: " << Number << endl
             << "Account name: " << Name << endl
             << "Balance: " << Balance << endl << endl;
    }
};

int main()
{
    vector<Account> accounts;
    for(int i = 0; i < 2; i  )
    {
        Account person;
        person.getData();
        accounts.emplace_back(person);
    }

    for(int i = 0; i < 2; i  ) accounts[i].printAccount();
}

Both codes give the exact same output: codes

  • Related