473,473 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File input and output

87 New Member
Hey all. I am working with a file input/output program. The program is take input from a file, manipulate the info, and then output the info to another file.

The program takes the input just fine, although I am unable to manipulate the info (hours worked* payrate). I have all arrays set as character arrays, I believe that might be the problem.
Jun 8 '07 #1
17 2058
mac11
256 Contributor
How are you trying to manipulate your data. Manipulating character arrays is quite common. What specific methods/functions are you trying that aren't working?
Jun 8 '07 #2
Savage
1,764 Recognized Expert Top Contributor
Hey all. I am working with a file input/output program. The program is take input from a file, manipulate the info, and then output the info to another file.

The program takes the input just fine, although I am unable to manipulate the info (hours worked* payrate). I have all arrays set as character arrays, I believe that might be the problem.
That's good,u know to determine where the problem is and this is first and(probably) ur hardest step.Can u please post that troubleshoting code,so that we can help u solve it?

And it would be nice if u please read posting guidelines if u haven't already,they will help u to get ur answer a way more faster..

:D

Savage
Jun 8 '07 #3
Hypnotik
87 New Member
Hey Savage, sorry for not posting the code right away. I realized I had taken out some info from the program while troubleshooting, and I was on my way out the door.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6. void main()
  7. {
  8.     int i=1,r=0;
  9.     char FirstName[10]={0}, LastName[9]={0}, IDNumber[10]={0},PayRate[10]={0},HoursWorked[12]={0},TotalPay[10]={0};
  10.     ifstream ifp;
  11.     ofstream ofp;
  12.     ifp.open("c:\\Documents and Settings\\Jeff Young\\Desktop\\Lab14.in");
  13.     ofp.open("c:\\Pay.out");
  14.     while (ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate)
  15.     {
  16.         if (HoursWorked<=40)
  17.             TotalPay=HoursWorked*PayRate;
  18.         else
  19.             TotalPay=(40*PayRate)+(HoursWorked-40)*PayRate*1.5;
  20.     }
  21. //    cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<IDNumber<<"    "<<setw(10)<<HoursWorked<<"  "<<setw(10)<<PayRate<<"   "<<endl;
  22.     cout<<setw(10)<<LastName<<"    "<<setw(10)<<IDNumber<<"   "<<setw(10)<<TotalPay<<endl;
  23. }
  24.  
  25.  
Thanks alot.

J
Jun 9 '07 #4
Savage
1,764 Recognized Expert Top Contributor
Hey Savage, sorry for not posting the code right away. I realized I had taken out some info from the program while troubleshooting, and I was on my way out the door.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6. void main()
  7. {
  8.     int i=1,r=0;
  9.     char FirstName[10]={0}, LastName[9]={0}, IDNumber[10]={0},PayRate[10]={0},HoursWorked[12]={0},TotalPay[10]={0};
  10.     ifstream ifp;
  11.     ofstream ofp;
  12.     ifp.open("c:\\Documents and Settings\\Jeff Young\\Desktop\\Lab14.in");
  13.     ofp.open("c:\\Pay.out");
  14.     while (ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate)
  15.     {
  16.         if (HoursWorked<=40)
  17.             TotalPay=HoursWorked*PayRate;
  18.         else
  19.             TotalPay=(40*PayRate)+(HoursWorked-40)*PayRate*1.5;
  20.     }
  21. //    cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<IDNumber<<"    "<<setw(10)<<HoursWorked<<"  "<<setw(10)<<PayRate<<"   "<<endl;
  22.     cout<<setw(10)<<LastName<<"    "<<setw(10)<<IDNumber<<"   "<<setw(10)<<TotalPay<<endl;
  23. }
  24.  
  25.  
Thanks alot.

J
U need to convert those char arrays into integer and then u can use them like u use them now.

Check out function itoa

