Thread: arrays and piping and frustration

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    arrays and piping and frustration

    Hi I'm studying c++ at university (introduction to it) and i got this assignment, i don't expect the the answer to be given to me straight out, and I've read the sticky's.

    i have this file: datafile.txt
    contents of it:

    /rawprices
    P010001 24.99
    QX-35 19.99
    DVD-player5 58.95
    P010002 8.85
    P010003 35.00

    /items
    P010001 2
    DVD-player5 1
    P010002 20

    /invoice
    /quit





    # /rawprices: this indicates that the data records to follow, if any, will be for the product pricing in the form of

    productId price

    # /items: this indicates that the data records to follow, if any, will be for the purchased items in the form of

    productId quantity

    # /invoice: this generates the invoice on the purchased items already read into the program

    # /quit: this terminates the program


    my code so far before i break my keyboard

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
     
     
    int main() {
    
    
    
    
    
    
    
        
    system("pause");
    return 0;
    }
    im not a c++ genious i understand it at a very basic level, im just struggling to figure out how to put the contents of the txt file in array. if anyone can help me pls do. if this post is against the rules then just delete it sry.

    ty in advance if anyoen can help

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    9
    ty reading up on that now

  4. #4
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    this is a a lot of work to do and since you are c++ newbie it will be a lot harder for you just do no get frustrated and do not hatec++ otherwise you will be doomed at the start all the bessssst

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    9
    i got 1 week to learn how to do this, i need to develop a program that will calculate the total price of what i want to buy (the information is all in the text file). been nearly 7 hours and i haven't gotten anywhere

  6. #6
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    okay if you can come up with a pseudo code then we will help to convert that to c++ syntax should be fair enough no rules broken okay? post a step-by-step algorithmic description of what you would do to solve this

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    i got 1 week to learn how to do this, i need to develop a program that will calculate the total price of what i want to buy (the information is all in the text file). been nearly 7 hours and i haven't gotten anywhere
    Read a good C++ tutorial from beginning to finish (for example, the one on this site
    http://www.cprogramming.com/tutorial.html)
    read the "Learning to Program in C++" section, from beginning to "Typecasting". Those should be the very basic. Then, read the rest as you need/encounter them. Those are short lessons, and should not take you more than a few hours. After understanding those, your question at hand will seem obvious. If not, ask then.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    9
    Code:
    BEGIN PointOfSaleTeminal
    	READ ProductID, Price
    	DOWHILE more products exist
    		STORE ProductID, Price
    	ENDDO
    
    	READ ProductID, Quantity
    	DOWHILE more products exist
    		STORE ProductID, Quantity
    	ENDDO
    
    	IF ProductID match then
    		STORE ProductID, Price, Quantity
    	ELSE
    		DELETE ProductID, Price
    	ENDIF
    
    	Cost = Quantity * Price
    
    	PRINT ProductID, Quantity, Price, Cost
    	
    	Total = 0
    
    	DOWHILE more Cost exist
    		Total = Cost + Total
    	ENDDO
    
    	PRINT Total
    
    END
    thats wrong isn't it lol

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think it's that wrong, but I don't have a clear goal of your aim either...
    And don't hate C++. The language is great, it's just somewhat newbie unfriendly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    9
    well heres the question but im only posting this coz none of u seem to understand me lol.

    You are required to design a program post.cpp (point of sale terminal) that will be used to calculate the total cost for a list of purchased items against a list of product prices. Each purchased item will be presented by its productId, the price per item, and the corresponding quantity. The program is expected to be interactive and the user can enter everything line by line and generate such as an invoice at the end. We note that such an application usage can be alternatively illustrated by piping all the data to the program. Suppose all the program control commands and the data for the products and purchased items are stored in a plain text file datafile.txt in the form similar to the following sample
    Code:
    /rawprices
    P010001		24.99	
    QX-35		19.99	
    DVD-player5	58.95	
    P010002		 8.85	
    P010003		35.00
    
    /items
    P010001		2
    DVD-player5	1
    P010002		20
    
    /invoice
    /quit
    where in a typical data line (blank lines are ignored), the first column represents the product ID which will be alphanumeric without white spaces, the second column represents the product price in Australian dollars when inputing product prices, and will represent the quantity of the corresponding purchased item when inputing purchased items. Your program post.exe compiled from post.cpp will then be able to generate an invoice via
    Code:
    post.exe < datafile.txt > invoice.txt
    whose output invoice.txt could look like
    Code:
    PHANTOM COMPANY INVOICE
    
    PRODUCT ID	QUANTITY  PRICE ($)    COST ($)
    P010001		   2	     24.99	 49.98
    DVD-player5	   1	     58.95	 58.95
    P010002		  20	      8.85	177.00
    ...
    			TOTAL COST = $xxxxx.xx
    When the post program is executed, it will process commands and data records entered by the user. If a word read into the program starts with the character "/", possibly when the programming is attempting to read in a product ID, then the word is deemed a program command. This program should support at least the following commands

    * /rawprices: this indicates that the data records to follow, if any, will be for the product pricing in the form of

    productId price

    * /items: this indicates that the data records to follow, if any, will be for the purchased items in the form of

    productId quantity

    * /invoice: this generates the invoice on the purchased items already read into the program
    * /quit: this terminates the program

    and any other words that start with the character '/' will be considered an invalid command. For the convenience and uniformity of the program design, we assume that the total number of different products will not exceed 2000, and the total number of purchased items for each invoice will not exceed 1000.

    The program should be designed in such a way that piping can also be used to properly generate an invoice output as in the example at the beginning of this document. Any explicit use of file inside the C++ program for this part will not be considered a proper or complete solution in this particular assessment. We note that if you are using prompts for interactive data input, you need to make sure that the prompts are sent to standard error device using cerr (instead of cout), i.e. using cerr in place of cout. This way, the prompts will not be piped into the output file, say, statement.txt.

  11. #11
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    int main() {
    	string tag;
    	ifstream dataFile("data.txt");
    	getline(dataFile, tag);
    	
    	if (tag == "/rawprices") {
    		readPrices(dataFile);
    	} else if (tag == "/items" ) {
    		readItems(dataFile);
    	} else {
    		// ...
    	}
    }
    that should give some startup ideas. you want full code? come over to rentadcoder.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Start by writing a program which simply copies the input file to the output file. Until that works, the rest of the problem is inaccessible.

    Then perhaps add some code which recognises say lines beginning with "/", and just prints out "Found a /". Enhance that by extracting and printing whatever else is important on that kind of line.

    Just keep adding and testing as you go, and resist the temptation to add too much code in one step.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    9
    Quote Originally Posted by (::) View Post
    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    int main() {
    	string tag;
    	ifstream dataFile("data.txt");
    	getline(dataFile, tag);
    	
    	if (tag == "/rawprices") {
    		readPrices(dataFile);
    	} else if (tag == "/items" ) {
    		readItems(dataFile);
    	} else {
    		// ...
    	}
    }
    that should give some startup ideas. you want full code? come over to rentadcoder.
    lol its my uni assignment im supposed to do it to learn how its done + i dont even have enough cash to rent a coder rofl

    Quote Originally Posted by Salem View Post
    Start by writing a program which simply copies the input file to the output file. Until that works, the rest of the problem is inaccessible.

    Then perhaps add some code which recognises say lines beginning with "/", and just prints out "Found a /". Enhance that by extracting and printing whatever else is important on that kind of line.

    Just keep adding and testing as you go, and resist the temptation to add too much code in one step.
    ehh ill post up some code tomorrow i kinda got it working but i wana ask my uni professor to help me out a bit and then ill show u all what i have and if anyone has any suggestions then feel free to modify my code XD


    by the way, thanks to EVERYONE for everything tyvm!

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    9
    ok after long hard work with teacher i got this so far

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    //#include <
    using namespace std;
    
    string readprices(string products[], double prices[], int &total){
    
     string id;
                             double cost;
                             do {
                               cerr << "enter id, price: ";
                               cin >> id;
                               if(id[0]!= '/') {
                                          cin >> cost ;
                                          products[total] = id;
                                          prices[total] = cost;
                                          total++;
                                          }
                                        
                                    }while (id[0]!='/');
                                    
                             return id;
    
    }
    
    
    
    void display(string products[], double prices[], int total){
         int x = 0;
         
         for (x = 0; x < total; x++) {
         cout << products[x] << " " << prices[x] << endl;
         }
         cout << "total records: " << total << endl;
         }
    
    
    
    int main()
    {
    string products[100];
    double prices[100];
    int total=0;
    
    string command;
    
    while (command!="/quit") {
    command = readprices (products, prices, total);
    cout << command << endl;
    
         display(products, prices, total);
    
    }
    
    system("pause");
    return 0;
    }
    pretty sure im geting the idea now just gota figure out how to make it into c++ code

Popular pages Recent additions subscribe to a feed