Homework problem...structures or arrays?

This is a discussion on Homework problem...structures or arrays? within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm new here. Im a senior in high school, and I skipped Computer Programming I, and went straight to ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    Homework problem...structures or arrays?

    Hi. I'm new here. Im a senior in high school, and I skipped Computer Programming I, and went straight to Programming II. The problem is, I had never programmed in my life until yesterday - So I'm still getting the hang of things. I've got a problem for homework, but I dont know how I'd go about doing it. Suggestions and Ideas - please.

    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.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    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.
    Sent from my iPadŽ

  3. #3
    Registered User major_small's Avatar
    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

  4. #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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    The tutorials or FAQs or a C++ book might help.
    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.

  6. #6
    Registered User major_small's Avatar
    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

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    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.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by major_small
    just use a loop and file I/O...
    ...and if you don't understand what this means, I'd say your best bet is to visit your guidance counselor tomorrow with a change course form, and get yourself into Programming I which you shouldn't have bypassed in the first place.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    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.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    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.

  11. #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.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    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.

  13. #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

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  15. #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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 11:31 AM
  3. Structures tutorial problem
    By Steve MacD in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 03:27 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  5. Replies: 7
    Last Post: 12-29-2001, 10:25 PM

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