TJL
TJL

Reputation: 21

using getline instead of cin >>

Hey guys so my program is as follows, but when i input a word starting with w,d, b, or q like for example deposit instead of the letter d the program just keeps repeating the initial question of "would you like to withdraw , deposit etc." I tried to convert the cin>> to getline( cin, user_request) but that gave me an error. i am pretty sure the way to fix this is to convert the cin to getline but im not sure how to properly. if anyone can help out. and is there a way to do this program with the cin statements? :

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

int main ()
{
   char user_request;
   string user_string;
   double account_balance, dollars_withdraw, dollars_deposit;

   account_balance = 5000;
   user_request= user_string[0];


   while(account_balance =>0)
     {
        cout << "Would you like to Withdraw, Deposit, Check your balance or Quit?" 
           << endl;
        cin >> user_request;
        if (user_request == 'w' || user_request== 'W')
        {
           cout << "How much would you like to withdraw?" << endl;
           cin >> dollars_withdraw;
        if (dollars_withdraw > account_balance)
           cout << "Invalid transaction" << endl;
        else 
           account_balance = account_balance - dollars_withdraw;
           cout << "Your new balance is $" << account_balance << endl;
        }

        if (user_request == 'd' || user_request== 'D')
        {
           cout << "How much would you like to deposit?" << endl;
           cin >> dollars_deposit;
           account_balance= account_balance + dollars_deposit;
           cout << "Your new balance is $" << account_balance << endl;
        }
        if (user_request == 'b' || user_request == 'B')
      {
           account_balance= account_balance;
           cout << "Your available balance is $" << account_balance << endl;
      }
        if (user_request == 'q' || user_request == 'Q')
           break;
   }

   cout << "Goodbye" << endl;



  return 0;
}

Upvotes: 2

Views: 6053

Answers (7)

nardzkie
nardzkie

Reputation: 11

When you use declare the variable string,then

getline (cin,user_guest); //This is correct.

But when you use the variable char, that's another issue. Your getline will not be applicable. Try to use this:

char user_guest;
cin.get(user_request);

Upvotes: 1

Alan
Alan

Reputation: 46813

Yes std::getline, takes the input stream, and a std::string, or char*

You're trying to pass in a single char, and that's why you're getting an error.

FWIW, you can use getline() with a std::string.

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106068

While user_request is a char, then each non-space character in the input will satisfy another cin >> user_request; statement, so entering "deposit" will loop through 7 times, with only the 'd' matching an if statement, but the prompt message being printed each time.

You should change user_request to be a std::string, then getline(cin, user_request) will work, but you'll need to compare user_request's first character (i.e. [0]) to e.g. "w" (a string literal) instead of 'w' (a character literal).

It's also considered good practice to check whether the input request worked - an end-of-file condition would cause your loop to "spin" forever.

It's also a good idea to change your prompt so it mentions the letters you'd like them to type, otherwise they'd probably type e.g. 'c' rather than 'b' for "check balance". Just cout something like:

Would you like to [W]ithdraw, [D]eposit, Check your [b]alance or [Q]uit?

So, all up:

std::string user_request;

if (not getline(cin, user_request))    // or !getline(...) if your compiler lacks "not"
    break;

if (user_request.empty())
    continue;  // avoid [0] on an empty line, which may crash the program

if (user_request[0] == "w" || user_request[0] == "W")
    ...

Upvotes: 4

templatetypedef
templatetypedef

Reputation: 372694

As mentioned above, cin is a bit finicky when it comes to >> and will enter a "fail state" if it tries to read a value of the wrong type. There are actually many other failure modes as well, and so the preferred means of reading input from the console is to use some combination of getline and stringstreams. If you're interested, Stanford's introductory C++ programming course has an entire chapter on how to do this available online that goes over everything from the basics of cout and file I/O to how to properly and safely get data from the console.

Hope this helps!

Upvotes: 1

andy
andy

Reputation: 538

I would try to replace

cin >> user_request;

with

cin >> user_string;
user_request= user_string[0];

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58667

Either take a std::string from standard in, which will input a word (delimited by whitespace)

std::string input;
if( std::cin >> input ) {
    if( ! input.empty() && input[0] == 'd' || input[0] == 'D' )
        // ...
}

or take an entire line with std::getline():

std::string line;
if( std::getline(std::cin, line) ) {
    if( ! line.empty() && line[0] == 'd' || line[0] == 'D' )
        // ...
}

Of course, in the case of taking a std::string from std::cin if the user enters multiple words then you have the same "problem" as in the case of multiple characters which you mention in your question.

Upvotes: 2

icedtoast
icedtoast

Reputation: 138

you need to retreive the string into a buffer (not a single char).

see:

http://www.cplusplus.com/reference/iostream/istream/getline/

Upvotes: 0

Related Questions