4

This is my first attempt at C++, following an example to calculate a tip through a console application. The full (working code) is shown below:

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

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

int _tmain(int argc, _TCHAR* argv[])
{
    // Declare variables
    double totalBill = 0.0;
    double liquour = 0.0;
    double tipPercentage = 0.0;
    double totalNoLiquour = 0.0;
    double tip = 0.0;
    string hadLiquour;

    // Capture inputs
    cout << "Did you drink any booze? (Yes or No)\t";
    getline(cin, hadLiquour, '\n');

    if(hadLiquour == "Yes") {
        cout << "Please enter you booze bill\t";
        cin >> liquour;
    }

    cout << "Please enter your total bill\t";
    cin >> totalBill;

    cout << "Enter the tip percentage (in decimal form)\t";
    cin >> tipPercentage;

    // Process inputs
    totalNoLiquour = totalBill - liquour;
    tip = totalNoLiquour * tipPercentage;

    // Output
    cout << "Tip: " << (char)156 << tip << endl;
    system("pause");

    return 0;
}

This works fine. However, I want to move:

cout << "Please enter your total bill\t";
cin >> totalBill;

to be the first line under:

 // Capture inputs

But when I do the application breaks (it compiles, but just ignores the if statement and then prints both cout's at once.

Im scratching my head becuase I cant understand what's going on - but I'm assuming I'm being an idiot!

Thanks

rwb
  • 4,309
  • 8
  • 36
  • 59
  • 1
    Of course you are not an idiot:) I suspect that there is a newline at the end of hadLiquour and that's why it doesn't match. Try to investigate with debuggger, or just cout the value to the screen. If so you can rework this to compare only the first 3 characters, but I won't ruin your fun how to make it. – Tsvetomir Dimitrov May 11 '12 at 08:24
  • Print out `hadLiquour` and see what it contains this will give you some indication as to what is going wrong. – MoonKnight May 11 '12 at 08:28
  • 2
    Try not to mix **getline()** and **cin** up because it may cause problem as described [here](http://www.cplusplus.com/forum/articles/6046/) – Imran Rana May 11 '12 at 08:39
  • You know, that you can also do `cin >> hadLiquour`? – AquilaRapax May 11 '12 at 08:41

3 Answers3

6

Try this

    // Capture inputs
cout << "Please enter your total bill\t";
cin >> totalBill;
cin.clear();
cin.sync();

See c++ getline() isn't waiting for input from console when called multiple times

Or, better yet don't use getline at all:

cout << "Please enter your total bill\t";
cin >> totalBill;

cout << "Did you drink any booze? (Yes or No)\t";
cin >> hadLiquour;
Community
  • 1
  • 1
acraig5075
  • 10,588
  • 3
  • 31
  • 50
1

totalBill is a number, i.e. the program "consumes" everything from your input that is a number. Let's say you entered:

42.2[RETURN]

The 42.2 gets copied into totalBill. The [RETURN] doesn't match, and remains in the input buffer.

Now, when you call getline(), the [RETURN] is still sitting there... I am sure you can figure out the rest from there.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

Cin doesn't remove the newline character from the stream or do type-checking. So using cin>>var; and following it up with another cin >> stringtype; or getline(); will receive empty inputs. It's best practice to NOT MIX the different types of input methods from cin.

[for more informations see link]

you may change your code as below :

cout << "Please enter your total bill\t";
getline(cin, hadLiquour);          // i used the hadLiquour string var as a temp var 
                                   // so don't  be confused
stringstream myStream(hadLiquour);
myStream >> totalBill;
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43