![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 13
| Homework problem...structures or arrays? 8. Three employees in a company are up for a special pay increase. You are given a file, Ch3_Ex8Data.txt, with the following data Miller Andrew 65789.87 5 Green Sheila 75892.56 6 Sethi Amit 74900.50 6.1 Each input line consists of the employee's last name, first name, current salary, and precent pay increase. Write a program that reads data from the specified file and stores the output in the file Ch3_Ex8Output.dat. For each employee the days must be output in the following form: firstName, lastName, updatedSalary. Format the ouput of the decimal numbers to two decimal places. I know how to input the file, and all the main basics of C++, but I dont understand how I can the program to like....loop for each person in the file? Maybe I'm not supposed to do that? Any advice/help is appreciated. |
| tortan is offline | |
| | #2 |
| Devil's Advocate Join Date: May 2004 Location: Out of scope
Posts: 3,775
| Did they teach structures and arrays before loops? I don't know how much you expect us to tell you when we know it's a homework problem, but I will tell you that, yes, you're supposed to loop for each entry and yes, you're supposed to store it in one of the two programming terms in your thread title.
__________________ Terms of Service By quoting or replying directly to this post, you consent to the fact that all of the information in the post above is completely accurate and highly intelligent and no comments will be made towards its validity, thoughtlessness, and/or grammatical structure. Violators will be prosecuted to the fullest extent of the law. |
| SlyMaelstrom is offline | |
| | #3 |
| Registered User Join Date: May 2003
Posts: 2,787
| I wouldn't store it in either... it's just a read, calculate, write program... nothing really fancy needed IMO unless of course the assignment calls for it... but I didn't see that...
__________________ Join is in our Unofficial Cprog IRC channel Server: irc.phoenixradio.org Channel: #Tech Team Cprog Folding@Home: Team #43476 Download it Here Detailed Stats Here More Detailed Stats 52 Members so far, are YOU a member? Current team score: 1223226 (ranked 374 of 45152) The CBoard team is doing better than 99.16% of the other teams Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374) Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT |
| major_small is offline | |
| | #4 |
| Registered User Join Date: Aug 2006
Posts: 13
| I've really got no idea what everyone else in my class has been taught. My teacher said this should be an easy program to refresh everyones memory about reading and writing from an external file. I've been looking stuff up for over 2 hours trying to find out how to do it. I just need some guidance on what to do/use. |
| tortan is offline | |
| | #5 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
|
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #6 |
| Registered User Join Date: May 2003
Posts: 2,787
| just use a loop and file I/O... if you can't do the refresher work, how'd you get into a class that far above your level?
__________________ Join is in our Unofficial Cprog IRC channel Server: irc.phoenixradio.org Channel: #Tech Team Cprog Folding@Home: Team #43476 Download it Here Detailed Stats Here More Detailed Stats 52 Members so far, are YOU a member? Current team score: 1223226 (ranked 374 of 45152) The CBoard team is doing better than 99.16% of the other teams Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374) Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT |
| major_small is offline | |
| | #7 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| File I/O and loops tutorials. They might be hard to read if you've never done any programming before, though.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #8 | |
| Devil's Advocate Join Date: May 2004 Location: Out of scope
Posts: 3,775
| Quote:
__________________ Terms of Service By quoting or replying directly to this post, you consent to the fact that all of the information in the post above is completely accurate and highly intelligent and no comments will be made towards its validity, thoughtlessness, and/or grammatical structure. Violators will be prosecuted to the fullest extent of the law. | |
| SlyMaelstrom is offline | |
| | #9 |
| Registered User Join Date: Aug 2006
Posts: 159
| I'd suggest either dropping the class and starting with programming 1, or talk to the teacher and get a week or so extension and get a few books ("Teach Yourself C++ in 21 Days" is excellent to learn quickly) to get a grasp on things. |
| System_159 is offline | |
| | #10 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| For recommended books, see the C++ Book Recommendations sticky: C++ Book Recommendations
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #11 |
| Registered User Join Date: Aug 2006
Posts: 13
| Ah, I've got it figured out now. Problem is I was sitting here without a book. The teacher is cool, and she'll let me catch up, so it wont be a problem. The reason I didnt take Comp Programming 1 is because its mainly VB - they only touch only c++ basics at the end - and I have no interest in learning VB. |
| tortan is offline | |
| | #12 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Books are essential. In fact, if you started a poll in General Discussions on how many books everyone owns, I'm sure the average would be over half a dozen.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #13 |
| Registered User Join Date: Aug 2006
Posts: 13
| Ideas why this doesnt work? Code: #include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
bool getEmp(ifstream& empFile, string& lastName, string& firstName,
double& salary, double& raise);
void writeEmp(ofstream& newFile, string& firstName, string&lastName,
double& updatedSalary);
void calcSalary(double salary, double raise, double updatedSalary);
using namespace std;
int main()
{
cout << "Beginning employee salary updation.\n";
ifstream empFile ("Ch3_Ex8Data.txt");
if (!empFile) {
cerr << "The system was unable to open the file."; //error if file is in use.
exit(100); //if it is, terminate.
}
ofstream newFile ("Ch3_Ex8Output.dat")
if (!newFile) {
cerr << "They system was unable to open the file.";
exit (102);
}
string firstName, lastName;
double salary, raise, updatedSalary;
while (getEmp (empFile, lastName, firstName, salary, raise))
{
calcSalary (salary, raise, updatedSalary);
writeEmp (newFile, firstName, lastName, updatedSalary);
} //end of while
empfile.close();
newfile.close();
cout << "End of salary updation.\n";
return 0;
} //end of main
bool getEmp(ifstream& empFile, string& lastName, string& firstName,
double& salary, double& raise)
{
empFile >> lastName >> firstName >> salary >> raise;
if (!empFile)
return false;
return true;
} //end getEmp
void calcSalary(double salary, double raise, double updatedSalary)
{
updatedSalary = (((raise*.01)+1)*salary)
return;
} //end calcSalary
void writeEmp(ofstream& newFile, string& firstName, string&lastName,
double& updatedSalary)
{
newfile << firstName;
newfile.fill << (' ');
newfile << lastName;
newfile.fill (' ');
newfile << updatedSalary;
return;
} //end writeEmp
|
| tortan is offline | |
| | #14 |
| carry on Join Date: Feb 2003 Location: Seattle, WA
Posts: 1,971
| Could you give a little more detail than "doesn't work"?
__________________ "Think not but that I know these things; or think I know them not: not therefore am I short Of knowing what I ought." -John Milton, Paradise Regained (1671) "Work hard and it might happen." -XSquared |
| JaWiB is offline | |
| | #15 |
| Registered User Join Date: Aug 2006
Posts: 13
| I'm getting errors such as 'ifstream' was not declared in this scope. that along with all the rest of my variables and ofstream. I pretty much suck at programming I guess. |
| tortan is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sorting problem with structures and arrays, could I get some guidance? | badtwistofate | C Programming | 26 | 06-11-2009 11:27 AM |
| Problem with arrays structures sorting. | pitifulworm | C Programming | 42 | 02-09-2009 12:31 PM |
| Structures tutorial problem | Steve MacD | C Programming | 3 | 11-21-2005 04:27 PM |
| Crazy memory problem with arrays | fusikon | C++ Programming | 9 | 01-15-2003 09:24 PM |
| c++ help with arrays pointers enumerators , structures and unions as well asfunctions | Unregistered | C++ Programming | 7 | 12-29-2001 11:25 PM |