PS:U are working in c++,arrays of chars are used in c,in c++ u have strings
Savage
Jun 9 '07 #5
Hypnotik
87 New Member
So is it possible to type cast the arrays that contains the info I need to multiply?

J
Jun 9 '07 #6
JosAH
11,448 Recognized Expert MVP
So is it possible to type cast the arrays that contains the info I need to multiply?

J
Don't do that; simply define your numeric variables as a double or int or whatever
you find suitable; as in:
Expand|Select|Wrap|Line Numbers
  1. double HoursWorked;
The >> operator is overloaded for those types and you can read sequences of
input characters as doubles or ints ... no need to convert char arrays yourself.
You're programming in C++, not C, make good use of that language.

kind regards,

Jos
Jun 9 '07 #7
Hypnotik
87 New Member
Hey Jos. I'm a bit confused. I have HoursWorked declared as a char array. Do you suggest I create a new "double" variable?? I created HoursWorked1 and I still receive an error for converting char to double.

I guess I'm more than a little confused.

J
Jun 9 '07 #8
JosAH
11,448 Recognized Expert MVP
Hey Jos. I'm a bit confused. I have HoursWorked declared as a char array. Do you suggest I create a new "double" variable?? I created HoursWorked1 and I still receive an error for converting char to double.

I guess I'm more than a little confused.

J
Don't do that; simply define HoursWorked (and the other numeric variables) as
doubles, ints, etc. Don't use char arrays for them. That's all you need to do to
get your numbers read from the istream. Don't even define additional variables;
they have no use.

kind regards,

Jos
Jun 9 '07 #9
Hypnotik
87 New Member
That is actually how I began the program, however I loose my column headings when I do that.
Jun 9 '07 #10
JosAH
11,448 Recognized Expert MVP
That is actually how I began the program, however I loose my column headings when I do that.
Your column headings are a 'special' row (the first one I presume); read that row
using those char arrays if you want but don't read the actual data rows like that.

kind regards,

Jos
Jun 9 '07 #11
Hypnotik
87 New Member
Ok, so I've worked with the code. I took in the file heading, and I've created another input file pointer. Although I am unsure how to point to the beginning of the second row.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6. void main()
  7. {
  8.     int HoursWorked=0,TotalPay=0,IDNumber=0,PayRate=0;
  9.     char FirstName[10]={0}, LastName[10]={0};
  10.     ifstream ifp,ifp2;
  11.     ofstream ofp;
  12.     ifp.open("c:\\Documents and Settings\\Jeff Young\\Desktop\\Lab14.in");
  13.     ofp.open("c:\\Pay.out");
  14.     ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate;
  15.     while (ifp2>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate)
  16.     {
  17.         if (HoursWorked<=40)
  18.             TotalPay=HoursWorked*PayRate;
  19.         else
  20.             TotalPay=(40*PayRate)+(HoursWorked-40)*PayRate*1.5;
  21.     }
  22. //    cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<"IDNumber"<<"    "<<setw(10)<<"HoursWorked"<<"  "<<setw(10)<<"PayRate"<<"   "<<endl;
  23.     cout<<LastName<<"    "<<setw(10)<<"IDNumber"<<"   "<<setw(10)<<"TotalPay"<<endl<<endl;
  24. }
  25.  
  26.  
Thanks,

J
Jun 9 '07 #12
JosAH
11,448 Recognized Expert MVP
Ok, so I've worked with the code. I took in the file heading, and I've created another input file pointer. Although I am unsure how to point to the beginning of the second row.
After you've read the first row, the 'pointer' points to the second row automatically.
You can even read the entire first line and forget all about it if you want. Your
last reply's question wasn't that clear to me so my reply could be completely off.

kind regards,

Jos
Jun 9 '07 #13
Hypnotik
87 New Member
I meant I had changed the code to read in the first line, and I was asking how to move to the second line of the file.

My program now outputs the correct headings (LastName, HoursWorked,TotalPay) however it does not output any other information, and I haven't even attempted to manipulate the information.

