2

I have been asked to create a petrol pump program for my referral coursework and I'm having an issue with running it, at the moment this is the main thing the compiler is outputting while trying to compile the code full build dialogue here

1>m:\visual studio 2010\projects\referral\referral\main.cpp(56): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <string>
#include <Windows.h>

using namespace std;
int reciept();
int pump;
int petrol;

int main()
{
    bool exit = false;
    int code;
    string p1w ("Waiting");
    string p2w ("Waiting");
    string p3w ("Waiting");
    string p4w ("Waiting");
    string p1r ("Ready");
    string p2r ("Ready");
    string p3r ("Ready");
    string p4r ("Ready");

    if (GetAsyncKeyState(VK_ESCAPE))
    {
        exit = true;
    }

    cout << "***************************************************" << endl;
    cout << "*Liquid Gold v1.0.0 Last revised 18/07/13         *" << endl;
    cout << "*The process of processing transactions is simple,*" << endl;
    cout << "*activate a pump by entering its code shown below.*" << endl;
    cout << "*After pump operation is verified (generally 10   *" << endl;
    cout << "*seconds though this may vary) the attendant      *" << endl;
    cout << "* will be able to enter the amount of petrol to 3 *" << endl;
    cout << "*decimal places which will then be converted based*" << endl;
    cout << "*on a predetermined price (which can be altered in*" << endl;
    cout << "*price.txt) and once this process is complete a   *" << endl;
    cout << "*receipt will be created (you will need seperate  *" << endl;
    cout << "*software in order to print this recipt) and the  *" << endl;
    cout << "*transaction will be terminated.                  *" << endl;
    cout << "*© Simple Software Solutions 2013                 *" << endl;
    cout << "***************************************************" << endl << endl;
    system("Pause");

    while (exit == false)
    {

        cout << "       Pump (1) - " << p1w << "        Pump (2) - " << p2w << endl << endl << endl;
        cout << "       Pump (3) - " << p3w << "        Pump (4) - " << p4w << endl << endl << endl;

        cin >> "Please enter a pump code:" >> code;

        if (code == 1)
        {
            swap (p1w, p1r);
            pump = 1;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        } 

        else if (code == 2)
        {
            swap (p2w, p2r);
            pump = 2;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        }

        else if (code == 3)
        {
            swap (p3w, p3r);
            pump = 3;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        }

        else if (code == 4)
        {
            swap (p4w, p4r);
            pump = 4;
            cin >> "Please enter the amount of petrol deposited" >> petrol;
            break;
        }
        else
        {
            cout << "Invalid pump code entered";

        }
        reciept();
        {
             ofstream transactions;
             transactions.open ("reciept.txt");
             transactions << "****************************************************/n";
             transactions << "                        SALE                        /n";
             transactions << "****************************************************/n /n";
        }
    }

    return 0;
}

I've looked around and the only solution I can find for that error is including which I've already done and I can't see any other solution.

Anyone more observant than me care to have a look through and tell me where I'm going wrong?

Also I know my code is an inefficient mess and I apologise for that.

2 Answers2

14

Change

cin >> "Please enter a pump code:" >> code;

to

cout << "Please enter a pump code: ";
cin >> code;

You need to change all the cin >> "string" in your code. This does not mean prompting user for input. Instead, you're actually trying to write into a string literal.

Yang
  • 7,712
  • 9
  • 48
  • 65
  • You should also flush `cout` before reading `cin`. Otherwise, the user may not see the message before expecting input. – Nathan Ernst Aug 05 '13 at 18:07
  • 1
    @NathanErnst Thanks for your comment. We don't need explicit flush in this case. When reading from an std::istream the "tied" std::ostream, if any, is flushed. http://stackoverflow.com/questions/9274057/c-cout-and-cin-buffers-and-buffers-in-general/9275346#9275346 – Yang Aug 05 '13 at 18:40
4

Just for some added color on top of Yang's answer, this is not a "binary error" as suggested in the title. The error message refers to binary'>>'. >> is a binary operator, and binary operators take two operands, one on each side. + and - are functioning as binary operators in the following:

1 + 2
var1 - var2

A unary operator takes just one operand. & and - are functioning as unary operators in the following:

my_pointer = &n;
int var3 = -5;

The important part in the error message you're getting:

binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

is the last bit, "or there is no acceptable conversion". There certainly is a >> operator which takes a left hand operand of std::istream, but there is no >> operator defined which takes a string literal on the right hand side, since string literals can't be assigned to. In this case, std::cin >> myvar takes stuff from std::cin and tries to put it into the variable myvar, but there's no way you can stuff anything into a string literal like "Please enter a pump code:", as that would be like trying to do:

"Please enter a pump code:" = 5;

which is obviously nonsense.

Crowman
  • 25,242
  • 5
  • 48
  • 56