473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

cout overwrites previous inputs

20
Hi all - I'm trying to generate a payroll. I'm having trouble with the cout statements overwriting the previous inputs, e.g. enter info for employe 1: blah blah blah. Enter info for employee 2: blah blah blah...

When I generate the report, only information for employee 2 appears. I can see why this would happen from looking at my code... I would appreciate it if someone could let me know how to modify the code so that I could print out the results for all the employees entered.

Here is the some code from main():

Expand|Select|Wrap|Line Numbers
  1.     while (empNum != 0)
  2.     {
  3.           cout<<"Enter employee name: ";
  4.           cin>>str;
  5.           cout<<"Enter pay rate: ";
  6.           cin>>prate;
  7.           cout<<"Enter hours worked: ";
  8.           cin>>hours;
  9.           CalcPay(prate, hours, wages);
  10.           total = total + wages;
  11.           payFile<<empNum<<prate<<hours<<wages;
  12.           cout<<"\nEnter Employee number (Or press 0 to exit): ";
  13.           cin>>empNum;
  14.     }
  15.     cout<<"Total Payroll is: ";
  16.     cout<<total<<endl;
  17.  
  18.     cout<<"\n\nDo you want to generate a report? Press Y or N: ";
  19.     cin>>yorn;
  20.     if(yorn != 'n' || yorn != 'N')
  21.     {
  22.             report(str, prate, hours, otime, wages);
  23.     }

Here is some code from the function report() which is supposed to generate the report:

Expand|Select|Wrap|Line Numbers
  1. void report(char* str, float prate, float hours, float otime, float wages)
  2. {
  3.      cout<<endl<<setw(55)<<"EMPLOYEE PAYROLL REPORT - SPRING 2007\n"<<endl;
  4.  
  5.      line();
  6.      cout<<endl;
  7.  
  8.      cout<<"    "<<setw(15)<<"Hourly"<<setw(10)<<"     "<<setw(15)<<"Overtime"<<setw(15)<<"Total \n";
  9.      cout<<"Name"<<setw(15)<<"Rate  "<<setw(10)<<"Hours"<<setw(15)<<"Hours   "<<setw(15)<<"Salary\n";
  10.  
  11.      line();
  12.      cout<<endl;
  13.  
  14.      cout<<str<<setw(15)<<prate<<setw(10)<<hours<<setw(15)<<otime<<setw(15)<<wages<<endl;
  15. }
Thanks!
Mar 27 '07 #1
3 2529
Savage
1,764 Expert 1GB
Hi all - I'm trying to generate a payroll. I'm having trouble with the cout statements overwriting the previous inputs, e.g. enter info for employe 1: blah blah blah. Enter info for employee 2: blah blah blah...

When I generate the report, only information for employee 2 appears. I can see why this would happen from looking at my code... I would appreciate it if someone could let me know how to modify the code so that I could print out the results for all the employees entered.

Here is the some code from main():

Expand|Select|Wrap|Line Numbers
  1.     while (empNum != 0)
  2.     {
  3.           cout<<"Enter employee name: ";
  4.           cin>>str;
  5.           cout<<"Enter pay rate: ";
  6.           cin>>prate;
  7.           cout<<"Enter hours worked: ";
  8.           cin>>hours;
  9.           CalcPay(prate, hours, wages);
  10.           total = total + wages;
  11.           payFile<<empNum<<prate<<hours<<wages;
  12.           cout<<"\nEnter Employee number (Or press 0 to exit): ";
  13.           cin>>empNum;
  14.     }
  15.     cout<<"Total Payroll is: ";
  16.     cout<<total<<endl;
  17.  
  18.     cout<<"\n\nDo you want to generate a report? Press Y or N: ";
  19.     cin>>yorn;
  20.     if(yorn != 'n' || yorn != 'N')
  21.     {
  22.             report(str, prate, hours, otime, wages);
  23.     }

Here is some code from the function report() which is supposed to generate the report:

Expand|Select|Wrap|Line Numbers
  1. void report(char* str, float prate, float hours, float otime, float wages)
  2. {
  3.      cout<<endl<<setw(55)<<"EMPLOYEE PAYROLL REPORT - SPRING 2007\n"<<endl;
  4.  
  5.      line();
  6.      cout<<endl;
  7.  
  8.      cout<<"    "<<setw(15)<<"Hourly"<<setw(10)<<"     "<<setw(15)<<"Overtime"<<setw(15)<<"Total \n";
  9.      cout<<"Name"<<setw(15)<<"Rate  "<<setw(10)<<"Hours"<<setw(15)<<"Hours   "<<setw(15)<<"Salary\n";
  10.  
  11.      line();
  12.      cout<<endl;
  13.  
  14.      cout<<str<<setw(15)<<prate<<setw(10)<<hours<<setw(15)<<otime<<setw(15)<<wages<<endl;
  15. }
Thanks!
I think that str must be a array of strings,so that it can store data in depandanece of array index.

This will also provoke that you must integrate a counter inside :

Expand|Select|Wrap|Line Numbers
  1. while (empNum != 0)
so that array stores report for each employe.


Savage
Mar 27 '07 #2
shk253
20
Savage - I haven't learnt arrays yet. I've been trying to use fstream to output to a file but each employee's data gets overridden. Is it possible to to tell the fstream file handle to skip a line each time it write the employees data? I suppose each time main() calls report(), the previous gnrt file gets overridden. Are arrays my only option?

Here is a snippet:

Expand|Select|Wrap|Line Numbers
  1. void report(char* str, float prate, float hours, float otime, float wages)
  2. {
  3.      ofstream gnrt;
  4.      gnrt.open("gnrt.txt");
  5.  
  6.      cout<<endl<<setw(55)<<"EMPLOYEE PAYROLL REPORT - SPRING 2007\n"<<endl;
  7.      gnrt<<endl<<setw(55)<<"EMPLOYEE PAYROLL REPORT - SPRING 2007\n"<<endl<<std::endl;
  8.  
  9.      line();
  10.      cout<<endl;
  11.      for(char c=0;c<75;c++)
  12.      {
  13.      gnrt<<'-';
  14.      }
  15.      gnrt<<std::endl;     
  16.  
  17.      cout<<"    "<<setw(15)<<"Hourly"<<setw(10)<<"     "<<setw(15)<<"Overtime"<<setw(15)<<"Total \n";
  18.      cout<<"Name"<<setw(15)<<"Rate  "<<setw(10)<<"Hours"<<setw(15)<<"Hours   "<<setw(15)<<"Salary\n";
  19.      gnrt<<"    "<<setw(15)<<"Hourly"<<setw(10)<<"     "<<setw(15)<<"Overtime"<<setw(15)<<"Total \n"<<std::endl;
  20.      gnrt<<"Name"<<setw(15)<<"Rate  "<<setw(10)<<"Hours"<<setw(15)<<"Hours   "<<setw(15)<<"Salary\n"<<std::endl;
  21.  
  22.      line();
  23.      cout<<endl;
  24.      for(char c=0;c<75;c++)
  25.      {
  26.      gnrt<<'=';
  27.      }
  28.      gnrt<<std::endl;
  29.  
  30.      cout<<str<<setw(15)<<prate<<setw(10)<<hours<<setw(15)<<otime<<setw(15)<<wages<<endl;
  31.      gnrt<<str<<setw(15)<<prate<<setw(10)<<hours<<setw(15)<<otime<<setw(15)<<wages<<endl<<std::endl;
  32. }     
Mar 27 '07 #3
Savage
1,764 Expert 1GB
Try opening file in append regime:

Expand|Select|Wrap|Line Numbers
  1. ....Code.....
  2.  
  3. ofstream gnrt("gnrt.txt", ios::app);
  4.  
  5. ..............................................

Savage
Mar 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
16
by: David | last post by:
I have following code. float b; b= 1.234; cout<<hex<<b<<endl; How come it cannot ouput the hex representation of b?
7
by: Andrea | last post by:
Hi there - I'm hoping someone can help me; I've been struggling with this for a few days! :-) I have a webpage that is comprised of many forms containing questions. As the user answers one...
4
by: olson_ord | last post by:
Hi, I am not so new to C++, but I have not used it much. I was trying to append a string at the end of a previous string. If I just do this in a test program that is 3 lines long i.e. define the...
5
by: Cliff Martin | last post by:
Hi, I am writing a simple filter type program that can take input from file or stdin, and output to a file or stdout. It would make life much easier if I could assign cin to a ifstream object...
8
by: naveen.dixit | last post by:
I have a piece of code #include<iostream> using namespace std; main() { int x=12; int *p = &x; cout<<endl<<p<<endl<<*p++<<endl; cout<<endl<<p<<endl<<*p<<endl;
23
by: mahesh | last post by:
Hi all, I have following code that is supposed to increase the power by specified value. int main() { system("cls"); int i, exponent; double base; double new_base=0.0;
2
by: Jahan zaib butt | last post by:
Hi all, I have take some inputs from user i variable a and b and want to show them using cout function i have used this code cout<<"Numbers are %d and %d",a,b; and the outout is Numbers...
3
by: Down South | last post by:
Hi, I am using Dev-C++ 4.9.9.2, when compiling and running the following code, #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main(int nAverage, int a, int...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.