Can't figure out what is wrong.

Can someone tell me what is wrong with this code.

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

struct DATA{
int ID;
string name;
double balance;
};

//Functions
void ReadData(DATA x[], int a);
void Menu();
void Display(DATA x[], int n);
void IDBalance(DATA x[], int n);
void NameBalance(DATA x[], int n);
void Withdrawal(DATA x[], int n);
void Deposit(DATA x[], int n);

int main()
{
DATA accounts[6];
char choice, yesno;

//Read data
ReadData(accounts, 6);

//Show menu
Menu();

//Input
do{
cout << "Enter you choice: "; cin >> choice;
switch (choice)
{
case'a':Display(accounts, 6); break;
case'b':IDBalance(accounts, 6); break;
case'c':NameBalance(accounts, 6); break;
case'd':Withdrawal(accounts, 6); break;
case'e':Deposit(accounts, 6); break;
}

//Repeat
cout << "\tContinue(y/n)? "; cin >> yesno;
} while (yesno == 'Y' || yesno == 'y');

//Terminate program
system("pause");
return 0;
}

void ReadData(DATA x[], int a)
{
fstream f;
f.open("data.txt", ios::in);
for (int i = 0; i < a; ++i)
{
f >> x[i].ID >> x[i].name >> x[i].balance;
}
f.close();
}
void Menu()
{
cout << "----------Menu----------\n";
cout << "a. Display all records\n";
cout << "b. Show balance when ID is given\n";
cout << "c. Show balance when name is given\n";
cout << "d. Withdraw when ID is given\n";
cout << "e. Deposit when name is given\n";
}
void Display(DATA x[], int n)
{
cout << setfill(' ') << setw(5) << right << "ID\t" << setw(12) << left << "Name" << setw(8) << right << "Balance" << endl;
cout << setfill('­') << setw(32) << right << " \n";
for (int i = 0; i<n; ++i)
{
cout << setfill(' ') << setw(4) << right << x[i].ID << '\t' << setw(12) << left << x[i].name << setw(8) << right << x[i].balance << endl;
}
}
void IDBalance(DATA x[], int n)
{
int ID;
cout << "Enter you ID number: "; cin >> ID;

//Find corresponding records
for (int i = 0; i < n; ++i)
{
if (x[i].ID==ID)
{
int row = i;
}
}
cout << " " << x[row].name << ", your balance is " << x[row].balance << endl;
}
void Deposit(DATA x[], int n)
{
int row;
string name;
double amount = 0;
cout << "Enter your name to deposit: "; cin >> name;

//Find corresponding records
for (int i = 0; i < n; ++i)
{
if (x[i].name == name)
{
row = i;
}
}
//Deposit
cout << "Enter the amount: "; cin >> amount;
x[row].balance += amount;
cout << " " << x[row].name << ", your balance is " << x[row].balance << endl;
}
Last edited on
I think we'd be better guided if you gave us a specific error the compiler is throwing and/or what the program is actually doing versus what it's supposed to be doing.
Error 1 error C2065: 'row' : undeclared identifier c:\users\lester\desktop\visual studio\project 7\project 7\no 2.cpp 96 1 Project 7
Error 3 error C2228: left of '.balance' must have class/struct/union c:\users\lester\desktop\visual studio\project 7\project 7\no 2.cpp 96 1 Project 7
Error 2 error C2228: left of '.name' must have class/struct/union c:\users\lester\desktop\visual studio\project 7\project 7\no 2.cpp 96 1 Project 7
4 IntelliSense: identifier "row" is undefined c:\Users\LESTER\Desktop\Visual Studio\Project 7\Project 7\No 2.cpp 96 20 Project 7
This is the output that I am trying to get. It pulls the information from a data.txt file.

­­­­­­­­­­­­­­­Menu­­­­­­­­­­­­­­­
a. Display all records
b. Show balance when ID is given
c. Show balance when name is given
d. Withdraw when ID is given
e. Deposit when name is given

Enter your choice: a
ID Name Balance ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
22 Angelica 123.45 33
Jacob 2345.67 44
Amanda 345.67 55
Karla 456.78 66
Allyson 5678.9 77
Jonathan 67.89
Continue(y/n)? y

Enter your choice: b
Enter your ID number:
33 Jacob, your balance is 2345.67
Continue(y/n)? y
Enter your choice: c
Enter your name: Amanda Amanda, your balance is 345.67
Continue(y/n)? y

Enter your choice: d
Enter your ID number to withdraw: 66
Enter the amount: 1000 Allyson, your balance is 4678.9
Continue(y/n)? y

Enter your choice: e
Enter your name to deposit: Karla Enter the amount: 50
Karla, your balance is 506.78
Continue(y/n)? n
Excluding the functions you've yet to define, you've only got a small syntax problem in your ID balance function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void IDBalance(DATA x[], int n)
{
    int ID;
    cout << "Enter you ID number: "; cin >> ID;

    //Find corresponding records
    for (int i = 0; i < n; ++i)
    {
        if (x[i].ID==ID)
        {
            int row = i;
        }
    }
    cout << " " << x[row].name << ", your balance is " << x[row].balance << endl;
}


Your compiler is throwing errors at you because on the cout line, the variable row has not been declared. This is because when you do declare, its scope is limited to just the life of the if statement.

Secondly, the way you're doing this is inefficient. You don't need to have the cout statement outside the loop. You can just put it within the if statement, using the ith index that corresponds to the correct id:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void IDBalance(DATA x[], int n)
{
    int ID;
    cout << "Enter you ID number: "; cin >> ID;

    //Find corresponding records
    for (int i = 0; i < n; ++i)
    {
        if (x[i].ID==ID)
        {
            cout << " " << x[i].name << ", your balance is " << x[i].balance << endl;
        }
    }
    
}


Your code runs fine after that in my check (excluding the fact that you've yet to define the NameBalance and Withdrawal functions).
Awesome. Should I put each cout statement inside each loop? I am a beginner and would learn a more efficient way of doing this.
Yes. For all your other search functions you would want to do the same thing. If there is a unique condition (i.e. there are no matching account numbers on names) then once you find the matching case, you no longer need to search through the list. What I omitted from the corrected code is a break statement immediately after you output to the console. This forces the loop to quit after its found the unique case.
1
2
3
4
5
if (x[i].ID==ID)
{
    cout << " " << x[i].name << ", your balance is " << x[i].balance << endl;
    break;
}

For the scope of your problem, this really has no noticeable effect on your program, but say you were searching ten million records for a unique account number and you matched on the first result. You wouldn't want to go and search 9,999,999 records needlessly.

But even then, algorithms will get more efficient the deeper you go into computer science. As a matter of fact, if you are majoring/plan to major in that field, you'd actually take a course on algorithm design.
I am majoring in Computer Engineering. Thanks for the advice Keene.
Topic archived. No new replies allowed.