Announcement

Collapse
No announcement yet.

C++ Help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ Help

    I have a little problem in my C++ class. We have a HW assignment that requires us to find the first and last digit in a number like 56789.

    The number comes to the program from an input file, but I was wondering if the first and last digits must be found in the file or can they be computed once the number is retrieved from the input file.

    I need as muc help as I can get, so any help will be appreciated.



    I'll post this in Sandbox too, to try and get more views.

    THANKS!!!


  • #2
    Re: C++ Help

    Originally posted by .143 Beth© View Post

    The number comes to the program from an input file, but I was wondering if the first and last digits must be found in the file or can they be computed once the number is retrieved from the input file.
    huh? maybe rephrase that?

    Comment


    • #3
      Re: C++ Help

      Originally posted by .143 Beth© View Post
      The number comes to the program from an input file, but I was wondering if the first and last digits must be found in the file or can they be computed once the number is retrieved from the input file.!!!
      Isn't that something to ask the instructor?

      It really depends on how they want you to approach it, but you really could do either.

      It doesnt make much sense to read the data from a file and then process it a second time to find the first and last digit. I suggest you just do it when you read the file.
      Steam Community: | |

      Comment


      • #4
        Re: C++ Help

        I wasn't as clear as I should have been.

        Here is a better explanation of the problem:

        I have to write a program that computes the raises of employees at a company. Although the means to determine the raises are improbable they give the students intersting ideas to encode in C++.
        The raise amount will be determined by the employee code.
        The employee codes are read from an input file that have 4 items on each line of input
        1)Name
        2)year hired
        3)salary
        4) employee code
        (Example employee codes: 565656, 12345, 54321, 99, 054670)
        If the employee code ends with 0,1,2,3; then add $500 to the salary.
        If the employee code ends with 4,5,6; then add $1000 to the slalary.
        If the employee code ends with 7,8,9; then add $2000 to the salary.
        If the first digit of the employee code is 0-4 then multiply the salary by 2.
        If the first digit of the employee code is 5-9 then multiply the salary by 3.

        I then have to create an output file and store the new salaries on it.

        I know how to do everything for the program but read and isolate the first and last digits of the employee code.

        Comment


        • #5
          Re: C++ Help

          cast the int to a string. Then you can deal with the number one character at a time. I don't use C++ much but every language has built in functions to work with strings. I'm sure there is a function to find the length of a string and then you can just pull the first and last character.

          Comment


          • #6
            Re: C++ Help

            This really depends on what file access library is being used, but it is a simple parsing exercise.

            Considering that I don't even know what topics you have covered, it is hard to help, but I would:
            - make an employee record struct with the 4 fields, all strings
            - make a list or an array out of that struct for all of the employees

            Now if you are doing OO (as you should be for stuff like this), then that struct would be an object, and it would have a constructor which took a line of text to initialize the fields. Then your whole file would be a list of objects.

            Create a GiveRaise member function which does the little calculation based on the number. The salary field will have to be converted to an int and back to add the raise. Create a GetText member function which returns the contents of the struct in the correct format.

            You then
            - read a line
            - create an employee record object
            - until file complete

            - for each ER
            - call GiveRaise
            - call GetText
            - write text line to file


            Any other homework you need done? :p
            Peace through fear... since 1947!

            Comment


            • #7
              Re: C++ Help

              Arrays look like the way to go for this one.

              Comment


              • #8
                Re: C++ Help

                I think BHack's solution is best. The steps are as follows:

                1. Read the number as ascii characters into an array. Keep a int counter (call it i, don't forget to initialize) to mark your position as you write characters.
                2. Do a test against array[0] and array[i] for your first and last character values in the emp#. You may need to test against array[i-1] depending on where you increment i.
                3. Create an int called action (or choice, salaryMod, etc), and assign it a value based on your tests against the first and last value.
                4. Switch on action to perform the correct salary calculations, as outlined in the assignment.
                Code:
                switch(action){ //case 1, case 2, etc };
                Turn the above instructions into code, and bob's your uncle.

                For help with the actual code, I recommend you bookmark and use this site frequently: http://www.cplusplus.com/

                Also, if you take the time to learn the Standard Template Library earlier rather than later in your school career, you will save yourself major headaches...assuming you're in a comp sci program.

                [drill][medic][conduct][tg-c1][tpf-c1]
                [ma-c2][taw-c1]

                Principles of good Sandbox Etiquette:
                Assume good faith - Be polite, please! - Work toward agreement. - Argue facts, not personalities. - Concede a point when you have no response to it, or admit when you disagree based on intuition or taste. - Be civil. - Be prepared to apologize. In animated discussions, we often say things we later wish we hadn't. Say so. - Forgive and forget. - Recognize your own biases and keep them in check. - Give praise when due.

                Treat others as you would have them treat you

                Comment


                • #9
                  Re: C++ Help

                  Oh noes! A programming thing I might actually be able to help with!

                  Too bad it looks like the questions already been answered, though. Plus, Icky's trying to show off with all his OOP, structure things, and functions. Couldn't just let him off with the simple answer, could you?

                  So, OOP yours, Icky! :madsmile:

                  P.S. - If that's a bit too vulgar, a moderator is free to remove that last line :D

                  |TG-Irr| ThePenguineer ::: |TG-Irr| Pengie the Noob
                  TG-Irregulars Commanding Officer
                  ||

                  Comment


                  • #10
                    Re: C++ Help

                    Create a type EmployeeRecord with the atributes
                    Name(String)
                    year hired(Date)
                    salary(Double)
                    employee_code (String)
                    Salary raise(Double)

                    Note that you should use String for the employee code because the leading zero is significant for the salarycheck.
                    Use one of the FileReading libs and read one and one line into a String each time iterating over the String with a StringTokenizer(delimited with ",").
                    Create one employee entity pr line from the parsing.
                    Add all the Empolyee(s) to a vector wich you later Iterate over and compute the raise in a
                    void computeSalary(Empolyee employee) method(add the information to the Object in that method or let it return the value to the field of the employee)

                    A hint on this one is to first check the first digit by getting it from the employee_code and casting it to an int(call it firstDigit) then just get the last digit by getting the char at

                    yourString.substring(yourString.length-1, yourString.length) and cast it like the first one. The math should be easy.

                    Good luck :)
                    --
                    VI VI VI - the number of the beast

                    Comment


                    • #11
                      Re: C++ Help

                      Given that the items in the file are strings, it doesn't make sense to do the extra work to convert everything. The only thing you need to look at as an INT is the salary, and even that is only for the calculation.

                      And... I'm not trying to show off anything. If it is an OO course, then you would look like an idiot using a structure and some disconnected functions. A little prog like this begs for an OO approach.
                      Peace through fear... since 1947!

                      Comment


                      • #12
                        Re: C++ Help

                        I can't figure this out, we have only gotten to modular programming in our book, and I don't think the teacher explained this part too well.

                        So far I have every calculation done, they all depend on the first digit, and the last digit of the employee code. I read and stored the employee code in an int called EmployeeCode and the outputs that control the calcualtions are int values BeginDigit and EndDigit
                        ill append the source file to this post so you guys can take a look!!!

                        The big empty space in the file is where i would have to insert that code to find the first and last digit , the endigit in the code is still set to 7 for test purposes, in the final, it would be set to begin at 0
                        Attached Files

                        Comment


                        • #13
                          Re: C++ Help

                          I figured it out. I have all of your methods beat.

                          int EmployeeCode;
                          InputFile >> EmployeeCode;

                          LastDigit=EmployeeCode%10;

                          Example: code# 545454, LAstDigit= 4 etc....

                          I want to slap myself in the face

                          Comment


                          • #14
                            Re: C++ Help

                            Ah good, that helps frame the problem. :)

                            - read the employee number as a string, stringname
                            - access the first char of the string with stringname[0]
                            - access the last char with stringname[stringname.size() - 1]
                            (not sure if there's any way to get a 0 length string and break everything... check into it)
                            Peace through fear... since 1947!

                            Comment


                            • #15
                              Re: C++ Help

                              Originally posted by .143 Beth© View Post
                              I figured it out. I have all of your methods beat.

                              int EmployeeCode;
                              InputFile >> EmployeeCode;

                              LastDigit=EmployeeCode%10;

                              Example: code# 545454, LAstDigit= 4 etc....

                              I want to slap myself in the face
                              You still need to isolate the first digit from the emp#, and you can't read emp#s as an int without losing the leading zeros. That's why I still recommend going with BHack's solution. Because the requirement to be able to read leading zeros locks you into reading your numbers as chars, simply testing against array[i] (or array[i-1], as the case may be) is also the easiest way to read the last digit.

                              For clarity's sake, let's assume that you read in the emp# as an int, which you would have to do in order to use the mod operator. The data is mutated as follows:

                              Test Case 1: leading digit is non zero
                              File: 12345 (expected result is 1)
                              (gets read and stored in int x)
                              x: 12345
                              (perform some calc. to isolate most significant digit)
                              result: 1

                              Test Case 2: leading digit is zero
                              File: 01234 (expected result is 0)
                              (gets read and stored in int x)
                              x: 1234
                              (perform some calc. to isolate most significant digit)
                              result: 1

                              As you can see, the first test case is the expected and correct answer. But the second case is incorrect, because it drops the leading zero when handling integer values (since 01 = 1). The easiest way around this is to read the emp# from the file as an array of chars

                              [drill][medic][conduct][tg-c1][tpf-c1]
                              [ma-c2][taw-c1]

                              Principles of good Sandbox Etiquette:
                              Assume good faith - Be polite, please! - Work toward agreement. - Argue facts, not personalities. - Concede a point when you have no response to it, or admit when you disagree based on intuition or taste. - Be civil. - Be prepared to apologize. In animated discussions, we often say things we later wish we hadn't. Say so. - Forgive and forget. - Recognize your own biases and keep them in check. - Give praise when due.

                              Treat others as you would have them treat you

                              Comment

                              Connect

                              Collapse

                              TeamSpeak 3 Server

                              Collapse

                              Advertisement

                              Collapse

                              Twitter Feed

                              Collapse

                              Working...
                              X