Thread: I really need this extra credit but don't know how to do it please help?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    5

    I really need this extra credit but don't know how to do it please help?

    how can i write a program to prompt the user to enter weekly hours worked by three employees and the hourly hour (that is the same for all of them) and display the amount earned. It must use an array to store the hours worked.


  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Here's one way of doing it. Ready to hand in for you....
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	char entry[] = { 0x49,0x20,0x67,
    		0x65,0x74,0x20,0x6f,0x74,0x68,0x65,
    		0x72,0x73,0x20,0x74,0x6f,0x20,0x64,
    		0x6f,0x20,0x6d,0x79,0x20,0x68,0x6f,
    		0x6d,0x65,0x77,0x6f,0x72,0x6b };
    	for (int i = 0; i< sizeof(entry);i++)
    	{
    		cout << entry[i];
    	}
    	return 0;
    }

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Spoilers for those who don't feel like running the code, it simply prints out:
    Code:
    I get others to do my homework
    Lol! XD

    I wanted to post some over-engineered example using back_inserter iterators but then I got lazy :P

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Bah!

    He'll never hand it in now

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You know what's funny, I don't know why but when I read the question I thought it wanted to ask for the 3 names of the employees as well. So I figured I'd try to be tongue-in-cheek and over-engineer a solution that couldn't have possibly come from a student. :P

    I was going to do the rest but then I got bored lol.

    Here's what I came up with:
    Code:
    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <string>
    
    
    int main(void)
    {
      int const num_expected_employees = 3;
      
      std::vector<std::string> employees;
      employees.reserve(num_expected_employees);
    
    
      for (int i = 0; i < num_expected_employees; ++i) {
        std::cout << "Getting name " << i << "\n";
        std::cin.clear();
    
    
        employees.push_back(
          std::move(
            std::string{std::istream_iterator<char>{std::cin >> std::noskipws}, {}}
          )
        );
      }
    
    
      for (auto const& e : employees) {
        std::cout << e << "\n";
      }
    
    
      return 0;
    }
    Last edited by MutantJohn; 12-06-2016 at 10:11 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    std::istream_iterator<char>{std::cin >> std::noskipws}
    It's particularly a mean trick you pulled, because, wouldn't using noskipws mean you have to set a field width? Otherwise what stops this from slurping ALL of the input and not just names?

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You just use the EOF character. This way, you can enter names like `Punished "Venom" Snake` and it'll store that correctly. I've also tried using setw on that but it doesn't seem to work.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I see, that's odd, it works for C strings.

    It's for fun sure but I've always found approaches like this brittle, though. :P

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Brittle in what ways? The only flaw I know right off the bat is that it copies an arbitrary amount of characters. setw isn't working so I guess the only real approach is to simply resize each string in the vector and then use copy_n from the istream iterator pair.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well I mean from a user-friendly standpoint, I think there are nicer options. This is brittle because the user has to know the magic keys you want them to press instead of something else like "Here's a name, parse it loser". I don't personally choose to rely on EOF outside of file processing.

  11. #11
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Would you mind posting your version?

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm not sure if you're just asking how what I suggested would work, or something else.

    I suppose you could do this:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <cctype>
    using namespace std;
    
    int main() {
        const int ExpectedEmployees = 3;
        vector<string> emps(ExpectedEmployees);
        for (string& s : emps) {
            char delim;
            if ((delim = cin.peek()) == '\"' || delim == '\'') {
                cin.ignore();
                getline(cin, s, delim);
                if (isspace(cin.peek())) {
                    cin.ignore();
                }
            }
            else {
                getline(cin, s);
            }
        }
        
        for (string& s : emps) {
            cout << s << endl;
        }
        return 0;
    }
    I'm not suggesting this is the only way or something, either.

  13. #13
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    getline actually is a bit more sane than just reading from the input stream XD

    It automatically breaks on the newline character as a default which is nice.

  14. #14
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    how can i make the program ask the user to Please enter the weekly hours worked by
    3employees that earn the same hourly
    rate and will calculate their amount earned.

    then to do this : Please enter the hourly pay rate for all three employees: 20
    Here is the gross pay for each employee:

    and it wil calculate the numbers


    Please enter the weekly hours worked by
    3employees that earn the same hourly
    rate and I will calculate their amount earned.
    Employee #1: 12
    Employee #2: 13
    Employee #3: 14
    Please enter the hourly pay rate for all three employees: 20
    Here is the gross pay for each employee:
    Employee #1: $240.00
    Employee #2: $260.00
    Employee #3: $280.00

  15. #15
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    You just don't get it do you. Give up school, you're wasting your money if you are paying for it, go to your local McDonalds or similar and beg for a burger flipping job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zed Shaw Learn C The Hard Way - Extra Credit 15.3
    By Fauveboy in forum C Programming
    Replies: 12
    Last Post: 09-13-2014, 01:26 PM
  2. Replies: 4
    Last Post: 08-25-2014, 02:48 AM
  3. Help - Car Rite Rental (extra credit)
    By mmourot in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2011, 07:00 AM
  4. Credit Reports
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-10-2003, 12:53 AM
  5. Please help me with this Extra Credit program
    By DenisGFX in forum C Programming
    Replies: 10
    Last Post: 05-19-2002, 01:48 AM

Tags for this Thread