Dismiss Notice

Welcome To CK5!

Registering is free and easy! Hope to see you on the forums soon.

Score a FREE t-shirt and membership sticker when you sign up for a Premium Membership and choose the recurring plan.

Anyone know a reasonable amount of C++?

Discussion in 'The Lounge' started by mikey_d05, Nov 22, 2005.

  1. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    I'm dyin' here. I'm trying to get a program to read multipe pieces of data from a text file repeatedly, calculate figures based on said data, and print them to a new text file.

    It's due in 17 hours and I want to cry...or drink.
     
  2. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    If I didn't have to be at work at 4 AM tomorrow.... :(

    Still you could email me the project definition

    What are you "allowed" to use?
     
  3. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    "Using an editor, create the employee file shown in Table 7-7.

    (crude reproduction of the table)

    Employee # Department Pay Rate Exempt Hours Worked
    101 41 8.11 Y 49
    722 32 7.22 N 40
    1273 23 5.43 Y 39
    2584 14 6.74 N 45

    Write a program to read the employee file and creat a payroll register. The register is to contain the following data:

    Employee number (print left justified)

    department

    pay rate

    exempt

    hours worked

    base pay (pay rate * hours worked)

    overtime pay (exempt employees are paid for 40 hours at base pay regardless of the number of hours actually worked, whether it be above or below 40)

    total pay"

    Doing the calculations is simple enough but importing and reading the text file and then writing to another one is beyond me. Our prof. sucks and our book is ridden with error that make running sample programs impossible. By the time you go through and debug the programs in the book you could have written 10 good programs.
     
  4. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    ...and I'm not quite sure what you mean by "allowed to use" I still run windows so I have to run a shell and use good old VI or just use a text editor and FTP it to the linux server.
     
  5. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    int nEmpNum, nDepartment, nHours;
    char chExempt;
    double dPayRate, dTotalPay, dOvertimePay;

    ifstream inFile("./data.txt");
    ofstream outFile("./output.txt");
    while (!inFile.eof() ) {

    inFile >> nEmpNum >> nDepartment >> dPayRate >> chExempt >> nHours;
    dOvertimePay = 0;
    if( chExempt == 'N' ){
    dOvertimePay = nHours > 40 ? dPayRate * (nHours - 40) | 0;
    dTotalPay = dPayRate * nHours;
    }else
    dTotalPay = dPayRate * 40;
    outFile >> nEmpNum >> '\t' >> nDepartment >> '\t' >> dPayRate >> '\t' >> chExempt >> '\t' >> nHours >> '\t' >> (dOvertimePay > 0 ? dPayRate * 40 | dTotalPay) >> '\t' >> dOvertimePay >> '\t' >> dTotalPay >> endl;
    }

    inFile.close();
    outFile.close();
     
  6. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    If I'm ever going through Wichita, I'm stopping and buying you a beer.
     
  7. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    Goin to BB '06? :deal: :D
     
  8. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    Thinking about it, if I do, I'll make good on it. I've gotta rearrange a few things but that saved me a whole bunch of headaches. I'm cool with loops and such but I suck at I/O programming.
     
  9. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    The reason why I asked what you're "allowed" to use is because the way C++ classes are (no pun intended), you can have a simple project like that, but have to have some convoluted way of doing it.

    Like if you wanted to make a strictly object-based program, you'd need a couple different classes, and those are a PITA (for me). I always have to draw diagrams when I deal with classes.

    I have my email/IM stuff public here, so if you have any other questions later, feel free to get in touch with me.
     
  10. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    Yeah well, we're supposed to write it with multiple functions but then you have to deal with passing by values and when writing a program as simple as this I think it's pretty moronic to do so.

    The last time I disobeyed the assignment I got a 95% because she admitted my way was more efficient. :rolleyes:

    Thanks for your help man. I might need some info later but seeing a CORRECT example of reading and writing to a text file helps more than you know. I just ran another program from our textbook that threw multiple errors. :rolleyes:
     
  11. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    PHP:
    bool ReadFromFile(intnEmpNumintnDepartmentintnHourscharchExemptdoubledPayRateifstreaminFile)
    {
      if(!
    inFile.eof()){
        
    inFile >> nEmpNum >> nDepartment >> dPayRate >> chExempt >> nHours;
        return 
    true;
      }else
        return 
    false;
    }

    void WriteToFile(intnEmpNumintnDepartmentintnHourscharchExemptdoubledPayRatedoubledOvertimePaydoubledBasePayofstreamoutFile){

      
    outFile >>  nEmpNum >> '\t' >> nDepartment >> '\t' >> dPayRate >> '\t' >> chExempt >> '\t' >> nHours >> '\t' >> dBasePay >> '\t' >> dOvertimePay >> '\t' >> (dBasePay+dOvertimePay)  >> endl;

      return;
    }

    void CalculatePay(intnHourscharchExemptdoubledPayRatedoubledOvertimePaydoubledBasePay){

      if(
    chExempt == 'N'){
        
    dOvertimePay nHours 40 | (nHours-40)*dPayRate;
        
    dBasePay nHours 40 40*dPayRate dPayRate nHours;
      }else{
        
    dOvertimePay 0;
        
    dBasePay 40*dPayRate;
      }

      return;
    }


    int main(){

      
    int nEmpNumnDepartmentnHours;
      
    char chExempt;
      
    double dPayRatedBasePaydOvertimePay;

      
    ifstream inFile("./data.txt");
      
    ofstream outFile("./output.txt");
      while (
    ReadFromFile(nEmpNumnHourschExemptdPayRateinFile) ) {
        
    Calculate(nHourschExemptdPayRatedOvertimePaydBasePay);
        
    WriteToFile(nEmpNumnDepartmentnHourschExemptdPayRatedOvertimePaydBasePayoutFile);
      }


    inFile.close();
    outFile.close();

    return 
    0;
    }
     
  12. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    Jesus man, I owe you a friggin case of booze already.
     
  13. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    Ooops, had to clean that up a bit... had an extra ! and } or two :(
     
  14. newyorkin

    newyorkin 1 ton status

    Joined:
    May 8, 2001
    Posts:
    16,555
    Likes Received:
    157
    Location:
    Los Estados Unitos

    Keep it public! I'm enjoying following this!


    Does it take a lot of education to learn C++? I'm pretty good with VBscripting, so I can hack a lot of the concepts, but I wonder if it's as easy to learn as syntax, or completely different ways of thinking...
     
  15. Jagged

    Jagged 1 ton status

    Joined:
    Apr 25, 2005
    Posts:
    11,460
    Likes Received:
    1
    Location:
    SWVA
    Well, there's basically two schools of C++. C++ is object-oriented. Something that should look familiar with VB. But, C++ can also be written without objects.

    Myself, I learned C before C++, never really 'clicked' with the objects until I learned Java.

    I started coding with QBASIC. I wrote a text-based RPG (hundreds of SUBs and about 8000 lines of BASIC code). I wanted more, so I taught myself C, then I figured C++ can't be that much different, and it's not. In fact, C++ is easier with I/O stream manipulation. And because it's Object-Oriented, you can use inheritence attributes to easily create classes customized to your application.

    GUI implementation of C++ using the Windows API sucks.

    If you're wanting to expand your horizons, I suggest learning Java, then Python. Why Python? Because of the WxPython library. Cross-Platform GUI scripting capability. Ease of portability without compiling (Python is a scripting language, not a programming language).

    Ruby and Eiffel are good to look at for the OO aspect as well if Java isn't your thing.
     
  16. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    #include <fstream>
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    using namespace std;

    bool ReadFromFile(int& nEmpNum, int& nDepartment, int& nHours, char& chExempt, double& dPayRate, ifstream& inFile)
    {
    if(!inFile.eof()){
    inFile >> nEmpNum >> nDepartment >> dPayRate >> chExempt >> nHours;
    return true;
    }else
    return false;
    }

    void WriteToFile(int& nEmpNum, int& nDepartment, int& nHours, char& chExempt, double& dPayRate, double& dOvertimePay, double& dBasePay, ofstream& outFile){

    outFile << nEmpNum << '\t' << nDepartment << '\t' << dPayRate << '\t' << chExempt << '\t' << nHours << '\t' << dBasePay << '\t' << dOvertimePay << '\t' << (dBasePay+dOvertimePay) << endl;

    return;
    }

    void CalculatePay(int& nHours, char& chExempt, double& dPayRate, double& dOvertimePay, double& dBasePay){

    if(chExempt == 'N'){
    dOvertimePay = nHours < 40 ? 0 : (nHours-40)*dPayRate;
    dBasePay = nHours > 40 ? 40*dPayRate : dPayRate * nHours;
    }else{
    dOvertimePay = 0;
    dBasePay = 40*dPayRate;
    }

    return;
    }


    int main(){

    int nEmpNum, nDepartment, nHours;
    char chExempt;
    double dPayRate, dBasePay, dOvertimePay;

    ifstream inFile("./data.txt");
    ofstream outFile("./output.txt");
    while (ReadFromFile(nEmpNum, nDepartment, nHours, chExempt, dPayRate, inFile)) {
    CalculatePay(nHours, chExempt, dPayRate, dOvertimePay, dBasePay);
    WriteToFile(nEmpNum, nDepartment, nHours, chExempt, dPayRate, dOvertimePay, dBasePay, outFile);
    }


    inFile.close();
    outFile.close();

    return 0;
    }

    __________________


    [dannenbm@csc-linux1 ~]$ g++ test.cpp
    test.cpp:1: error: expected constructor, destructor, or type conversion before '
    <' token
    /usr/lib/gcc/x86_64-redhat-linux/4.0.1/../../../../include/c++/4.0.1/x86_64-redh
    at-linux/bits/c++config.h:1381: error: expected namespace-name before ';' token
    /usr/lib/gcc/x86_64-redhat-linux/4.0.1/../../../../include/c++/4.0.1/x86_64-redh
    at-linux/bits/c++config.h:1381: error: '<type error>' is not a namespace
    test.cpp: In function 'bool ReadFromFile(int&, int&, int&, char&, double&, std::
    ifstream&)':
    test.cpp:9: error: invalid use of undefined type 'struct std::basic_ifstream<cha
    r, std::char_traits<char> >'
    /usr/lib/gcc/x86_64-redhat-linux/4.0.1/../../../../include/c++/4.0.1/iosfwd:89:
    error: declaration of 'struct std::basic_ifstream<char, std::char_traits<char> >
    '
    test.cpp:10: error: no match for 'operator>>' in 'inFile >> nEmpNum'
    test.cpp: In function 'void WriteToFile(int&, int&, int&, char&, double&, double
    &, double&, std::ofstream&)':
    test.cpp:18: error: no match for 'operator<<' in 'outFile << nEmpNum'
    test.cpp: In function 'int main()':
    test.cpp:43: error: variable 'std::ifstream inFile' has initializer but incomple
    te type
    test.cpp:44: error: variable 'std::ofstream outFile' has initializer but incompl
    ete type
    test.cpp: At global scope:
    test.cpp:57: error: expected constructor, destructor, or type conversion at end
    of input
     
  17. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    Do it for fun and you'll be alright. Above is my latest version (which is messy as hell) and the errors that g++ threw. I'm debugging this in between caffeine driven nervous breakdowns.
     
  18. newyorkin

    newyorkin 1 ton status

    Joined:
    May 8, 2001
    Posts:
    16,555
    Likes Received:
    157
    Location:
    Los Estados Unitos
    A part of me is actually kinda jealous that you can stay up all night and play with that stuff...
     
    Last edited: Nov 22, 2005
  19. mikey_d05

    mikey_d05 1 ton status GMOTM Winner

    Joined:
    Nov 23, 2004
    Posts:
    10,453
    Likes Received:
    0
    Location:
    Minnesota
    Pay for a one size fits all USPS envelope after December 15th and my textbook is yours.
     
  20. newyorkin

    newyorkin 1 ton status

    Joined:
    May 8, 2001
    Posts:
    16,555
    Likes Received:
    157
    Location:
    Los Estados Unitos
    :rotfl: I'm not that jealous... I actually have a couple from my brother, he gave them to me when he joined the Navy. They are sooooooooooo boring though....
     

Share This Page