Writing to file on each input

This is a discussion on Writing to file on each input within the C++ Programming forums, part of the General Programming Boards category; Hi I'm writing a program for a school project for generating Pay Slips. Here's the code Code: #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Writing to file on each input

    Hi

    I'm writing a program for a school project for generating Pay Slips. Here's the code

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main(void)
    {
    char name[30];
    char surname[30];
    char id[30];
    double hourrate;
    char hourratechar[30];
    int numhour;
    char numhourchar[10];
    char taxcredit[10];
    float taxoutput;
    float basicpay;
    float overtime;
    float grosspay;
    float natins;
    
    cout <<"Please enter your name"<<endl;
    do
    {
    cin.getline(name,40);
     if(strlen(name) > 30)
      {
           cout<<"Error please enter sufficient characters"<<endl;
      }
      }while(strlen(name) > 30);
    
    cout <<"Please enter your surname"<<endl;
    do
    {
    cin.getline(surname,40);
    if(strlen(surname) > 30)
      {
           cout<<"Error please enter sufficient characters"<<endl;
      }
      }while(strlen(surname) > 30);
    
    cout <<"Please enter your ID"<<endl;
    do
    {
    cin.getline(id,15);
    if(strlen(id) > 5)
      {
           cout<<"Error please enter 5 numbers or less"<<endl;
      }
      }while(strlen(id) > 5);
    
    cout <<"Please enter your hourly rate"<<endl;
    cin.getline(hourratechar,30);
    hourrate= atof(hourratechar);
    
    do
    {
    cout <<"Please enter the number of hours you work"<<endl;
    cin.getline(numhourchar,30);
    numhour= atoi(numhourchar);
    if(numhour > 60)
            {
            cout<<"Error please enter 60 hours or less"<<endl;
            }    
     }while(numhour > 60);
    
        if (numhour<=39)
    {
       basicpay=numhour*hourrate;
    }
    else 
    {
       basicpay=39*hourrate;
    }
    
        if (overtime <= 39){
      overtime = 0;
      }
    else 
         if (overtime > 39) {
           overtime=numhour*hourrate*1.5;
           }
                  
    grosspay=basicpay+overtime;
    
    cout <<"Please enter whether you get tax credits or not"<<endl;
    cin.getline(taxcredit,10);
        if (taxcredit[0]== 'y')
        {
            taxoutput=grosspay*0.22;
        }
       else
        {
            taxoutput=grosspay*0.20;
        }
    
    natins=grosspay*0.11;
    
    cout <<""<<endl;
    cout <<"The employee entered the following information:"<<endl;
    cout <<"Their name" <<endl<<name; 
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their surname" <<endl<<surname; 
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their ID" <<endl<<id;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their Hour Rate" <<endl<<hourrate; 
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their Working Hours" <<endl<<numhour; 
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their Basic Pay" <<endl<<basicpay;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their Overtime Pay" <<endl<<overtime;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their Gross Pay" <<endl<<grosspay;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"Their Tax Credits" <<endl<<taxoutput;
    cout <<""<<endl;
    cout <<""<<endl;
    cout <<"National Insurance" <<endl<<natins;
    cout <<""<<endl;
    cout <<""<<endl;
    
    system("pause");
    }
    I was just wondering on all the cin.getline inputs how would you go about writing it to a basic text file. I've found the basic info but I can't seem to put it to good use.

    Cheers

    Tom Evans

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Writing what to a text file?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Hi

    Sorry if I was not clear

    for example:

    the line

    Code:
    cin.getline(name,40);
    when you input a name how would you write it to a text file

    Cheers

    Tom Evans

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I still don't get it. Do you want the program to read the input from a text file you've previously written, or do you want the program to echo all input to a text file?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Hi

    Sorry for all this hassle

    For example when I type in

    Name : Joe
    Surname : Bloggs
    ID : 84744
    Num of Hours Worked - 33

    and the rest of the inputs it comes up with a list of outputs such as gross pay, net pay, national insurance which have been worked out from the inputs.

    I would like all of the outputs to be saved to a text file to be viewed at a later date.

    I hope this explains it a bit better

    Apologies that this differs from my original question

    I have uploaded the program for to you look at

    http://rapidshare.de/files/11568782/...rator.exe.html

    Tom Evans
    Last edited by gold2040; 01-22-2006 at 07:03 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It's very simple. Just create an ofstream object:
    Code:
    ofstream outfile("result.txt");
    Then replace the cout in all output statements with outfile. (Or duplicate the output statements, once with cout and once with outfile, if you want to display it immediately AND write it to a file.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Cheers buddy

    1 more thing

    When you input some different details how can you make it so that it doesn't overwrite the orginal details and is just added to the file along with the other details

    Tom Evans
    Last edited by gold2040; 01-22-2006 at 07:26 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    ofstream outfile("result.txt", ofstream::ate);

    This opens the file in append mode.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. writing data(adding new records) to the input file
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 06:51 PM

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