程序因二进制错误而停止

时间:2013-08-05 15:10:14

标签: c++

我被要求为我的推荐课程创建一个汽油泵程序,我遇到了运行它的问题,目前这是编译器在尝试编译代码时输出的主要内容{{3} }

  
    

1> m:\ visual studio 2010 \ projects \ referral \ referral \ main.cpp(56):错误C2678:二进制'>>' :找不到哪个运算符带有'std :: istream'类型的左手操作数(或者没有可接受的转换)

  
#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;
}

我环顾四周,我能找到的唯一解决方案就是包括我已经完成的任何其他解决方案。

任何比我更敏锐的人都要仔细看看并告诉我哪里出错了?

此外,我知道我的代码是一个低效的混乱,我为此道歉。

2 个答案:

答案 0 :(得分:14)

更改

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

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

您需要更改代码中的所有cin >> "string"。这并不意味着提示用户输入。相反,您实际上是在尝试写入字符串文字。

答案 1 :(得分:4)

只是在杨的回答之上添加一些颜色,这不是标题中建议的“二进制错误”。错误消息指的是binary'>>'>>是二进制运算符,二元运算符采用两个操作数,每侧一个。 +-在以下内容中充当二元运算符:

1 + 2
var1 - var2

一元运算符只需一个操作数。 &-在以下内容中作为一元运算符运行:

my_pointer = &n;
int var3 = -5;

您收到的错误消息中的重要部分:

  

二进制'&gt;&gt;' :找不到哪个运算符需要左手操作数   输入'std :: istream'(或没有可接受的转换)

是最后一位,“或者没有可接受的转换”。肯定有一个>>运算符,它接受std::istream的左手操作数,但是没有定义>>运算符,它在右侧使用字符串文字,因为字符串文字可以'被分配到。在这种情况下,std::cin >> myvarstd::cin获取内容并尝试将其放入变量myvar中,但是您无法将任何内容填充到字符串文字中,如"Please enter a pump code:",因为那就像试图做的那样:

"Please enter a pump code:" = 5;

这显然是无稽之谈。