Space Cowboy
Space Cowboy

Reputation: 33

C++ Programming Error | Undeclared Identifier

So I'm trying to make a simple program and I keep getting an 'undeclared identifier error' with my name, author, price, isbn, quantity, first result, and second result, in the console. I apologize if this type of this has already been asked but I have not been able to fix it.

Here is my program:

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

Upvotes: 1

Views: 874

Answers (1)

Ron
Ron

Reputation: 15501

You are trying to declare multiple local variables of different types by separating them with comma , which is wrong. Use separate statements to declare your variables and apply semicolon ; instead. Semicolon marks the end of an statement:

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);

Those are not function parameters in which case they would be separated by comma. That being said you should use the std::getline when accepting strings from the standard input:

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

Upvotes: 2

Related Questions