LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2007, 11:23 AM   #1
enth
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Rep: Reputation: 0
cin.getline()function prob


#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;

int menu();

class bank_account{
char name[50];
char account_number[50];
char account_type[10];
int balance_amount;
public:
void init(){name[0]=0;}
void create_account();
void deposit_amount();
void check_account();
void withdraw_amount();
//delete_account();
}account;

int menu(){
int n;
cout<<"Options:\n";
cout<<"1.CREATE ACCOUNT 2.CHECK ACCOUNT STATUS 3.DEPOSIT AMOUNT 4.WITHDRAW AMOUNT 5.DELETE ACCOUNT 6.EXIT\n";
cin>>n;
return n;
}

void bank_account :: create_account(){
cout<<"Enter Account name: ";
cin.getline(name,50);
cout<<endl<<"Enter Account Number: ";
cin.getline(account_number,50);
cout<<endl<<"Enter amount to be deposited: ";
cin>>balance_amount;
}

void bank_account :: check_account(){
char number[50];
cout<<"Enter Account number:";
cin.getline(number,50);
if(!strcmp(number,account_number)){
cout<<"Account Name:"<<name<<endl;
cout<<"Account Number:"<<account_number<<endl;
cout<<"Balance:"<<balance_amount<<endl;
}
else cout<<"Invalid Account number";
}

void bank_account :: deposit_amount(){
int amount;
char number[50];
cout<<"Enter Account number:"<<endl;
cin.getline(number,50);
if(!strcmp(number,account_number)){
cout<<"Enter amount to be deposited:"<<endl;
cin>>amount;
balance_amount+=amount;
}
else cout<<"Invalid Account Number";
}

void bank_account :: withdraw_amount(){
int amount;
char number[50];
cout<<"Enter Account Number:"<<endl;
cin.getline(number,50);
if(!strcmp(number,account_number)){
cout<<"Enter amount to be withdrawn:"<<endl;
cin>>amount;
balance_amount-=amount;
}
else cout<<"Invalid Account Number.";
}

/* ********************************************************** */
int main(){
int n;
cout<<"BANGLADESH BANK"<<endl;
account.init();
for(;{
n=menu();
switch (n){
case 1: { account.create_account(); break;}
case 2: { account.check_account(); break;}
case 3: { account.deposit_amount();break;}
case 4: { account.withdraw_amount();break;}
//case 5: { delete_account();
case 6: exit(1);
}

}
}



when i enter option:1, the program straight goes to the second question which is asking for account number. why does not it ask the first question, which is asking account name? Can anyone please come up with a solution?
 
Old 09-23-2007, 12:45 PM   #2
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
Put ... cin.ignore(INT_MAX, '\n'); ... in the menu as shown below.

There's an explanation of why in this thread:
http://www.thescripts.com/forum/thread129769.html

Code:
int menu(){
    int n;
    cout<<"Options:\n";
    cout<<"1.CREATE ACCOUNT 2.CHECK ACCOUNT STATUS 3.DEPOSIT AMOUNT 4.WITHDRAW AMOUNT 5.DELETE ACCOUNT 6.EXIT\n";
    cin>>n;
    cin.ignore(INT_MAX, '\n');
    return n;
}

Last edited by jozyba; 09-23-2007 at 02:11 PM.
 
Old 09-26-2007, 11:41 AM   #3
enth
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you very much. That was really very helpful.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime error using cin.getline() with array of char AzMan Programming 2 03-26-2007 10:26 AM
getline hylke Programming 9 06-03-2004 01:24 PM
C++ | cin.getline question... Mega Man X Programming 7 01-08-2004 10:34 AM
g++ and cin.getline() codeviking Programming 8 07-03-2003 07:42 AM
cin.getline adam_boz Programming 6 10-02-2002 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration