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, 2:40 AM   #21
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
OMGBBQ111elevenHashbang David, your function and variable names are catAssTrophicly BadStard.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 11th, 2006, 1:24 PM   #22
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
So you made the class and used it as a structure, not worrying about defining class methods and made the variables public. Does making the class variables public allow you to do whatever you want with them?
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 1:48 PM   #23
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The only difference between a class and a structure in C++ is that the structure is by default public, and the class is by default private. I would have used a structure, but I decided AFTER making the class to make it public and avoid writing the getter/setter methods. It isn't a good idea, generally, but it was a short example. You'll note how simple it is to make a class -- it almost looks like a function with no code and local variables. Note that, unlike a function, it has to be followed by a semicolon. The class/structure does NOT generate code, typically. It is a pattern for an object that is later created by instantiating it. "bastardRecord theData;" makes one. Another one is made each time I copy theData into a vector. Objects scare a lot of people because self-appointed high-priest/gurus like to gold-plate their mantles and make acolytes kiss their asses. A lawnmower is an object. Mowing the lawn is procedural. Big deal. You might want to read the little blurb in my signature, "OOD, My View". Properly used, it's a helluva tool.

Ruben, when I write code for myself, I can be as informal as I like .
__________________
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

Last edited by DaWei; Apr 11th, 2006 at 2:11 PM.
DaWei is offline   Reply With Quote
Old Apr 11th, 2006, 2:18 PM   #24
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
I have read both your view and the thing on pointers, and not only is the pointer page funny, but it's probably the clearest and most illustrative explanation that i've ever seen... And I know that i've asked you before, wise ol' man, but what have you done career-wise to gain such experience with your computer languages?
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 3:05 PM   #25
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
And while you are still reading this thread, I've done the reformatting so that the file only saves as raw data with a single space between data. I'm trying to write the program that will read in the file and let you change the data, so as i'm reading it in from the file line by line, i'm splitting it up on the spaces, and funneling them into their respective arrays: BUT, i've got an error on one of them:

Please take a look:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector>
#include <cstdlib>
using namespace std;


int main()
{
	int i=0,j=0,id[10];
	vector<int> years;
	char sex[10];
	float wage[10];
	int yearsworked;
    ifstream infile;
    ofstream outfile;
    string disp1, disp2, filename;
    string line;

    cout << "Please input the name of the file you wish to open: " ;
    getline(cin, filename);
    
    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[i] = atoi(line.substr(0, s1).c_str());
         sex[i] = line.substr(s1+1, s2 - (s1+1)).c_str();
         wage[i] = atoi(line.substr(s2+1, s3 - (s2+1)).c_str());
         years[i] = atoi(line.substr(s3+1, line.length()-(s3+1)).c_str());
         
         i++;
    }
    
    infile.close();
         
  
	system("PAUSE");
	return 0;
}

There error is: invalid conversion from `const char*' to `char'

corresponds to :
 
sex[i] = line.substr(s1+1, s2 - (s1+1)).c_str();

What's the fix for this? After that, I'm going to let the users change certain data, but I gotta have the data first!!!
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 3:17 PM   #26
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You can't assign a c-string to a single character, which is what sex [index] is. I cautioned you earlier to forget treating gender as a single character, because it limits your options. I don't understand why you're regressing to (forgive me) trash code.
__________________
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, 3:21 PM   #27
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
no, please forgive me..lol.. Through the lengthy posts i forgot to change it to a string, which I did. It now compiles but crashes after completion......................
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 5:15 PM   #28
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
I've continued building onto the program, but I would like someone to explain to me why it crashes before I post newer, longer code to make the issue more confusing. Thanks!
codylee270 is offline   Reply With Quote
Old Apr 11th, 2006, 5:50 PM   #29
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Post your new code. You may be overflowing an array, which is why you should be using vectors. You have no idea what the size of the file may be (in a practical world), so you're always going to have problems allocating. Failure to put the items into a structure that aggregates them destroys the interrelationship that they naturally possess. You don't need to search the line for the separator; that's what the various input methods are for. The separator is probably better if it's a non-space (like a comma). You might introduce a name field and want "Joe Bob" to be one item.
__________________
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:02 PM   #30
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Alrighty, I know it's long, but i've been building on it. I've got a lot more on the end of it for saving the file, but i'll take it off just for the sake of length (even though its drawn out already. I've got an another few errors, and I'm thinking they all stem from the initial error. I'll elaborate below:

#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] = atoi(line.substr(s2+1, s3 - (s2+1)).c_str());
         years[i] = 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);

	system("PAUSE");
	return 0;
}

Besides the crash, (which i'm almost certain you're correct that it is an array issue, i just don't know how to fix it) the last line gives an error.

offset = find(id.begin(),id.end(), selection);

error: cannot convert `__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >' to `int' in assignment.

I've got another whole half to this program after that line, but like i said, it has to deal with making changes to the data brought in from the file. The other half would work if I could get the offset to work (and NOT CRASH in the first place), since changing the data is based on that offset!!!

daWei, if i'm a pain in your ass, please forgive me b/c I am definitely you're biggest fan and you have definitely kept this alive for me!
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 12:07 PM.

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