Thread: Making an invoice statement in C++

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Making an invoice statement in C++

    Hey guys,
    I'm kind of in a bit of trouble. I have to make a program in C++ (invoice.exe) that needs to calculate the total list of purchased items. Each purchased item needs to contain the product name, price and the quantity. I can do that, but the next part gets confusing, well atleast for me.

    I have to make a purchase.dat that contains the item information in a plain text file that the program (invoice.exe) can generate info from, and then it needs to provide an output statement (statement.txt). So I think that it will be like invoice.exe < purchase.dat > statement.txt.

    So basically the statement.txt needs to look like:

    Pelikan Invoice
    Product name Quantity Price Cost
    DVD Player 2 24.95 49.90
    LCD TV 1 599 599

    My question is, how do I go into making this program? I know the basics of C++, but this is abit too difficult for me. Any help is appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Create some sort of data stucture to hold your information. Then write a function that collects the information. Then write a function that outputs the information.
    The code's up to you.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2
    This is what I have so far:

    insert
    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
     const int NUMROWS = 5;
     const int NUMCOLS = 4;
     
     int prod, quant;
     int price, cost;
     cost = price * quant;
     
     cout << "\nPhantom Company Invoice" << endl
          << "Product ID \t QUANTITY \t PRICE ($) \t COST ($)" << endl   
    
    
    system ("pause");
    return 0;
    }
    and I have in a txt file saved as purchase.dat

    P010001 24.99 1
    QX-35 19.99 2
    DVD-player5 58.95 1
    P010002 8.85 20

    This is where I'm confused. How do i pipe this purchase.dat into the code? Sorry if the code is messy.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by gtride View Post
    This is where I'm confused. How do i pipe this purchase.dat into the code?
    Because of how you will be starting the program with the redirections, all data written out with cout will go to statement.txt, and all data read from cin will be read from purchase.dat.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Are you familiar with streams? Consider this:
    Code:
    std::ifstream inp("input.data");
    std::ofstream oup("output.txt");
    if( !inp || !oup)   
       {
          std::cerr << "Holy crap!" << std::endl;
          throw error();
       }
    So then you could either
    Code:
    oup << "Invoice version blah blah....." << std::endl;
    while(inp >> data_obj_built_for_streams)
          oup << data_obj_built_for_streams;
    or you could
    Code:
    std::string buffer;
    oup << "Invoice version blah blah....." << std::endl;
    while(std::getline(inp,buffer))
    {
       oup << processing_function_that_returns_a_string(buffer) << '\n';
    }
    
    //of course, your processing function would do the dirty work 
    //of parsing the input and reorganizing it. However, for your
    //model that might not be necessary.
    Or something like that. Hell, you could even do away with the output function altogether
    and just hope the input's alright as is.

    You could also write a class (or set of utilities in a namespace) that keeps track of products with maps and stuctures and functions, so you can do all sorts of fun stuff. But for what you need so far, the above code concepts should suffice.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  2. functions and switch statement
    By rmathus in forum C Programming
    Replies: 2
    Last Post: 11-29-2003, 03:24 PM
  3. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM