Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2006, 6:10 PM   #31
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 11th, 2006, 6:51 PM   #32
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
It's supposed to find the first occurence of the search value within the given range. Did I do something wrong?
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 7:57 PM   #33
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
Your crash is probably in this line:
         years[i] = atoi(line.substr(s3+1, line.length()-(s3+1)).c_str());
As years is a vector, same as id, you need to add elements into it in the same way you do to id.

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.
The Dark is offline   Reply With Quote
Old Apr 11th, 2006, 8:37 PM   #34
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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>]
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 9:59 PM   #35
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
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().
The Dark is offline   Reply With Quote
Old Apr 11th, 2006, 10:53 PM   #36
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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.00

This 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!
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 11:36 PM   #37
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
OK, two problems:
1.
         int s1 = line.find(" ");
         int s2 = line.find(" ");
         int s3 = line.find(" ");
s1, s2 and s3 will all be the same value after this code. What you need to do is start the space searching after the previous space found (and also you should be checking for whether the spaces were found)
         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()));
When I said "As years is a vector, same as id, you need to add elements into it in the same way you do to id.", I didn't mean that you should add the years values into the id vector! Change the red highlighted id in the above code to years.
The Dark is offline   Reply With Quote
Old Apr 12th, 2006, 1:17 PM   #38
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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!
codylee270 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC