How to program inputs for these?

Enter person's name (last,first): Smith,John
Enter January income: 3560.39
Enter February income : 3557.84
Enter March income : 2788.43
Quarterly statement of earnings
First name : John Last name: Smith
=======================================================================
January income February income March income
3560.39 3557.84 2788.43
=======================================================================
Total gross income Income tax Total net income
9906.66 1486.00 8420.66


So, the bold letters are inputs and everything else should be output. And here are the hints that are given to me, but i'm having trouble inputting the names with commas, and showing names separately on output.
- Input the names as a string using cin and the >> operator, formatted as lastname,firstname - making sure there are no spaces anywhere in the string .
- you will need to use find() and substr() string member functions to extract first and last names from the full name.
- You will need to use setw() to align the items correctly, as in the examples below.
- You will need to use setprecision() and fixed to correctly print all numerical values.

Thank you, I will greatly appreciate it if you can help.
try
Need to use find() and substr() and must use >> per the assignment?!

Sorry, I got overexcited. Aside from that, do see what you can come up with. We tend to avoid giving either complete solutions or code to start with, but if you're REALLY stuck, I might consider giving you some psuedocode to work with.

Even though I laughed at hints 1 and 2, they're really not bad to start with, and I really shouldn't have, as probably the problem is to teach you how to use strings, which are very useful. "Hint" 1 explains how your should read the names with the comma out from the stream, and hint 2 hints at how you should get rid of the comma. Remember: stringinstance.find() and stringinstance.substr() will help you tremendously here.
http://cplusplus.com/reference/string/string/find/
http://cplusplus.com/reference/string/string/substr/

-Albatross
Last edited on
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main() {
    string name  = "";
    string lname = "";
    string fname = "";

    cout << "Enter person's name (last,first):";
    getline(cin,name);
    lname = name.substr(0,name.find(","));
    fname = name.substr(name.find(",")+1,name.size()-1);

    cout << "Quarterly statement of earnings:\n";
    cout << "First Name : " << fname << " Last Name: " << lname << "\n";

    return 0;
}


http://codewall.blogspot.com
Last edited on
thecodewall,

First, this is not the solution to the OP's problem. Reread the problem carefully, and please do not post the solution when you get it. I'll explain why below, but please keep in mind that solving problems for someone doesn't help them nearly as much as giving helpful pointers does.

linoch,

In case you're wondering why I asked thecodewall to not give you the solution is not because I'm a warden who makes sure that someone follows the rules and solves the homework problem on their own (which I totally am, but come on...) but because if you do the homework problem yourself, you'll benefit FAR more than if someone else does it for you. :)

-Albatross
Last edited on
I'm actually new to C++ and I had no idea how to use substr() and find() because they were not on my textbook, so I needed some guidance and i thought i'm a person who learns more by looking at examples like what thecodewall provided above. But, i'll be more cautious in the future. Thanks for the advice Albatross :)
@Albatross

Thanks for that, actually when you examine my code is not totally finish or what exactly linoch want. We must also know that fastest way to learn is thru example. Thats why in our kinder garden year our teacher always have a big picture of something or teaching as thru images. Do you think a newbie wants to read all the pages in the book to learn substr or find or simply copy the code and run thru it self and later study why this code process.

http://codewall.blogspot.com
Last edited on
I see your point, thecodewall, but... um... I'm not really sure if I agree with it. While teaching by example is fast and it usually works, giving something that's similar to the solution to a problem could confuse the OP and he/she might run off with the solution without studying it (no offense intended, linoch, this is just a possibility). While your intentions were good, I'm sure, it would have been more useful if you posted a related example and explicitly stated that this was an example. :)

-Albatross
@thecodewall: Yes, but the thing is, you didn't post an example, you posted the solution.
Actually, firedraco, technically, he didn't post the solution. That's the thing, what he gave was a partial solution which could be somewhat confusing....
The prof wrote:
- You will need to use setw() to align the items correctly, as in the examples below.
- You will need to use setprecision() and fixed to correctly print all numerical values.


:\

-Albatross
Last edited on
Topic archived. No new replies allowed.