![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It doesn't hurt to do some limited research outside the forum. The documentation covers constructors....
__________________
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 |
|
|
|
|
|
#22 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
I tried my main like this:
for(i=0; i<10; i++)
{
cout << "Please enter the following Employee information:" << endl;
cout << "\nEmployee ID No: ";
cin >> id;
cout << "\nEmployee Sex: ";
cin >> sex;
cout << "\nEmployee Wage: ";
cin >> wage;
cout << "\nYears with Company: ";
cin >> years;
Employee emp[i+1]=Employee(id,sex,wage,years);
}I get errors with the: Employee emp[i+1]=Employee(id,sex,wage,years); Just as an experiment, I took out the [i], and put a 1, just as if were creating one instance, and all I got was the Warning i had gotten a little while earlier, and it actually compiled. So the main problem is trying to create an instance for each of the 10 employees, and I really don't know how else to cycle through all of the inputs unless i actually hardcode for each instance, which seems unecessary, There has to be a way to loop through for each instance, and I know someone has to know how. And if anyone can think of a better way to go about this problem as a whole, i'm all for it. But can someone tell me, if we were to get the above to work, and each employee's info was stored in some sort of array or as an object, how could we sort just the years worked, and then get all of the corresponding info to sort with it? |
|
|
|
|
|
#23 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
if its done as an object, you could either have the year be public or make set/get methods for it, and then when you sort the objects based on the year value, they will be sorted correctly.
And for the indexing, use [i] instead of [i+1] or else you'll be reading into memory that most likey isn't yours. Also, dont put the type there, because you're not declaring a new variable (it needs to be declared outside of the loop, like before). |
|
|
|
|
|
#24 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Jimbo. Sounds like you are onto something. for the[i+1], i was using that as a naming for an instance of emp, it has nothing to do with the creation of an array. so instead of
Employee emp1 = employee(blah,blah,blah.blah) x 10 and one more quick issue w/ this: cout << "\nEmployee Sex: ";
cin >> sex[i];
while(sex[i] != 'M' || sex[i]!='m' || sex[i]!='F' || sex[i]!='f')
{
cout << "Must be M or F. Please retry: ";
cin >> sex[i];
}I didn't really think about it at first since getting the crap to initialize was a little more important, BUT, when it comes to sex, it can only be M, m, F, or f. My while() doens't work, it keeps asking for correct input regardless. Even if you input a correct character. What's wrong now??? C++ can't give me a break!!! Just to let everyone know though, i'm very gracious for everyone's input and opinions. It's amazing to know that there is someplace for myself to go with my problems in an area of study that i'm hoping to someday make a career out of. I love how much i can learn from other people, and in most cases, i get more from you guys and your experience than class itself..lol... Keep on truckin'! |
|
|
|
|
|
#25 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
Since you changed sex to a string, you have to use double quotes to compare it. IMHO, you would do just as well to leave it as a char, but that really comes down to your choice. (I did it here as a char for the first example, string for the second.)
int main()
{
int i,id, years;
char sex;
float wage;
Employee emp[10];
for(i=0; i<10; i++)
{
cout << "Please enter the following Employess information:" << endl;
cout << "\nEmployee ID No: ";
cin >> id;
do
{
cout << "\nEmployee Sex: ";
cin >> sex;
cin.clear();
sex = toupper(sex);
} while(sex != 'M' && sex != 'F');
cout << "\nEmployee Wage: ";
cin >> wage;
cout << "\nYears with Company: ";
cin >> years;
emp[i] = Employee(id, sex, wage, years);
}
system("PAUSE");
return 0;
}#include <vector>
int main()
{
int i,id, years;
string sex;
float wage;
std::vector<Employee> emp;
for(i=0; i<10; i++)
{
cout << "Please enter the following Employess information:" << endl;
cout << "\nEmployee ID No: ";
cin >> id;
do
{
cout << "\nEmployee Sex: ";
cin >> sex;
} while(sex != "M" && sex != "m" && sex != "F" && sex != "f");
cout << "\nEmployee Wage: ";
cin >> wage;
cout << "\nYears with Company: ";
cin >> years;
emp.push_back(Employee(id, sex, wage, years));
}
system("PAUSE");
return 0;
}[edit:] also, you dont want to be indexing the string (if you choose that route) when you compare it (see your code: sex[i]). |
|
|
|
|
|
#26 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Hi again,
I tried your main() and I got the following errors: invalid conversion from `char' to `const char* initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' These are associated with the line; emp[i] = Employee(id, sex, wage, years); But at lease now i see what you are trying to do. AND, on another note, When i was doing the comparision earlier to catch input for M or F, i had already changed it to char, and i was using the single quotes, i.e. 'M', but it still made me repeat the input over and over regardless had I input a legitimate value. |
|
|
|
|
|
#27 | |
|
Professional Programmer
|
Were you using the || operator? That would send you back to the loop every time because you can't select M and F. || checks to make sure the string is == to M AND F, use &&.
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#28 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I really think there are too many cooks in the kitchen, but I'm compelled to ask a simple question. Tell me what happens if the user inputs "Big Bucks" in answer to the wage question.
cout << "\nEmployee Wage: ";
cin >> wage;
cout << "\nYears with Company: ";
cin >> years;
emp[i] = Employee(id, sex, wage, years);
__________________
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 |
|
|
|
|
|
#29 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
big bucks into wage causes an explosion! But the && operator fixes the do while!
I guess i should do some check on wage to make sure it's numeric, eh. But I reazlly wish toupper() would work to make the code slightly more efficient. i'm looking at an example in my book and what i tried was the exact same thing the book has done. Weird.. |
|
|
|
|
|
#30 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Please forgive me for posting such a massive amount of code, but i'm in a horrible time crunch, and i tried to quickly come up with something that doesn't do the sort of years w/ company. This is supposed to write to a file. Except after you enter the info, instead of giving you the option to write to a file, the program stalls and fails. I'm clueless, it could be parentheses.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
using namespace std;
void descend_Sort(vector<int>, vector<int>);
int main()
{
int i,j,id[10];
vector<int> years;
char sex[10];
float wage[10];
for(i=0; i<1; i++)
{
cout << "Please enter the following Employee information:" << endl;
cout << "\nEmployee ID No: ";
cin >> id[i];
cout << "\nEmployee Sex (M or F): ";
cin >> sex[i];
while(sex[i] != 'M' && sex[i]!='m' && sex[i]!='F' && sex[i]!='f')
{
cout << "Must be M or F. Please retry: ";
cin >> sex[i];
}
cout << "\nEmployee Numeric Wage: ";
cin >> wage[i];
cout << "\nYears with Company: ";
cin >> years[i];
cout << endl;
}
//vector<int> yearscopy(years);
//descend_Sort(yearscopy, years);
cout << "You have entered:" <<endl;
cout <<"Employee ID " << "Employee Sex " << "Numeric Wage "
<< "Years with Company" << endl;
cout << "----------- " << "------------ " << "------------ "
<<"------------------" << endl;
for(j=0; j<1; j++)
{
cout << setw(11) << right << id[j] << " "
<< setw(12) << char(toupper(sex[j])) << " " << "$ " << setw(9) << fixed <<
setprecision(2) << right << wage[j] << " " << setw(18) <<
right << years[j] << endl;
}
cout << "Would you like to write the information to a file? This will overwrite the file."
<< "Would you like to continue? (y/n) : " ;
char response;
cin >> response;
while(response !='N' && response!='n' && response!='Y' && response!='y')
{
cout << "Must enter Y or N: ";
cin >> response;
}
if( response == 'Y' || response == 'y')
{
ofstream file;
file.open("testfile.txt");
if(file.fail())
{
cout << "The file was not succesfully opened." << endl;
}
else
{
file <<"Employee ID " << "Employee Sex " << "Numeric Wage "
<< "Years with Company" << endl;
file << "----------- " << "------------ " << "------------ "
<<"------------------" << endl;
for(j=0; j<1; j++)
{
file << setw(11) << right << id[j] << " "
<< setw(12) << char(toupper(sex[j])) << " " << "$ " << setw(9) << fixed <<
setprecision(2) << right << wage[j] << " " << setw(18) <<
right << years[j] << endl;
}
file.close();
}
cout << "The file has been written successfully." << endl;
}
else
{
cout << "The file has not been written." << endl;
}
system("PAUSE");
return 0;
}PLEASE FORGIVE ME FOR THE AMOUNT. It's the best i could do. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|