![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#31 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Cody, if you were a pain in the ass to me, I wouldn't give you one second of time beyond that required to whop you with a verbal 2 x 4. I'll have a look. Without looking, I'll tell you that different 'find' methods return different things. The one you're using probably returns an iterator, not a position. It's fairly common.
__________________
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 |
|
|
|
|
|
#32 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
It's supposed to find the first occurence of the search value within the given range. Did I do something wrong?
|
|
|
|
|
|
#33 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Your crash is probably in this line:
years[i] = atoi(line.substr(s3+1, line.length()-(s3+1)).c_str()); The find function you are using is returning a vector<int>::iterator. This is probably what you want, depending on what you want the offset for. If you need to find the offset (to access your other arrays), you can subtract id.begin() from the iterator. |
|
|
|
|
|
#34 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
i did fix the years array, but it wasn't giving me an error, but i'm sure it would eventually...lol.. And subtracting id.begin() did get rid of the error. But a few still remain:
years.erase[offset]; invalid types `<unknown type>[int]' for array subscript years.insert(holder , offset); no matching function for call to `std::vector<int, std::allocator<int> >::insert(int&, int&)' and the compiler gives me "notes": candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>] void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>] |
|
|
|
|
|
#35 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
I can't see the erase code in the previous code you posted. What are you trying to erase for? If you just want to update a value that your already have in the vector, you can use the [] operator
years[offset] = holder; If you do need to erase, and you need to get the iterator back again, just reverse the way you got the offset from the iterator. i.e. add offset to years.begin(). |
|
|
|
|
|
#36 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
I was confused, i thought vectors required more work to update an element since they were dynamic. Anyhoo, I fixed that as you said, and it compiles! It even reads in the file, but it doesn't quite display properly, and then it crashes again.
Here's the display. I just rapidly typed in crap for testing purposes: Employee ID Employee Sex Numeric Wage Years with Company
----------- ------------ ------------ ------------------
21231 M 231 32131 $ 0.00This is what is in the file: 21231 M 231 32131 1321321 F 1321 1321 It doesn't format something right, and then it blows up. For good, measure, here is the entire program, in its entirety: #include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main()
{
int i=0, j=0, holder, offset;
vector<int> years;
vector<int> id;
string sex[10];
float wage[10];
int yearsworked, selection;
ifstream infile;
ofstream outfile;
string disp1, disp2, filename, line;
char selection2, selection3, selection4;
// ----------------- Begin program body ----------------------------------
cout << "Please input the name of the file you wish to open: " ;
getline(cin, filename);
bool found;
infile.open(filename.c_str());
if(infile.fail())
{
cout <<"\nThe file was not successfully opened." << endl;
exit(1);
}
//Parsing
while(getline(infile,line))
{
int s1 = line.find(" ");
int s2 = line.find(" ");
int s3 = line.find(" ");
id.push_back(atoi(line.substr(0, s1).c_str()));
sex[i] = line.substr(s1+1, s2 - (s1+1)).c_str();
wage[i] = atof(line.substr(s2+1, s3 - (s2+1)).c_str());
id.push_back(atoi(line.substr(s3+1, line.length()-(s3+1)).c_str()));
i++;
}
infile.close();
cout << "The file contained the following:" << endl << endl;
cout <<"Employee ID " << "Employee Sex " << "Numeric Wage "
<< "Years with Company" << endl;
cout << "----------- " << "------------ "
<< "------------ " << "------------------" << endl;
for(j=0; j<2; j++)
{
cout << setw(11) << right << id[j] << " " << setw(12) << sex[j] << " " << "$ "
<< setw(9) << fixed << setprecision(2) << right << wage[j] << " "
<< setw(18) << right << years[j] << endl;
}
vector<int> idcopy(id);
sort(idcopy.begin(), idcopy.end());
more: //for goto
cout << "\nTo change the hourly wage or years with the company ";
cout << "for an employee, please enter the ID # from above: ";
cin >> selection;
cin.ignore();
found = binary_search(idcopy.begin(),idcopy.end(), selection);
while(!found)
{
cout << "\nThe Employee ID No: " << selection <<" was not found."
<< "\nPlease select another from the list provided:" ;
cin >> selection;
cin.ignore();
found = binary_search(idcopy.begin(),idcopy.end(), selection);
}
cout <<"\nEmployee ID " << "Employee Sex " << "Numeric Wage "
<< "Years with Company" << endl;
cout << "----------- " << "------------ "
<< "------------ " << "------------------" << endl;
offset = find(id.begin(),id.end(), selection)-id.begin();
cout << setw(11) << right << id[offset] << " " << setw(12) << sex[offset] << " " << "$ "
<< setw(9) << fixed << setprecision(2) << right << wage[offset] << " "
<< setw(18) << right << years[offset] << endl;
cout << "\nEnter 'W' to change wage or enter 'Y' to change years with the company.";
cout << "\nEnter 'X' to exit the program: ";
cin >> selection2;
cin.ignore();
while((selection2 != 'W') && (selection2 != 'w') && (selection2 != 'Y') && (selection2 != 'y') && (selection2 != 'X') && (selection2 != 'x'))
{
cout << "\nYou must enter 'W' or 'G'. Enter 'X' to exit: ";
cin >> selection2;
cin.ignore();
}
if(selection2 == 'X' || selection2 == 'x')
{
exit(1);
}
if(selection2 == 'W' || selection2 == 'w')
{
cout << "\nEnter new wage: ";
cin >> wage[offset];
cin.ignore();
}
else
{
cout << "\nEnter new Years with the company: ";
cin >> holder;
cin.ignore();
years[offset]=holder;
}
cout << "\nTo make more changed enter 'Y'. If not, enter 'N' : ";
cin >> selection3;
cin.ignore();
while((selection3 !='N') && (selection3!='n') && (selection3!='Y') && (selection3!='y'))
{
cout << endl << "Please retry using 'Y' or 'N': ";
cin >> selection3;
cin.ignore();
}
if(selection3 == 'Y' || selection3 == 'y')
{
goto more;
}
cout << "\nTo save change made to " << filename <<", Enter 'Y'." << endl
<< "To exit without saving changes, enter 'N' :";
cin >> selection4;
cin.ignore();
while((selection4 !='N') && (selection4!='n') && (selection4!='Y') && (selection4!='y'))
{
cout << endl << "Please retry using 'Y' or 'N': ";
cin >> selection4;
cin.ignore();
}
if(selection4 == 'Y' || selection4 == 'y')
{
outfile.open(filename.c_str());
if(outfile.fail())
{
cout <<"\nThe file was not successfully saved." << endl;
exit(1);
}
else
{
for(j=0; j<2; j++)
{
outfile << id[j] << " " << sex[j] << " " << wage[j] << " " << years[j] << endl;
}
outfile.close();
}
cout << "\nThe file has been written successfully." << endl;
}
else
{
cout << "The file has not been saved." << endl;
}
system("PAUSE");
return 0;
}Almost there guys. Thanks for all of your help! |
|
|
|
|
|
#37 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
OK, two problems:
1. int s1 = line.find(" ");
int s2 = line.find(" ");
int s3 = line.find(" "); int s1 = line.find(" ");
int s2 = line.find(" ", s1+1);
int s3 = line.find(" ", s2+1);2. id.push_back(atoi(line.substr(0, s1).c_str()));
sex[i] = line.substr(s1+1, s2 - (s1+1)).c_str();
wage[i] = atof(line.substr(s2+1, s3 - (s2+1)).c_str());
id.push_back(atoi(line.substr(s3+1, line.length()-(s3+1)).c_str())); |
|
|
|
|
|
#38 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
AH!!! Yeah, I was fairly tired when I made those changes. So far so good, the program is actually running and doind what it is supposed to do. THANK YOU GUYS SOOOOOOOOOO MUUUUCCCHHHHH!!!!. I'm gunna play with it for awhile and see if I can find and major errors.
Thanks again! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|