![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
C++ exercise - need ideas of how to go about it.
Hey everyone,
I'm supposed to complete the following exercise: "Write a C++ program that permits a user to enter the following information about your small company's 10 employees, sorts the information in decreasing value by years with the company, and writes the following sorted information to a file: ID No. Sex (M/F) Hourly Wage Years w/ Company ." After doing that, i'm supposed to "Write another program that allows you to read the file created and changed the hourly wage or years for an employee, and create a new, updated file." Well, to be honest i'm completely stumped as to where to begin. My first problem is that i'm not sure how to store 10 different entries, where each entry contains 4 pieces of information of different data types. Should I use an array, vector, or string? Should I store all of the information as one element in a 10 element array? Do I use an array or arrays? And regardless of how i store it, how can you sort it by years w/ company? I know there are sort functions, but they usually sort elements in "ascending" order. And that applies to single elements in ONE array or string or vector. Well, if i ever actually get something to work for the first part, i'm not sure i can really think of a way to change the wage or years, based on a read of the file created in the first part. I would say you would have to read the file line by line, store each line as an array w/ each piece of info as an element and alter it that way. But then again, i'm not totally sure. SO... my main issue is how to store 4 pieces of information for 10 people, 40 pieces of info in all, and sort it in descending order based on years with the company when i don't see how a sort function could work. Anyone have an ideas at all, I could surely use them!!! THANK YOU VERY VERY VERY VERY MUCH FOR ANY HELP AT ALL!!!! |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 766
Rep Power: 3
![]() |
What I would do:
1. make an Employee class 2. define input and output operators (istream& operator>> and ostream& operator<<) (assuming you're using streams for I/O). 3. write some comparison operators (bool operator{<,<=,>,>=,==,!=}) for sorting (you probably wouldn't need them all, but you might want 'em) which would probably just compare the yearsWithCompany for each Employee object 4. after you have all of the data, sort it. 5. write it out to the file (using operator<<). for the 2nd part, use the opeartor>> to read them all into a container (again, a std::vector would probably be fine), then find the one you want (I'm guessing you'd search by name), and set the new data fields. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Make your own object to represent the employee. Store the set as a vector, list, or whatever flips your skirt up. Members would be strings, say, for names, integers or doubles for years with (depending up fractional usage), etc. Try some code, post your problems.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
This is a start. But i'm already getting errors using this:
class Employee
{
private:
int id, years;
char sex;
float wage;
public:
Employee(int, char, float, int);
void makeString();
};
Employee :: Employee(int in_id, char in_sex, float in_wage, int in_years)
{
id=in_id;
sex=toupper(in_sex);
wage=in_wage;
years=in_years;
}
void Employee::makeString()
{
string profile;
profile[0]=id;
profile[1]=sex;
profile[2]=wage;
profile[3]=years;
}Is this going in the right direction? How can I have a string if the data entries are of different types? Better yet, is this exercise possible WITHOUT classes. Coming from C to C++, i've yet to embrace the class, and i figured it would be better to NOT compound my confusion of trying to sort this all out using them. The exercise doesn't state it has to be done with classes, and we haven't really used classes in a while. On that note, I was wondering if I stored each piece of information into its own array, as in an array for id numbers, array for wage... etc, so as to having each piece of relevant info for each employee having the same index value. As in id[0] corresponds to wage[0]. Now, i'm thinking i can sort the years with a function, and do a reverse on that to get the descending order, but i need a way to hzve the corresponding index values of the array entries to match up. If i can get that, i think this first part will be good to go. SO, any ideas on how to make the index values match up if i sort the Years???? Last edited by codylee270; Apr 8th, 2006 at 6:55 PM. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
A C++ class (or structure) is like a C struct on steroids. That is, it may have functions (methods) as members, as well as variables. If you recall your C structure, you'll recall that it's a programmer-defined type that can hold variables of differing types (strings, integers, etc.). This gives you the opportunity to aggregate a definition in one place and use it repeatedly. If you move one, every attribute (name, age...) automatically moves with it. Sort on any attribute you like. Think of a C struct with the personal information and an array of those structs representing the entire collection. Nothing daunting there. Same with a class in C++. If you don't want to worry about overloading I/O operators and so forth, forget that part; just use it as a struct for organizational purposes. A collection of arrays will work, of course, but it's doing things the silly way. Of course, only you can decide whether or not you want to advance.
It's not very productive to come here and say, "ERRORS, OMG !!!!!111", without saying what kind of errors you're getting; it requires your respondents to examine your code in detail and possibly compile and run it when they might well solve your problem in 7.5 seconds flat if you only describe it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
So what else would that class need to solve this problem?
|
|
|
|
|
|
#7 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Quote:
I would suggest, without studying your code in detail, that you might want to store the gender as a string, whether C- or C++-type. You'll have to manipulate it as a single byte, without being able to resort to string functions, if you don't. Again, we'd prefer to answer your questions the easiest way, without resorting to detailed study of the code, and you're still being stingy with information.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#8 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
If you tell me what i'm being stingy with, I'll gladly divulge all secrets!..lol...
|
|
|
|
|
|
#9 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
class Employee
{
private:
int id, years;
string sex;
float wage;
public:
Employee(int, string, float, int);
void makeString();
};
Employee :: Employee(int in_id, string in_sex, float in_wage, int in_years)
{
id=in_id;
sex=toupper(in_sex);
wage=in_wage;
years=in_years;
}
void Employee::makeString()
{
string profile;
profile[0]=id;
profile[1]=sex;
profile[2]=wage;
profile[3]=years;
}
int main()
{
Employee emp1(0002,m,25,10);
emp1.makeString();
system("PAUSE");
return 0;
}I was trying to test it but i got a crap load of errors. I also changed char data type to string. Errors: In constructor `Employee::Employee(int, std::string, float, int)': no matching function for call to `toupper(std::string&)' candidates are: int toupper(int) . In member function `void Employee::makeString()': cannot convert `std::string' to `char' in assignment . [Warning] converting to `char' from `float' . In function `int main()': `m' undeclared (first use this function) . (Each undeclared identifier is reported only once for each function it appears in.) |
|
|
|
|
|
#10 | |
|
Professional Programmer
|
Bold code has been changed. Read the comments, they might help you understand
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
class Employee
{
private:
int id, years;
string sex;
float wage;
public:
Employee(int, string, float, int);
void makeString();
};
Employee :: Employee(int in_id, string in_sex, float in_wage, int in_years)
{
id=in_id;
sex=toupper(1); // I'm not that familiar with toupper, but it requires an integer, not string. I think it might be for changing the amount of bytes in the string. sex.size() might work
wage=in_wage;
years=in_years;
}
void Employee::makeString()
{
string profile[4]; // This wasn't an array originally
profile[0]=id;
profile[1]=sex;
profile[2]=wage;
profile[3]=years;
}
int main()
{
Employee emp1(0002,"m",25,10); // m is not a variable, it is a string
emp1.makeString();
system("PAUSE");
return 0;
}
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|