2017-09-08 11 views
0

私は単純なプログラムを作成しようとしていますが、の「宣言されていない識別子のエラー」が私の名前、著者、価格、isbn、数量、最初の結果、私はこのタイプのこれがすでに頼まれているが、私はそれを修正することができていない場合はお詫び申し上げます。あなたは間違っているコンマ,で区切ることにより、異なる種類の複数のローカル変数を宣言しようとしているC++プログラミングエラー|宣言されていない識別子

#include <iostream> 
#include <string> 
using namespace std; 

int main() 

{ 
    string name, author, 
     double isbn, 
     float price, 
     int quantity, 
     float firstresult, 
     float secondresult, 
     const float tax (0.07); 

    cout << "What is the name of the book?"; 
    cin >> name; 
    cout << "What is the authors name?"; 
    cin >> author; 
    cout << "What is the ISBN number?"; 
    cin >> isbn; 
    cout << "What is the price?"; 
    cin >> price; 
    cout << "How many books did you purchase?"; 
    cin >> quantity; 

    firstresult = price*tax; 
    secondresult = price + firstresult; 

    if (quantity > 5) { 
     secondresult += (quantity - 5) * 2.00; 
    } 

    cout << "------------------------" << endl; 
    cout << "Invoice of Order:" << endl; 
    cout << name << endl; 
    cout << author << endl; 
    cout << isbn << endl; 
    cout << price << endl; 
    cout << quantity << endl; 
    cout << "Total Cost: " << secondresult << endl; 
    cout << "------------------------" << endl; 

    return 0; 
} 
+1

同じ行に異なるタイプの変数を定義することはできません。セミコロンが必要です。すべての変数を宣言しているメインの一番上にある各行の最後にカンマではありません。 clangからのエラーメッセージは、問題の内容を明確にしています。https://godbolt.org/g/CTefhu – xaxxon

+0

'std :: string'の入力には' std :: getline(std :: cin、stringVariable);を使用してください。 'string'変数を空文字列' variable = "" 'に定義する必要があります。 –

答えて

2

は、ここに私のプログラムです。別のステートメントを使用して変数を宣言し、代わりにセミコロン;を適用します。セミコロンは文の終わりを示します。

string name, author; // you are defining multiple variables of the same type which is fine 
double isbn; 
float price; 
int quantity; 
float firstresult; 
float secondresult; 
const float tax (0.07); 

これらは、カンマで区切られた関数パラメータではありません。標準入力から文字列を受け取るときにstd::getlineを使うべきだと言われています。

std::getline(std::cin, author); 
+0

ありがとうロン、本当にありがとう! –

関連する問題