CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2014
    Posts
    8

    C++ need help with class

    Hello i am a beginner in c++ and i really need some help with my code. i keep getting 2 errors and im not sure how to fix them...
    1. i keep getting that invoice2 is undeclared
    2. expected ';' before "invoice2" (Each undeclared identifier is reported only once for each function it appears in.)

    here my code

    //.h file

    #include <iostream>
    #include <string>

    using namespace std;

    class invoice{

    private:
    string num;
    string des;
    int quantity;
    int price;

    public:
    void setnum(string);
    string getnum();

    void setdes(string);
    string getdes();

    void setquantity(int);
    int getquantity();

    void setprice(int);
    int getprice();

    invoice();
    invoice(string, string, int, int);


    int getInvoiceAmount();






    };


    //cpp.file

    #include "invoice.h"




    invoice::invoice(string a, string b, int c, int d){
    setnum(a);
    setdes(b);
    setquantity(c);
    setprice(d);
    }


    void invoice::setnum(string a){
    num=a;
    }
    string invoice::getnum(){
    return num;
    }

    void invoice::setdes(string b){
    des=b;
    }
    string invoice::getdes(){
    return des;
    }

    void invoice::setquantity(int c){//loop until a positive number is assigned
    quantity=c;

    while(c<0){

    if (c>=0)
    quantity=c;

    if(c<0){
    cout << "ERROR: Quantity cannot be \nEnter Quantity: ";
    cin>>c;
    quantity=c;
    }
    }
    }


    int invoice::getquantity(){

    return quantity;

    }
    void invoice::setprice(int d){//loop until a positve number is assigned
    price = d;


    while(d<0){

    if (d>=0)
    price=d;

    if(d<0){
    cout << "ERROR: PriceperItem cannot be negative.\nEnter PriceperItem: ";
    cin>>d;
    price=d;
    }
    }
    }
    int invoice::getprice(){

    return price;

    }
    int invoice::getInvoiceAmount(){
    return (getquantity()*getprice());
    }


    //main


    #include "invoice.h"


    int main(){

    // create an Invoice object

    invoice invoice( "12345", "Hammer", 100, 5 );


    // display the invoice data members and calculate the amount
    cout << "Part number: " << invoice.getnum() << endl;
    cout << "Part description: " << invoice.getdes() << endl;
    cout << "Quantity: " << invoice.getquantity() << endl;
    cout << "Price per item: $" << invoice.getprice() << endl;
    cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;

    // modify the invoice data members
    invoice.setnum( "123456" );
    invoice.setdes( "Saw" );
    invoice.setquantity( -5 ); // negative quantity
    invoice.setprice( 10 );
    cout << "\nInvoice data members modified.\n\n";


    // display the modified invoice data members and calculate new amount
    cout << "Part number: " << invoice.getnum() << endl;
    cout << "Part description: " << invoice.getdes() << endl;
    cout << "Quantity: " << invoice.getquantity() << endl;
    cout << "Price per item: $" << invoice.getprice() << endl;
    cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;

    //Next Instance of Invoice Object
    //Both quantity and price are negative

    invoice invoice2("67890", "Drill", -8, -10);
    cout << "\n\nNEXT INVOICE \n\n";

    cout << "Part number: " << invoice2.getnum() << endl;
    cout << "Part description: " << invoice2.getdes() << endl;
    cout << "Quantity: " << invoice2.getquantity() << endl;
    cout << "Price per item: $" << invoice2.getprice() << endl;
    cout << "Invoice amount: $" << invoice2.getInvoiceAmount() << endl;





    return 0;
    }


    any help would be great

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: C++ need help with class

    Quote Originally Posted by dandkem View Post
    Hello i am a beginner in c++ and i really need some help with my code. i keep getting 2 errors and im not sure how to fix them...
    1. i keep getting that invoice2 is undeclared
    Then you have to declare it!
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ need help with class

    When posting code, please use code tags as the code as shown is not easy to read. Go Advanced, select the code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ need help with class

    Having a class invoice and a variable invoice with the same name as the class is not a good idea! I would suggest having the class called Invoice (with a capital I) .
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Oct 2014
    Posts
    8

    Re: C++ need help with class

    Thanks alot it works now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured