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.
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.
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; }
Spoilers for those who don't feel like running the code, it simply prints out:
Lol! XDCode:I get others to do my homework
I wanted to post some over-engineered example using back_inserter iterators but then I got lazy :P
Bah!
He'll never hand it in now
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.
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?Code:std::istream_iterator<char>{std::cin >> std::noskipws}
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.
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
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.
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.
I'm not sure if you're just asking how what I suggested would work, or something else.
I suppose you could do this:
I'm not suggesting this is the only way or something, either.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; }
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.
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
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.