strings

This is a discussion on strings within the C++ Programming forums, part of the General Programming Boards category; Why does the 'getline(cin, ownerName);' and the 'cin >> ownerName;' cause the user to be kicked out of the program. ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    Question strings

    Why does the 'getline(cin, ownerName);' and the 'cin >> ownerName;' cause the user to be kicked out of the program. It continues to print the 'cout' statements but allows no more input.

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

    class SavingsAccount
    {
    int accountType;
    string ownerName;
    long ssn;
    double accountClosurePenaltyPercent, accountBalance;
    public:
    void getValues();
    void printValues();
    inline double calcAcctPen() {return(accountBalance * accountClosurePenaltyPercent);}
    };

    int main()
    {
    SavingsAccount *Account1 = new SavingsAccount;
    Account1->getValues();
    cout << "The amount of the closure penalty is " << Account1->calcAcctPen() << endl;
    Account1->printValues();

    return 0;
    }

    void SavingsAccount::getValues()
    {
    cout << "Enter your account type ( 1 for checking, 2 for savings, 3 for IRA ): ";
    cin >> accountType;
    cout << "\nEnter your name ";
    getline(cin, ownerName); // cin >> ownerName;
    cout << "\nEnter your ss number ";
    cin >> ssn;
    cout << "\nEnter your account balance ";
    cin >> accountBalance;
    cout << "\nEnter the account Closure Penalty Percent ";
    cin >> accountClosurePenaltyPercent;
    cout << endl;

    return;
    }

    void SavingsAccount::printValues()
    {
    cout << "Account type: " << accountType << endl;
    cout << "Name on account: " << ownerName << endl;
    cout << "Social Security Number: " << ssn << endl;
    cout << "Account closure penalty percent: " << accountClosurePenaltyPercent << endl;
    cout << "Account balance: " << accountBalance << endl;

    return;
    }
    Last edited by rc7j; 11-14-2001 at 04:14 PM.
    rc7j

  2. #2
    Unregistered
    Guest
    the getline function and regular cin >> do not cooperate well. one of them leaves extra newline chars in the buffer and the other does not.

    You can read the docs on both of these to find a solution or try a quick fix of calling cin.ignore() after all cin >> statements and that might fix it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21