reading data from text file

This is a discussion on reading data from text file within the C++ Programming forums, part of the General Programming Boards category; I'm writing this program where I write data to s file and the read it from it. Here is my ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Cool reading data from text file

    I'm writing this program where I write data to s file and the read it from it. Here is my code:
    Code:
    #include<iostream>
    #include<string>
    #include<iomanip>
    #include<fstream>  //for using files
    
    using namespace std;
    
    int main(){
        int hours;
        double bill = 0; //total charge
        char package;
        string name, line;
    
        ofstream output_file;   //to write bill
        ifstream input_file;    //to display bill
    
        cout << "welcome to your bill.\nPlease enter your first name.\n";
        cout << ">>";
        cin >> name;
    
        cout << "select the internet package you have:\n";
        cout << "Pacakage A\nPackage B\nPackage C\n";
        cin >> package;
        package = toupper(package);
    
        cout << "enter the amount of hours you used the service: ";
        cin >> hours;
    
        switch(package){
            case 'A':
                bill = hours <= 10? 9.95 : (9.5 + (2*(hours - 10))); //calculate price
                break;
            case 'B':
                bill = hours <= 20? 14.95 : (14.95 + (1 * (hours - 20)));
                break;
            case 'C':
                bill = 19.95;
                break;
            default :
                        cout<< "you entered an invalid choice";
    
        }
        
    
    
        //write data to file
        
        output_file.open("bill.txt");
        output_file << name << "\n";
        output_file << "you have package " << package << "\n";
        output_file << "you have used " << hours << " hours this month\n";
        output_file <<"your bill is " << bill << " dollars.\n";
        output_file.close();
    
        input_file.open("bill.txt");
        getline(input_file, line, '.');
        cout << line << endl;
    
        input_file.close();
    
    return 0;
    
    }
    the problem I have is that it doesn't read the word "dollars" even though is before the '.'
    sample output
    Code:
    welcome to your bill.
    Please enter your first name.
    >>peter
    select the internet package you have:
    Pacakage A
    Package B
    Package C
    b
    enter the amount of hours you used the service: 5
    peter
    you have package B
    you have used 5 hours this month
    your bill is 14
    Also how can I display the correct amount. When I look at the text file everything seems to be written to it.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Using '.' as a terminating character is probably not a good idea if you have decimal (currency) values in the text.

    ie 'your bill is 14.25 dollars.' will be truncated to 'your bill is 14'

    Try using a different character.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    thanks
    how didn't I see that before?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a text file into a C struct
    By someone2088 in forum C Programming
    Replies: 11
    Last Post: 12-07-2011, 09:14 AM
  2. Reading data from Text File
    By DarrinTE in forum C Programming
    Replies: 8
    Last Post: 03-25-2011, 03:11 PM
  3. Reading Multiple Pieces of Data From Text File
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2009, 07:49 AM
  4. Reading data from a text file
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 02:30 PM
  5. Reading in data from Text file
    By fortune2k in forum C Programming
    Replies: 214
    Last Post: 04-10-2008, 11:12 AM

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