![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#31 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Try to indent a bit better next time.
Your problem was: cin >> years[i]; Solved it like: #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];
int yearsworked;
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 >> yearsworked;
years.push_back(yearsworked);
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;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#32 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'll have a look at your code. Meanwhile, let me repeat this for the umpty-umpth time: You can't control your user. He/she may provide invalid input erroneously (typos), through lack of training or familiarity, maliciously (knowing how to break your code and delighting in doing so), or accidentally (falling asleep with forehead on keyboard). It is your job to control this. You may choose to terminate the application or chew the user a new asshole or whatever, but you MUST choose. Most novices do not realize they need to do this. Once they have been informed, failing to uphold their responsibility to take these measures can only be attributed to schlockiness or recalcitrant obstreperousness. The people who wrote the functions did not promise that the functions would succeed. They promised to succeed OR NOTIFY THE USER OF FAILURE. They don't volunteer the information. You have to ask. The documentation will tell you how to ask. Failing to do your job properly can result in poor code. It can also result in civil or criminal liability. Quite a range, eh?
EDIT: I see Ruben has responded. If there are additional problems, post back.
__________________
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 |
|
|
|
|
|
#33 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Dawei is quite right about the following, note that in your code I did not fix such issues, possibly only made it worse. You can search the forums on how to do this but it's as DaWei says your job to do it.
Quote:
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#34 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The opinion I put forth is sort of a hot-button issue with me. Designing medical equipment tends to emphasize its importance. If one decides not to program such things robustly, I sincerely hope they don't design such equipment so long as I may need to use medical facilities. Oh, stay away from automotive control equipment also, please.
__________________
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 |
|
|
|
|
|
#35 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Thank you very much nnxion, that fixed it. I'm rushing b/c i have so much crap to do and so little time, and doing all this while trying to move into a new apartment. Please forgive me for some sloppiness of the code. And Dawei, i totally agree with you on the bulletproofing the input. And i'm not trying to be rude, because I look forward to your input with every post i make, b/c you shed light on the full spectrum, and it's overly helpful to me. Just for the pure simplicity of the program though, and the fact its for C++ class, and mainly b/c i am TOTALLY OVERWHELMED with things i have to do, as in by the end of this day Sunday!!, i chose only to make sure that required things, such as the m or f, or the y or n, be accounted for. But don't get me wrong, I completely understand what you mean, and had this been for an actual customer or company, I would have taken as many measures as possible to account for input. So please don't look at this as me being an ignorant ass, only as myself being a stressed-out college student with no time for anything at all anymore. And to be honest, if it wasn't for this forum, I wouldn't have done as well in my past two programming classes, and i've learned quite a bit from everyone here, especially those who have helped on this thread. So again, I certainly appreciate it. I wish i could have done this differently as to make it easier for me to actually be able to sort by years w/ the company. My plan was to use the vector, sort it, reverse it, then use a search using the descending ordered years with company list, and find how the new indexes compare old indexes, and then transfer that over to the other corresponding pieces of info, but that could be a whole other thread in itself. As for this though, It works well enough as it is, and it saves to the file wonderfully.
Once again, thank you everyone! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|