2011-12-04 11 views
0

私はプロジェクトの基本コードを書いています。私は、キーボードエミュレータを使って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タグのシリアル番号で、入力しようとしているものです。私はgetl​​ine関数にも問題があることを知っていますが、シリアル番号を入力として取得することがさらに心配です。

+0

コンパイラはどのタイプが 'E007C02A55EF918D'であるかを知る必要があります。あなたは' char'(製品)をコンパイラにとって未知のものと比較しています。 – Nasreddine

答えて

2

charは、1文字(通常は8ビットの数ですが、それには依存しません)に十分です。

したがって、product変数には1つの文字しか格納できません。

E007C02A55EF918Dは識別子である(文字で始まり、数字と見なされないため、引用符で囲まれていないため、文字列として解釈されません)。あなたは64ビットの数値であることをproductおよびそれらのシリアル番号を意図している場合

、あなたは(たとえば、uint64_t)、それらを格納するのに十分な大きさであることをproductを変更する必要がある、と付けることによって数字であるためにあなたのコード内でシリアル番号を変更します0xである。また、入力方法を変更する必要があります(getlineは文字列を取りますので、その文字列を数値に変換する必要があります - 例えばHow to convert a number to string and vice versa in C++を参照してください)。

if (product == 0xABCD1234) 

あなたが文字列であるために両方をインデントした場合、次いでproductを宣言:

std::string product; 

と引用符("")シリアル番号。 ("z"はCスタイル0で終了する文字列である、charがある'z')あなたは、単一のcharstd::stringを比較することはできません

if (product == "z") 
      ^^ 

:あなたはまたに最後のテストを変更する必要があります。

+0

ありがとうございます。 'if(product ==" E007C02A55EF918D ")を修正し、' product'の変数declerationを 'string product:'に変更しました。私のプログラムは魅力的に機能します。 – ajaustin12

-2

""でそれを持ってみて、

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

または

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

のように、==の代わりにstrcmp()を使用するには、それはあなたを助け願っています。

+1

'strcmp'には1つの引数しかありませんか? – vsz

+0

@kittyPL関数呼び出しでいくつかの引数を指定するとエラーが発生するので使用できません – ajaustin12

+0

if(strcmp( "E007C02A55EF937C"、product)== 0) –