getting data from a .txt file! HELF!

This is a discussion on getting data from a .txt file! HELF! within the C++ Programming forums, part of the General Programming Boards category; I'm doing this for a c++ class I have, and I'm stuck as to where to go next. This is ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    getting data from a .txt file! HELF!

    I'm doing this for a c++ class I have, and I'm stuck as to where to go next. This is super easy stuff I'm assuming, i'm just stuck.

    The program is: I have to read this data:

    Box | 250 5750
    Sideline | 100 28000
    Premium | 50 35750
    General | 25 18750

    This is data from seats sold to a game. the first number after the seat type is the price of the ticket, the second is the number sold. I am only telling you this so my code so far will make sense to you.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	ifstream inFile;
    	ofstream outFile;
    	string seatType;
    	double ticketPrice;
    	int ticketsSold;
    	double total;
    	double grandTotal=0.0;
    
    	inFile.open("c:\\tickets.txt");
    	outFile.open("c:\\Atchley_Alex.txt");
    
    	outFile<<fixed << showpoint;
    	outFile<< setprecision(2);
    
    	//gathering data from tickets.txt
    	getline(inFile, seatType, '|');
    	inFile >> ticketPrice >> ticketsSold;
    	
    	
    	//outputting data to Atchley_Alex.txt
    	outFile << setw(10) << seatType << setw(5) << ticketPrice 
                         << setw(5)  << ticketsSold << endl;
    
    	//calculating total ticket sales
    	grandTotal = (ticketsSold+grandTotal);
    
    
    
    
    	system("pause");
    
    
    	inFile.close();
    	outFile.close();
    
    
    
    
    	return 0;
    
    
    }

    Now this might have not been TOTALLY neccissary to copy what I have so far. But! I do not know how to get to the second line of ticket-sales data. It reads the first one, the BOX string and the numbers after it just fine. HOWEVER, I don't know how to get it to go on a head and read the second one. I know i can set up a looping structure to make it repeat what it just did, but I don't know how to make it move on. Thanks for any help.

    sorry if this has been covered in the FAQ, but I'm not sure what this would fall under.
    Last edited by lastwaver0cker; 02-22-2006 at 02:27 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    The extraction operator ( >> ) automatically goes to the beginning of the next line for you.

    What you have to understand is, when your textfile looks like this:

    Box | 250 5750
    Sideline | 100 28000
    Premium | 50 35750
    General | 25 18750

    As far as the computer is concerned, it actually looks like this:

    Box | 250 5750\nSideline | 100 28000\nPremium | 50 35750\nGeneral | 25 18750\n

    That '\n' between each of your lines is a newline character. It's what gets appended to a string when you press the enter key. Think of cin, you enter your data and press enter. It reads up to the enter (newline character) and stops. Then the next input it dumps the leading newline and starts at the next character in the buffer.
    Last edited by SlyMaelstrom; 02-22-2006 at 02:32 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    3
    oooh i think i know what to do now! thanks for your help!

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    3
    okay...my initial theory (copy and pasting the code as much times as neccissary to read all the data) failed.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Code:
    	//gathering data from tickets.txt
    	getline(inFile, seatType, '|');
    	inFile >> ticketPrice >> ticketsSold;
    	
    	
    	//outputting data to Atchley_Alex.txt
    	outFile << setw(10) << seatType << setw(5) << ticketPrice 
                         << setw(5)  << ticketsSold << endl;
    Put all that in a loop until inFile fails.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21