我已经为项目编写了一些基本代码。我正在尝试使用键盘仿真器从RFID阅读器获取输入。以下是我的代码:
#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
}
我正在使用MSVS 2010来编译此代码。使用此代码我无法编译,因为它说未定义E007C02A55EF918D。 E007C02A55EF918D是其中一个RFID标签的序列号,是我想要输入的。我知道我也遇到了getline函数的问题,但我更担心的是将序列号作为输入。
答案 0 :(得分:2)
char
对于单个字符来说足够大(它通常是8位数量,但不依赖于它)。
所以你的product
变量只能容纳一个字符。
E007C02A55EF918D
是一个标识符(因为它以字母开头,不被视为数字,因为它没有被引用,所以它不会被解释为字符串)。
如果您打算product
并且这些序列号为64位数字,则需要将product
更改为足以存储它们(例如uint64_t
),并更改代码中的序列号是0x
前缀的数字。您还必须更改输入法(getline
需要字符串,因此您需要将该字符串转换为数字 - 例如,请参阅How to convert a number to string and vice versa in C++。
if (product == 0xABCD1234)
如果您将两者都缩进为字符串,则使用:
声明product
std::string product;
并引用(""
)序列号。您还需要将最后一个测试更改为:
if (product == "z")
^ ^
您无法将std::string
与单个char
进行比较('z'
是char,"z"
是C样式的0终止字符串)。
答案 1 :(得分:-2)
尝试在""
中使用strcmp()
代替==
,例如
if (!strcmp("E007C02A55EF937C",product))
或
if (strcmp("E007C02A55EF937C",product)==0)
希望它对你有所帮助。