0

I have wrote some basic code for a project. I am at the point I am trying to take an input from a RFID reader using a keyboard emulator. The follwing is my code to this point:

    #include <iostream>
#include <ios>
#include <iomanip>
#include <cmath>

using namespace std;


int main()
{
    char product; //declaring the variable for the switch/case
    int Pay = 0; //Declaring the variable Pay
    int Payment = 0;
    double Total = 0; // Declaring the Final Total variable
    double Subtotal = 0; // Declaring the variable Subtotal
    double Tax = 0; // Declaring the variable Tax
    int m = 0; //counts the amount of times milk is scanned
    int b = 0; //counts the amount of times beer is scanned
    int c = 0; //counts the amount of times candy bar is scanned
    int r = 0; //counts the amount of times rice is scanned


    cout << "Scan the product you desire to purchase: \n";//Asking user to input product purchasing
    cout << "When ready to checkout press the z button.\n\n\n"; //Telling user to press button z to pay

    while(Pay < 1) //Keeps in the loop until pay is increased to 1
    {
        getline(cin, product); //Taking input and assining to the variable product


            if(product == E007C02A55EF918D) 
            {
                cout << "6 pack of Budlight...........$6.49\n"; // If the button b is pushed displays
                Subtotal = Subtotal + Beer; // Calculates the Subtotal and stores it
                Tax = Beer * Taxrate + Tax; // Claculates the total Tax and stores it
                b++;
            }

            else if(product == E007C02A55EF937C)
            {
                cout << "Snickers Bar.................$0.99\n";// If the button c is pusehd displays
                Subtotal = Subtotal + Candy_Bar;
                Tax = Candy_Bar * Taxrate + Tax;
                c++;
            }

            else if(product == E007C02A554A7A8B)
            {
                cout << "1 Gallon of 2% Milk..........$3.99\n";//If the button m is pushed displays
                Subtotal = Subtotal + Milk;
                m++;
            }

            else if(product == E007C02A55CE0766)
            {
                cout << "Box of Brown Rice............$2.79\n";//If the button r is pushed displays
                Subtotal = Subtotal + Rice;
                r++;
            }

            else
            cout << "Invaild product. Please scan a different product.\n";

            if (product == 'z')
            Pay++; //When finished it increases pay to 1 to break the while loop


        Total = Subtotal + Tax; // Claculates the Total

    }

I am using MSVS 2010 to compile this code. With this code I can not compile because it says E007C02A55EF918D is undefined. E007C02A55EF918D is the serial number from one of the RFID tags and is what I am trying to input. I know I am having problems with the getline function also, but I am more worried about getting the serial number as an input.

ajaustin12
  • 111
  • 1
  • 3
  • 10
  • The compiler needs to know what type is `E007C02A55EF918D`, you are comparing a `char` (product) to something unknown to the compiler. – Nasreddine Dec 04 '11 at 08:30

2 Answers2

2

char is big enough for a single character (it is usually an 8bit quantity, but don't rely on that).

So your product variable can only hold one char.

E007C02A55EF918D is an identifier (because it begins with a letter, it is not considered as a number, and because it is not quoted, it is not interpreted as a string).

If you intended product and those serial numbers to be 64bit numbers, you'll need to change product to be large enough to store them (uint64_t for instance), and change the serial numbers in your code to be numbers by prefixing with 0x. You'll also have to change your input method (getline takes strings, so you will need to convert that string to a number - see How to convert a number to string and vice versa in C++ for instance).

if (product == 0xABCD1234)

If you indented both to be strings, then declare product with:

std::string product;

and quote ("") the serial numbers. You'll also need to change the last test to:

if (product == "z")
               ^ ^

You can't compare an std::string with a single char ('z' is a char, "z" is a C-style 0-terminated string).

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • Thank you for your help. I fixed the `if(product == "E007C02A55EF918D")` and I changed the variable decleration of `product` to `string product:` and my program works like a charm. – ajaustin12 Dec 04 '11 at 09:26
-2

Try having it in "" and use strcmp() instead of ==, like

if (!strcmp("E007C02A55EF937C",product))

or

if (strcmp("E007C02A55EF937C",product)==0)

Hope it helped you.

noisy cat
  • 2,865
  • 5
  • 33
  • 51