Hi there,

I am using code blocks and working on bank account programe. I have three classes but just included main function code. My programe works all fine, in which user create account and then deposite initial balance but before end of do while loop, there is getline function to ask user to repeat programe or not. My programe skips that getline function input and continue to rest of programe.

Anybody please help ! and plz tell me why is this happening. I am working on this program from many days.

#include <iostream>
#include <string>
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"

using namespace std;

int main()
{
    string input, acc, repeat;
    //string aName;   //account name

    bool rep = false;   //skip the do you want to create acc msg
    //boolean check whether an account is created or not.
    bool bankA = false;
    bool checkingA = false;
    bool savingsA = false;

    //objects
    BankAccount b;
    CheckingAccount c;
    SavingsAccount s(10.0);

//start of do while loop
start:do{

        if(rep == false)
        {
            cout <<"Do you want to create an account (y/n) ??"<<endl;
            getline(cin, input);
        }

        if(input == "y" || input == "Y")
        {
            if(acc == "b")
            {
                bankA = true;
                cout << "\nBank Account has been created !\n" <<endl;
                b.deposite("Bank Account", true);

                //cout <<"repeat value : " <<repeat <<endl;

            }
            else if(acc == "c")
            {
                cout << "\nChecking Account has been created !\n" <<endl;
                c.deposite("Checking Account", true);
                checkingA = true;
            }
            else if(acc == "s")
            {
                cout << "\nSavings Account has been created !\n" <<endl;
                s.deposite("Savings Account", true);
                savingsA = true;
            }
            else
            {
                cout <<"\nNo such type of account !\n"<<endl;
            }
        }
        else if(input == "n" || input == "N")
        {
            break;
        }
        else
        {
            cout <<"\nInvalid input !\n"<<endl;
            goto start;
        }


            cout <<"Repeat (y/n) ??"<<endl;     //this message is displaying but
            getline(cin, repeat);               //does not getting input



        if(repeat == "y")
            rep = true;

    }while((input != "y") || repeat == "y");

    cout <<"\n\nPrograme is terminated !"<<endl;
    return 0;
}

Recommended Answers

All 6 Replies

Do your deposit functions take a value directly into a numeric variable, like:

cin >> dep_amount;

If so, then there's a newline left on the input stream that halts your getline without any new input to the loop variable.

Thanks for your quick reply. But I didn't understand. This is my deposite function. After this my programe does not take input from getline function.

void BankAccount::deposite(std::string aName, bool initial)
{
    double dep;

    do{
            if(!initial)
            {
                cout << "Enter amount you want to deposite in "<<aName <<" :" <<endl;
                cin >> dep;
            }
            else
            {
                cout << "Specify initial amount you want to deposite in "<<aName <<" :" <<endl;
                cin >> dep;
            }

            if(dep > 0)
            {
                balance += dep;
                cout <<"\nYour amount of "<<dep <<" Rs has been deposited to "<<aName <<"."<<endl;
                tr++;
            }
            else
                cout <<"\nAmount in "<<aName <<" can't be less than or equal to zero."<<endl;

        }while(dep <= 0);
}

There's a sticky post at the top of the C++ forum on "flushing the input stream", it should help you understand what's going on here.

On another note, I don't understand how you don't just see the "No such type of account" message every time you run the program. You have a series of if... else statements checking the value of acc, but it is never assigned a value, so the program should always go into the final else statement of this logic.

Additionally, you shouldn't use goto in this context (line 69 of your original post). You can use the continue keyword to achieve the same thing. continue just says to the program "go back to the start of this loop", which is what you want to do:

do {

    // some code...

    if ( ! someCondition )
        continue;   // Go back to the start of the loop

    // Some other code that only gets executed if someCondition == true...

} while( condition );

You also don't need to logic around the rep variable that you have. This is equivalent to what you have:

do {
    // Don't need the check of rep here
    cout <<"Do you want to create an account (y/n) ??"<<endl;
    getline(cin, input);

    // Some code

    cout <<"Repeat (y/n) ??"<<endl;     //this message is displaying but
    getline(cin, repeat);               //does not getting input

    // Don't need to do this, it's checked in the while condition below
    // if(repeat == "y")
    //     rep = true;

} while( repeat == "y" );

I have found the solution in other websites that we have to clear the buffer and use cin.ingnore(). Does anyone tell me what does it means ??

And also what actually getline() does ? Actually I didn't understand these technical terms. Thanks !

Thanks for your reply ravenous.

I have found the solution in other websites that we have to clear the buffer and use cin.ingnore(). Does anyone tell me what does it means ??

cin.ignore() reads and discards characters. cin.ignore(80, '\n') is roughly equivalent to this loop:

{
    char ch;

    for (int i = 0; i < 80; i++) {
        if (!cin.get(ch) || ch == '\n')
            break;
    }
}

"Clearing the buffer" means removing pending characters in a line. The reason a line is important is because input from std::cin is nearly always line oriented. You can safely assume that by discarding characters up to and including a newline, that basically ensures that the next character request will force the user to type something.

And also what actually getline() does ?

Really all it does is read characters from the stream up to end-of-file or the specified delimiter and appends them onto the supplied string. A greatly simplified facsimile might look like this:

istream& mygetline(istream& in, string& s, const char delim)
{
    char ch;

    s.clear();

    while (in.get(ch) && ch != delim) {
        if (s.size() == s.max_size()) {
            in.setstate(ios::failbit);
            break;
        }

        s.push_back(ch);
    }

    return in;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.