I've posted the updated code.

I have the ifp while loop commented out, as soon as I uncomment the while loop the output is incorrect.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6. void main()
  7. {
  8.     int HoursWorked=0,TotalPay=0,IDNumber=0,PayRate=0;
  9.     char FirstName[10]={0}, LastName[10]={0};
  10.     ifstream ifp,ifp2;
  11.     ofstream ofp;
  12.     ifp.open("c:\\Documents and Settings\\Jeff Young\\Desktop\\Lab14.in");
  13.     ofp.open("c:\\Pay.out");
  14.     ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate;
  15. //    while (ifp>>FirstName>>LastName>>IDNumber>>HoursWorked>>PayRate)
  16. //    {
  17. //        if (HoursWorked<=40)
  18. //            TotalPay=HoursWorked*PayRate;
  19. //        else
  20. //            TotalPay=(40*PayRate)+(HoursWorked-40)*PayRate*1.5;
  21. //    }
  22. //    cout<<setw(10)<<FirstName<<"   "<<setw(10)<<LastName<<"   "<<setw(10)<<"IDNumber"<<"    "<<setw(10)<<"HoursWorked"<<"  "<<setw(10)<<"PayRate"<<"   "<<endl;
  23.     cout<<LastName<<"    "<<setw(10)<<"IDNumber"<<"   "<<setw(10)<<"TotalPay"<<endl<<endl;
  24. }
  25.  
  26.  
j
Jun 10 '07 #14
JosAH
11,448 Recognized Expert MVP
I meant I had changed the code to read in the first line, and I was asking how to move to the second line of the file.

My program now outputs the correct headings (LastName, HoursWorked,TotalPay) however it does not output any other information, and I haven't even attempted to manipulate the information.

I've posted the updated code.

I have the ifp while loop commented out, as soon as I uncomment the while loop the output is incorrect.
Good; so you do get output don't you? If so that implies there's something wrong
in the body of your while loop. It also implies that everything is nicely converted
to the correct type.

kind regards,

Jos
Jun 10 '07 #15
Hypnotik
87 New Member
The exact output I receive from the code I posted is:

LastName IDNumber TotalPay



I don't receive any of the other info which is in the file.

Should my while loop only loop over the information I need to manipulate and eventually output?


BTW: Thanks for all the help/suggestions Jos
Jun 10 '07 #16
Hypnotik
87 New Member
Might also help if my output statement is in my loop. That doesn't correct the problem though.

J
Jun 10 '07 #17
JosAH
11,448 Recognized Expert MVP
BTW: Thanks for all the help/suggestions Jos
No need to thank me; I didn't do much. Your own idea of putting cout >>
expressions at every location you suspect is a good idea though; I use it all
the time and you are actually able to pinpoint your bug between two locations
that are very near to each other in your code: first everything was fine, second
everything turned out goofy. Between those two points your bug can be found
most of the time. Give it a try.

kind regards,

Jos
Jun 10 '07 #18

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

Similar topics

0
by: suresh | last post by:
Hello All, Iam writing a wsdl file using the Servicedescription class.The document I get is a well formed xml document.But some how when I try to add this wsdl file as a webrefernce to another...
1
by: moonriver | last post by:
In a xml file, can we make reference to another xml file so that all contents of the latter xml file will be included into the first xml file? Had better give me an example for details.
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
6
by: neo | last post by:
Hello everyone, I want to write a function that takes a simple text file as input and outputs a file such that the last line in the input file becomes the first line in the output file and the...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
0
by: Craftkiller | last post by:
=========Program compiles on both windows and linux=========== Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
4
by: rsaharia | last post by:
Hello All, I need help with this particular .pl file I picked up from http://www.veritools-usa.com/xnf2vhdl.htm What it's supposed to do is really convert an xnf file to a vhdl file. I need it for...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.