Thread: Homework problem...structures or arrays?

  1. #16
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    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;
    ifstream, ofstream, and string are all part of the std namespace, so the compiler doesn't see them until after your using declaration. the simple fix is to put 'using namespace std' immediately after your includes.

    >I pretty much suck at programming I guess.

    Maybe, but I doubt you've been at it long enough to know that
    "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

  2. #17
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Quote Originally Posted by tortan
    I pretty much suck at programming I guess.
    No need to get discouraged over a simple problem. Just keep reading, trying things, debugging, reading some more, and if you still can't get it, come back here and ask questions. What about asking your teacher and/or classmates for help?

    Offtopic: Why does it seem like all the other highschools in the world offer programming courses, and not mine?
    Last edited by psychopath; 08-29-2006 at 09:08 PM.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  3. #18
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I looked at your code more closely (that is, I actually put it in my compiler) and it looks like you've got a ton a little syntax errors. For example, you're missing a couple semicolons, and you type the wrong variable name ("newfile" is different than "newFile"). I'd say you need to write smaller portions of code between compiles.
    "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

  4. #19
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Quote Originally Posted by JaWiB
    Code:
    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;
    ifstream, ofstream, and string are all part of the std namespace, so the compiler doesn't see them until after your using declaration. the simple fix is to put 'using namespace std' immediately after your includes.

    >I pretty much suck at programming I guess.

    Maybe, but I doubt you've been at it long enough to know that

    Thanks so much! You gave me the insight I needed to get this working.

  5. #20
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Hmm, one more thing if anyone can figure this out. (I've been trying the past 30 minutes.) When my data is outputted, the updated salary is wrong. The output text file shows as:

    Andrew Miller 1.8e-043
    Sheila Green 1.8e-043
    Amit Sethi 1.8e-043


    Obviously, these salaries are wrong. Right now I'm just looking over my math and whatnot to see if I can figure this out, but if you have ideas let me know. Updated coded is below.

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    bool getEmp (ifstream& empFile, string& lastName, string& firstName,
                 float& salary, float& raise);
                 
    void writeEmp (ofstream& newFile, string& firstName, string&lastName, 
                   float& updatedSalary);
    
    void calcSalary (float salary, float raise, float updatedSalary);
    
    
    
    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;
     float 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";
     
     cout << salary;
    return 0;
    
    } //end of main
    
    
    bool getEmp(ifstream& empFile, string& lastName, string& firstName,
                 float& salary, float& raise)
    {           
                empFile >> lastName >> firstName >> salary >> raise;
                if (!empFile)
                   return false;
                return true; 
    } //end getEmp
    
    void calcSalary(float salary, float raise, float updatedSalary)
    {
          updatedSalary = (salary * (1+ (raise/100)));
         
         return;
    } //end calcSalary
    
    void writeEmp(ofstream& newFile, string& firstName, string& lastName, 
                   float& updatedSalary)
    {
                   newFile << setprecision(2) << firstName << "\t";
                   newFile << lastName << "\t";
                   newFile << updatedSalary << "\n";
                   return; 
    } //end writeEmp

    edit i changed the setprecision line to this

    Code:
    newFile << fixed << setprecision(2) << firstName << "\t";
    Now sci-notation is gone, but the numbers are still wrong.
    Last edited by tortan; 08-29-2006 at 09:51 PM.

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void calcSalary(float salary, float raise, float updatedSalary)
    {
          updatedSalary = (salary * (1+ (raise/100)));
         
         return;
    } //end calcSalary
    The reason is, updatedSalary is local to this function. You probably forgot to pass it as a reference.
    Although it would make more sense if the function took two float arguments and returned the result as float (not void return type).

  7. #22
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by JaWiB
    ifstream, ofstream, and string are all part of the std namespace, so the compiler doesn't see them until after your using declaration. the simple fix is to put 'using namespace std' immediately after your includes.
    The better fix (although not as simple) is to change all the ofstream to std::ofstream, string to std::string, etc.

    In the end, "using namespace std;" really is a bad habit to get into. When you work on code seriously, those namespaces become a real benefit. I haven't used the keyword "using" for any reason for the past seven years; it actually makes code far more readable to keep the std:: everywhere (along with boost::, text::, and all the other namespaces I have).

    On beginner code, it may make things easier, but it also tends to ingrain a bad habit you'll need to shake eventually.
    Last edited by Cat; 08-30-2006 at 01:34 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

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, 12:31 PM
  3. Structures tutorial problem
    By Steve MacD in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 04:27 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Replies: 7
    Last Post: 12-29-2001, 11:25 PM