13 Replies - 1309 Views - Last Post: 23 November 2011 - 06:05 PM Rate Topic: -----

#1 earl01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Trying to get this program to run on DEV C++

Posted 22 November 2011 - 10:05 AM

This program uses a structure to store the following data about a customer account:
NAME
ADDRESS
CITY, STATE, AND ZIP
TELEPHONE NUMBER
ACCOUNT BALANCE
DATE OF LAST PAYMENT

The structure should be used to store customer account records in a file. The program should have a menu that lets the user perform the following operations:

• Enter new records into the file.
• Search for a particular customer’s record and display it.
• Search for a particular customer’s record and delete it.
• Search for a particular customer’s record and change it.
• Display the contents of the entire file.

Input Validation: When the data for a new account is entered, be sure the user enters data for all the fields. No negative account balances should be entered.





// info.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream?
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct Info
{
string name;
string address;
string city;
string state;
string zip;
string tele;
double balance;
string date;
};

vector < Info > Bank;

bool more();
Info takeIn();
void flushCin();
void newAcc();
void menu();
void showAll();
void editAcc( int );

int main()
{
for(;;)/> menu();
}
bool more()
{
cout << "More (y/n) ? ";
int reply = cin.get();
if(reply != '\n') flushCin();
return reply=='y' || reply=='Y';
}

void flushCin()
{
while( cin.get() != '\n' );
}

Info takeIn()
{
Info file;
bool ok;
do
{
cout << "NAME : ";
getline( cin, file.name );

cout << "ADDRESS : ";
getline( cin, file.address );

cout << "CITY : ";
getline( cin, file.city );

cout << "STATE : ";
getline( cin, file.state );

cout << "ZIP : ";
getline( cin, file.zip );

cout << "TELEPHONE NUMBER : ";
getline( cin, file.tele );

cout << "DATE OF LAST PAYMENT : ";
getline( cin, file.date );

for(;;)/>
{
cout << "Balance : ";
cin >> file.balance;
if( !cin.good() )
{
cout << "\nERROR! Entry NOT accepted! ... Re-enter numbers only!\n";
cin.clear();
flushCin();
continue;
flushCin();
break;
}
cout<< "\nYou entered:" << endl;
int w =45;
cout<< left;
cout<< setw(w) << "Customer's name is " << file.name << endl;
cout<< setw(w) << "Customer's address is " << file.address << endl;
cout<< setw(w) << "Customer's current city of residence is " << file.city << endl;
cout<< setw(w) << "Customer's current state of residence is " << file.state << endl;
cout<< setw(w) << "Customer's zip code is " << file.zip << endl;
cout<< setw(w) << "Customer's current telephone number is " << file.tele << endl;
cout<< setw(w) << "Customer's late date of payment is " << file.date << endl;
cout<< setw(w) << "Customer's late recorded account balance is " << file.balance << endl;
cout<< right;

cout << "Ok ... (y/n) ? ";
int reply = cin.get();
if(reply != '\n') flushCin();
ok = (reply=='y' || reply=='Y');

}while(!ok );
return file;
}
void menu()
{
cout << "\nWelcome to you bank company that has lost all your money.\n"
<< "Please choose an action from the menu displayed below.\n\n"
<< "Press 1 to enter a new account(s).\n"
<< "Press 2 to see ALL your current balances.\n"
<< "Press 3 to edit a current balance.\n"
<< "Press 4 to exit.\n\n"
<< "Your choice : " << flush;

string temp;
getline( cin, temp );
int choice = atoi( temp.c_str() );

switch(choice)
{
case 1:
do { newAcc(); } while( more() );
break;

case 2:
cout << "All your current account balances are ..." << endl;
showAll();
break;

case 3:
showAll();
cout << "\nWhich account to edit ? ";
getline( cin, temp );
choice = atoi( temp.c_str() );
editAcc( choice-1 );
break;

case 4:
exit(1);

default:
cout << "\nNot a valid choice ..." << endl;
}
}

void newAcc()
{
Info tmpFile = takeIn();
Bank.push_back( tmpFile );
}

void showAll()
{
int w = 45;
for(unsigned i = 0; i < Bank.size(); ++i )
{
cout<< setw(4) << "Customer's number " << i+1 << endl;
cout<< left;
cout<< setw(w) << "Customer's name is " << Bank[i].name << endl;
cout<< setw(w) << "Customer's address is " << Bank[i].address << endl;
cout<< setw(w) << "Customer's current city of residence is " << Bank[i].city << endl;
cout<< setw(w) << "Customer's current state of residence is " << Bank[i].state << endl;
cout<< setw(w) << "Customer's zip code is " << Bank[i].zip << endl;
cout<< setw(w) << "Customer's current telephone number is " << Bank[i].tele << endl;
cout<< setw(w) << "Customer's late date of payment is " << Bank[i].date << endl;
cout<< setw(w) << "Customer's late recorded account balance is " << Bank[i].balance << endl;
cout<< right << "\nPress 'Enter' to continue ... ";
flushCin();
}
}

void editAcc( int i )
{
if( i > int(Bank.size()) || i < 0 )
{
cout << "\nNo Account with this number " << i+1 << endl;
return;
}
cout << "\nEnter new data ...\n" << flush;

Info tmp = takeIn();
Bank[i].name = tmp.name;
Bank[i].address = tmp.address;
Bank[i].city = tmp.city;
Bank[i].state = tmp.state;
Bank[i].zip = tmp.zip;
Bank[i].tele = tmp.tele;
Bank[i].date = tmp.date;
Bank[i].balance = tmp.balance;
}
 
                    


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to get this program to run on DEV C++

#2 pjammin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-November 11

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 10:43 AM

Hello there. Instead of using a structure to hold the data, why don't you make it a class and add mutator (setter) functions and do the validation within those functions? Is there any reason that this must be a structure?
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 10:50 AM

> Instead of using a structure to hold the data, why don't you make it a class

Structures and classes are the same, except for default visibility.

> add mutator (setter) functions and do the validation within those functions?

The validation is an input issue. Why would interleave storage of data with input of data? That's bad design.
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 11:45 AM

This needs a fix ... something like ...
for(;;)/>
{
   cout << "Balance : " << flush;
   cin >> file.balance;
   if( !cin.good() )
   {
      cout << "\nERROR! Entry NOT accepted! ... Re-enter numbers only!\n";
      cin.clear();
      flushCin();
      continue; 
   }
   // else ...
   flushCin();
   break;
}


But it's good to handle most probable case first ...
for(;;)/>
{
   cout << "Balance : " << flush;
   if( cin >> file.balance )
   {
      flushCin();
      break; // good data, so break out of for loop now
   }
   // else ...
   cout << "\nERROR! Entry NOT accepted! ... Re-enter numbers only!\n";
   cin.clear();
   flushCin();
}

This post has been edited by David W: 22 November 2011 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 11:45 AM

@earl01
You need to find an indentation style you like and use it consistently. Your code, as presented, is almost impossible to read. You have also failed to ask any questions. Is there a problem with your code? Does your code compile without errors? If not post the complete error messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 11:59 AM

Also ... don't use a global vector object. Your vector can be declared inside main or within the scope needed, then passed to any functions that use it ... using a form of passing by reference ...

You may like to see the snippets here at DIC that demo similar problems ... Or here ...
http://developers-heaven.net/forum/index.php/topic,2585.0.html

This post has been edited by David W: 22 November 2011 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 earl01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 01:12 PM

View PostOler1s, on 22 November 2011 - 10:50 AM, said:

> Instead of using a structure to hold the data, why don't you make it a class

Structures and classes are the same, except for default visibility.

> add mutator (setter) functions and do the validation within those functions?

The validation is an input issue. Why would interleave storage of data with input of data? That's bad design.


View PostOler1s, on 22 November 2011 - 10:50 AM, said:

> Instead of using a structure to hold the data, why don't you make it a class

Structures and classes are the same, except for default visibility.

> add mutator (setter) functions and do the validation within those functions?

The validation is an input issue. Why would interleave storage of data with input of data? That's bad design.


The program is suppose to use a structure, I can not get it to compile in DEV C++.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 01:16 PM

If the program won't compile, post the error messages exactly as they appear in your development environment. Also ask specific questions about the code or error messages the code produces.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 02:18 PM

If you start with a working shell, that compiles and runs and outputs the exact output expected ...
then add a function and recompile as above ...
you will know that a new compile problem is probably limited to your new added code chunk ...
The goal is to progress in steps and to learn to understand the compiler error messages to help you quickly locate/fix the problem in your code ...
Was This Post Helpful? 0
  • +
  • -

#10 earl01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 02:38 PM

This program uses a structure to store the following data about a customer account:
NAME
ADDRESS
CITY, STATE, AND ZIP
TELEPHONE NUMBER
ACCOUNT BALANCE
DATE OF LAST PAYMENT

The structure should be used to store customer account records in a file. The program should have a menu that lets the user perform the following operations:

• Enter new records into the file.
• Search for a particular customer’s record and display it.
• Search for a particular customer’s record and delete it.
• Search for a particular customer’s record and change it.
• Display the contents of the entire file.

Input Validation: When the data for a new account is entered, be sure the user enters data for all the fields. No negative account balances should be entered.



#include <iostream>

#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct Info
{
string name;
string address;
string city;
string state;
string zip;
string tele;
double balance;
string date;
};

vector < Info > Bank;

bool more();
Info takeIn();
void flushCin();
void newAcc();
void menu();
void showAll();
void editAcc( int );

int main()
{
for(;;)/> menu();
}
bool more()
{
cout << "More (y/n) ? ";
int reply = cin.get();
if(reply != '\n') flushCin();
return reply=='y' || reply=='Y';
}

void flushCin()
{
while( cin.get() != '\n' );
}

Info takeIn()
{
Info file;
bool ok;
do
{
cout << "NAME : ";
getline( cin, file.name );

cout << "ADDRESS : ";
getline( cin, file.address );

cout << "CITY : ";
getline( cin, file.city );

cout << "STATE : ";
getline( cin, file.state );

cout << "ZIP : ";
getline( cin, file.zip );

cout << "TELEPHONE NUMBER : ";
getline( cin, file.tele );

cout << "DATE OF LAST PAYMENT : ";
getline( cin, file.date );

for(;;)/>
{
cout << "Balance : ";
cin >> file.balance;
if( !cin.good() )
{
cout << "\nERROR! Entry NOT accepted! ... Re-enter numbers only!\n";
cin.clear();
flushCin();
continue;
flushCin();
break;
}
cout<< "\nYou entered:" << endl;
int w =45;
cout<< left;
cout<< setw(w) << "Customer's name is " << file.name << endl;
cout<< setw(w) << "Customer's address is " << file.address << endl;
cout<< setw(w) << "Customer's current city of residence is " << file.city << endl;
cout<< setw(w) << "Customer's current state of residence is " << file.state << endl;
cout<< setw(w) << "Customer's zip code is " << file.zip << endl;
cout<< setw(w) << "Customer's current telephone number is " << file.tele << endl;
cout<< setw(w) << "Customer's late date of payment is " << file.date << endl;
cout<< setw(w) << "Customer's late recorded account balance is " << file.balance << endl;
cout<< right;

cout << "Ok ... (y/n) ? ";
int reply = cin.get();
if(reply != '\n') flushCin();
ok = (reply=='y' || reply=='Y');

}while(!ok );
return file;
}
void menu()
{
cout << "\nWelcome to you bank company that has lost all your money.\n"
<< "Please choose an action from the menu displayed below.\n\n"
<< "Press 1 to enter a new account(s).\n"
<< "Press 2 to see ALL your current balances.\n"
<< "Press 3 to edit a current balance.\n"
<< "Press 4 to exit.\n\n"
<< "Your choice : " << flush;

string temp;
getline( cin, temp );
int choice = atoi( temp.c_str() );

switch(choice)
{
case 1:
do { newAcc(); } while( more() );
break;

case 2:
cout << "All your current account balances are ..." << endl;
showAll();
break;

case 3:
showAll();
cout << "\nWhich account to edit ? ";
getline( cin, temp );
choice = atoi( temp.c_str() );
editAcc( choice-1 );
break;

case 4:
exit(1);

default:
cout << "\nNot a valid choice ..." << endl;
}
}

void newAcc()
{
Info tmpFile = takeIn();
Bank.push_back( tmpFile );
}

void showAll()
{
int w = 45;
for(unsigned i = 0; i < Bank.size(); ++i )
{
cout<< setw(4) << "Customer's number " << i+1 << endl;
cout<< left;
cout<< setw(w) << "Customer's name is " << Bank[i].name << endl;
cout<< setw(w) << "Customer's address is " << Bank[i].address << endl;
cout<< setw(w) << "Customer's current city of residence is " << Bank[i].city << endl;
cout<< setw(w) << "Customer's current state of residence is " << Bank[i].state << endl;
cout<< setw(w) << "Customer's zip code is " << Bank[i].zip << endl;
cout<< setw(w) << "Customer's current telephone number is " << Bank[i].tele << endl;
cout<< setw(w) << "Customer's late date of payment is " << Bank[i].date << endl;
cout<< setw(w) << "Customer's late recorded account balance is " << Bank[i].balance << endl;
cout<< right << "\nPress 'Enter' to continue ... ";
flushCin();
}
}

void editAcc( int i )
{
if( i > int(Bank.size()) || i < 0 )
{
cout << "\nNo Account with this number " << i+1 << endl;
return;
}
cout << "\nEnter new data ...\n" << flush;

Info tmp = takeIn();
Bank[i].name = tmp.name;
Bank[i].address = tmp.address;
Bank[i].city = tmp.city;
Bank[i].state = tmp.state;
Bank[i].zip = tmp.zip;
Bank[i].tele = tmp.tele;
Bank[i].date = tmp.date;
Bank[i].balance = tmp.balance;
}



Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 02:44 PM

Please don't open a new topic when you already have an existing topic for the same problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 earl01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 02:48 PM

View Postjimblumberg, on 22 November 2011 - 01:16 PM, said:

If the program won't compile, post the error messages exactly as they appear in your development environment. Also ask specific questions about the code or error messages the code produces.


Jim

These are the error messages I get
Was This Post Helpful? 0
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Trying to get this program to run on DEV C++

Posted 22 November 2011 - 07:35 PM

Ok ... you were just missing a few things to get it to compile ...see comments in the edited program.

If you get the indent thing down pat ... your code will be much easier for you to see what structures are incomplete ... and thus need fixing.

Otherwise ... I like your coding style and you seem off to a good start to an exciting C++ (and C) programming future :)

// structBank.cpp

#include <iostream>
#include <fstream> // re. ostream and istream objects ...
#include <string>
#include <vector>
#include <iomanip>
#include <cstdlib> // re. atoi and exit(1)
#include <cctype> // re. tolower ...

using namespace std;

const string HEADER =
    "You have arrived at 'TRUSTWORTHY BANK' that owns all 'your' money.\n"
    "Choose an action from the menu displayed below ...\n\n"
    "Enter 1 to create (a) new account(s)\n"
    "Enter 2 to see ALL current balances IN BANK\n"
    "Enter 3 to edit a current balance\n"
    "Enter 4 to exit\n\n"
    "Your choice : ";

struct Info
{
    string name;
    string address;
    string city;
    string state;
    string zip;
    string tele;
    double balance;
    string date;
};

vector < Info > Bank; // Global ok HERE to keep this a simple student program

ostream& operator << ( ostream& os, const Info& rec )
{
    int w = 45;
    os << left
       << setw(w) << "Customer's name is " << rec.name << '\n'
       << setw(w) << "Customer's address is " << rec.address << '\n'
       << setw(w) << "Customer's current city of residence is " << rec.city << '\n'
       << setw(w) << "Customer's current state of residence is " << rec.state << '\n'
       << setw(w) << "Customer's zip code is " << rec.zip << '\n'
       << setw(w) << "Customer's current telephone number is " << rec.tele << '\n'
       << setw(w) << "Customer's last date of payment is " << rec.date << '\n'
       << setw(w) << "Customer's last account balance is " << rec.balance
       << endl << right;
    return os;
}

void flushStream( istream& is );
int getReply( const string& msg );
istream& operator >> ( istream& is, Info& rec ) // designed for keyboard entry
{
    do
    {
        cout << "NAME : " << flush;
        getline( is, rec.name );

        cout << "ADDRESS : "<< flush;
        getline( is, rec.address );

        cout << "CITY : "<< flush;
        getline( is, rec.city );

        cout << "STATE : "<< flush;
        getline( is, rec.state );

        cout << "ZIP : "<< flush;
        getline( is, rec.zip );

        cout << "TELEPHONE NUMBER : "<< flush;
        getline( cin, rec.tele );

        cout << "DATE OF LAST PAYMENT : "<< flush;
        getline( is, rec.date );

        for(;;)/>
        {
            cout << "Balance : "<< flush;;
            if( is >> rec.balance )
            {
                flushStream( is );
                break; // good data ... so break right now ...

            } // you forgot this }
            // else ...
            cout << "\nERROR! Enter only numeric value for balance ...\n";
            is.clear();
            flushStream( is );
        }

        cout << "\nYou entered:\n"
             << rec;

    }while( getReply( "Ok ... (y/n) ? " ) != 'y' );

    return is;
}

bool showMenuDoJob(); // changed this to bool ... to avoid call to exit
void newAcc();
void showAll();
void editAcc( int );



int main() ///////////////////////////////////
{
    for( ; ; ) if( !showMenuDoJob() ) return 0;
    
} ////////////////////////////////////////////



void flushStream( istream& is )
{
    while( is.get() != '\n' );
}

int getReply( const string& msg )
{
    cout << msg;
    int reply = cin.get();
    if( reply != '\n' ) flushStream( cin );
    return tolower( reply );
}

bool showMenuDoJob()
{
    cout << HEADER << flush;

    string tmp;
    getline( cin, tmp );
    int choice = atoi( tmp.c_str() );

    switch( choice )
    {
        case 1:
            do { newAcc(); } while( getReply( "More (y/n) ? " ) == 'y' );
        break;

        case 2:
            cout << "All your current account balances are ...\n";
            showAll();
        break;

        case 3:
            showAll();
            cout << "\nWhich account number to edit ? "<< flush;
            getline( cin, tmp );
            choice = atoi( tmp.c_str() );
            editAcc( choice-1 );
        break;

        case 4: return false;

        default: cout << "\nNot a valid choice ..." << endl;
    }
    return true;
}

void newAcc()
{
    Info rec;
    cin >> rec;
    Bank.push_back( rec );
}

void showAll()
{
    for( size_t i = 0; i < Bank.size(); ++i )
    {
        cout << "Customer's number " << setw(4) << i+1 << '\n'
             << Bank[i]
             
             << "\nPress 'Enter' to continue ... " << flush;
        flushStream( cin );
    }
}

void editAcc( int i )
{
    if( 0 <= i  &&  i < int(Bank.size()) ) // highest valid index is size()-1
    {
        cout << "\nEnter new data ...\n" << flush;
        cin >> Bank[i];
    }
    else cout << "\nNo Account with this number " << i+1 << endl;
}


This post has been edited by David W: 23 November 2011 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#14 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Trying to get this program to run on DEV C++

Posted 23 November 2011 - 06:05 PM

You may like to see a version of the above that doesn't use a global vector container ...

// structBankNoGlobalBankVec.cpp

#include <iostream>
#include <fstream> // re. ostream and istream objects ...
#include <string>
#include <vector>
#include <iomanip>
#include <cstdlib> // re. atoi
#include <cctype> // re. tolower ...

using namespace std;

const string HEADER =
    "You have arrived at 'TRUSTWORTHY BANK' that owns all 'your' money.\n"
    "Choose an action from the menu displayed below ...\n\n"
    "Enter 1 to create (a) new account(s)\n"
    "Enter 2 to see ALL current balances IN BANK\n"
    "Enter 3 to edit a current balance\n"
    "Enter 4 to exit\n\n"
    "Your choice : ";

struct Info
{
    string name;
    string address;
    string city;
    string state;
    string zip;
    string tele;
    double balance;
    string date;
};

ostream& operator << ( ostream& os, const Info& rec )
{
    int w = 45;
    os << left
       << setw(w) << "Customer's name is " << rec.name << '\n'
       << setw(w) << "Customer's address is " << rec.address << '\n'
       << setw(w) << "Customer's current city of residence is " << rec.city << '\n'
       << setw(w) << "Customer's current state of residence is " << rec.state << '\n'
       << setw(w) << "Customer's zip code is " << rec.zip << '\n'
       << setw(w) << "Customer's current telephone number is " << rec.tele << '\n'
       << setw(w) << "Customer's last date of payment is " << rec.date << '\n'
       << setw(w) << "Customer's last account balance is " << rec.balance
       << endl << right;
    return os;
}

void flushStream( istream& is );
int getReply( const string& msg );
istream& operator >> ( istream& is, Info& rec ) // designed for keyboard entry
{
    do
    {
        cout << "NAME : " << flush;
        getline( is, rec.name );

        cout << "ADDRESS : "<< flush;
        getline( is, rec.address );

        cout << "CITY : "<< flush;
        getline( is, rec.city );

        cout << "STATE : "<< flush;
        getline( is, rec.state );

        cout << "ZIP : "<< flush;
        getline( is, rec.zip );

        cout << "TELEPHONE NUMBER : "<< flush;
        getline( cin, rec.tele );

        cout << "DATE OF LAST PAYMENT : "<< flush;
        getline( is, rec.date );

        for(;;)/>
        {
            cout << "Balance : "<< flush;;
            if( is >> rec.balance )
            {
                flushStream( is );
                break; // good data ... so break right now ...

            }
            // else ...
            cout << "\nERROR! Enter only numeric value for balance ...\n";
            is.clear();
            flushStream( is );
        }

        cout << "\nYou entered:\n"
             << rec;

    }while( getReply( "Ok ... (y/n) ? " ) != 'y' );

    return is;
}

bool showMenuDoJob( vector< Info >& );
void newAcc( vector< Info >& );
void showAll( const vector< Info >& );
void editAcc( vector< Info >&, int );



int main() /////////////////////////////////////////
{
    vector < Info > Bank;
    for( ; ; ) if( !showMenuDoJob( Bank ) ) return 0;
    
} //////////////////////////////////////////////////



void flushStream( istream& is )
{
    while( is.get() != '\n' );
}

int getReply( const string& msg )
{
    cout << msg;
    int reply = cin.get();
    if( reply != '\n' ) flushStream( cin );
    return tolower( reply );
}

bool showMenuDoJob( vector< Info >& b )
{
    cout << HEADER << flush;

    string tmp;
    getline( cin, tmp );
    int choice = atoi( tmp.c_str() );

    switch( choice )
    {
        case 1:
            do { newAcc( b ); } while( getReply( "More (y/n) ? " ) == 'y' );
        break;

        case 2:
            cout << "All your current account balances are ...\n";
            showAll( b );
        break;

        case 3:
            showAll( b );
            cout << "\nWhich account number to edit ? "<< flush;
            getline( cin, tmp );
            choice = atoi( tmp.c_str() );
            editAcc( b, choice-1 );
        break;

        case 4: return false;

        default: cout << "\nNot a valid choice ..." << endl;
    }
    return true;
}

void newAcc( vector< Info >& b )
{
    Info rec;
    cin >> rec;
    b.push_back( rec );
}

void showAll( const vector< Info >& b )
{
    for( size_t i = 0; i < b.size(); ++i )
    {
        cout << "Customer's number " << setw(4) << i+1 << '\n'
             << b[i]
             
             << "\nPress 'Enter' to continue ... " << flush;
        flushStream( cin );
    }
}

void editAcc( vector< Info >& b, int i )
{
    if( 0 <= i  &&  i < int(b.size()) ) // highest valid index is size()-1
    {
        cout << "\nEnter new data ...\n" << flush;
        cin >> b[i];
    }
    else cout << "\nNo Account with this number " << i+1 << endl;
}




And ... using friend functions of the overloaded operators >> and << ... you can see the slight coding changes ... to convert the above struct ... to a class with private data. Recall that declaring functions or classes as a 'friend' to a class gives them direct access to the private data and/or member fuctions of that class ...


// classBankNoGlobalBankVec.cpp

#include <iostream>
#include <fstream> // re. ostream and istream objects ...
#include <string>
#include <vector>
#include <iomanip>
#include <cstdlib> // re. atoi
#include <cctype> // re. tolower ...

using namespace std;

const string HEADER =
    "You have arrived at 'TRUSTWORTHY BANK' that owns all 'your' money.\n"
    "Choose an action from the menu displayed below ...\n\n"
    "Enter 1 to create (a) new account(s)\n"
    "Enter 2 to see ALL current balances IN BANK\n"
    "Enter 3 to edit a current balance\n"
    "Enter 4 to exit\n\n"
    "Your choice : ";

class Info
{
    friend istream& operator >> ( istream& is, Info& rec );
    friend ostream& operator << ( ostream& os, const Info& rec );
public:
    Info () { balance = 0; } // default ctor, all strings are "" default string
private:
    string name;
    string address;
    string city;
    string state;
    string zip;
    string tele;
    double balance;
    string date;
};

// friend function definitions ...
ostream& operator << ( ostream& os, const Info& rec )
{
    int w = 45;
    os << left
       << setw(w) << "Customer's name is " << rec.name << '\n'
       << setw(w) << "Customer's address is " << rec.address << '\n'
       << setw(w) << "Customer's current city of residence is " << rec.city << '\n'
       << setw(w) << "Customer's current state of residence is " << rec.state << '\n'
       << setw(w) << "Customer's zip code is " << rec.zip << '\n'
       << setw(w) << "Customer's current telephone number is " << rec.tele << '\n'
       << setw(w) << "Customer's last date of payment is " << rec.date << '\n'
       << setw(w) << "Customer's last account balance is " << rec.balance
       << endl << right;
    return os;
}

void flushStream( istream& is );
int getReply( const string& msg );
istream& operator >> ( istream& is, Info& rec ) // designed for keyboard entry
{
    do
    {
        cout << "NAME : " << flush;
        getline( is, rec.name );

        cout << "ADDRESS : "<< flush;
        getline( is, rec.address );

        cout << "CITY : "<< flush;
        getline( is, rec.city );

        cout << "STATE : "<< flush;
        getline( is, rec.state );

        cout << "ZIP : "<< flush;
        getline( is, rec.zip );

        cout << "TELEPHONE NUMBER : "<< flush;
        getline( cin, rec.tele );

        cout << "DATE OF LAST PAYMENT : "<< flush;
        getline( is, rec.date );

        for(;;)/>
        {
            cout << "Balance : "<< flush;;
            if( is >> rec.balance )
            {
                flushStream( is );
                break; // good data ... so break right now ...

            }
            // else ...
            cout << "\nERROR! Enter only numeric value for balance ...\n";
            is.clear();
            flushStream( is );
        }

        cout << "\nYou entered:\n"
             << rec;

    }while( getReply( "Ok ... (y/n) ? " ) != 'y' );

    return is;
}

// these functions are needed in main program ...
bool showMenuDoJob( vector< Info >& );
void newAcc( vector< Info >& );
void showAll( const vector< Info >& );
void editAcc( vector< Info >&, int );



int main() /////////////////////////////////////////
{
    vector < Info > Bank;
    for( ; ; ) if( !showMenuDoJob( Bank ) ) return 0;
    
} //////////////////////////////////////////////////



void flushStream( istream& is )
{
    while( is.get() != '\n' );
}

int getReply( const string& msg )
{
    cout << msg;
    int reply = cin.get();
    if( reply != '\n' ) flushStream( cin );
    return tolower( reply );
}

bool showMenuDoJob( vector< Info >& b )
{
    cout << HEADER << flush;

    string tmp;
    getline( cin, tmp );
    int choice = atoi( tmp.c_str() );

    switch( choice )
    {
        case 1:
            do { newAcc( b ); } while( getReply( "More (y/n) ? " ) == 'y' );
        break;

        case 2:
            cout << "All your current account balances are ...\n";
            showAll( b );
        break;

        case 3:
            showAll( b );
            cout << "\nWhich account number to edit ? "<< flush;
            getline( cin, tmp );
            choice = atoi( tmp.c_str() );
            editAcc( b, choice-1 );
        break;

        case 4: return false;

        default: cout << "\nNot a valid choice ..." << endl;
    }
    return true;
}

void newAcc( vector< Info >& b )
{
    Info rec;
    cin >> rec;
    b.push_back( rec );
}

void showAll( const vector< Info >& b )
{
    for( size_t i = 0; i < b.size(); ++i )
    {
        cout << "Customer's number " << setw(4) << i+1 << '\n'
             << b[i]
             
             << "\nPress 'Enter' to continue ... " << flush;
        flushStream( cin );
    }
}

void editAcc( vector< Info >& b, int i )
{
    if( 0 <= i  &&  i < int(b.size()) ) // highest valid index is size()-1
    {
        cout << "\nEnter new data ...\n" << flush;
        cin >> b[i];
    }
    else cout << "\nNo Account with this number " << i+1 << endl;
}


This post has been edited by David W: 24 November 2011 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1