Results 1 to 20 of 20

Thread: [Resolved] Vector help

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734

    [Resolved] Vector help

    Code:
                   eANum[i] = hold[0];
                   eAMass[i] = hold[1];
                   eName[i] = hold[2];
                   eSymbol[i] = hold[3];
    In the above code eANum etc are string arrays and hold is a vector, I try to assign the array of strings to a vector element, however, when I run the executeable it crashes, and I know it is this part because I commented it out and it ran fine.
    Last edited by dsheller; Apr 1st, 2003 at 09:41 PM.

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    could u show the rest of the code?
    \m/\m/

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Code:
    #include <fstream>
    #include <conio.h>
    #include <algorithm>	// container manipulation
    #include <string>		// easy strings
    #include <vector>		// growable array
    #include <string>
    
    using namespace std;
    
    void split_string(const string &s, vector<string> & out, char split_at);
    
    int main()
    {
    
       string incoming;
       int search;
    
       string eANum[105];
       string eAMass[105];
       string eName[105];
       string eSymbol[105];
       vector<string> hold;
    
       int i;
       int j;
       int k;
    
       ifstream infile;
    
       infile.open("a:\\elemental.txt", ios::in);
    
       if (!infile)
       	{
          	cout << "Fatal Exception: File could not be opened." << endl
             	  << "Press any key to terminate...";
             while (!kbhit());
          }
       else
       	{
          	for (i = 0; i <= 104; i++)
             	{
                   hold.resize(0);
    
          			infile >> incoming;
    
                   split_string(incoming, hold, ';');
    
                   eANum[i] == hold[0];
                   eAMass[i] == hold[1];
                   eName[i] == hold[2];
                   eSymbol[i] == hold[3];
                }
    
          }
    
       cout << "Enter a way to search: " << endl
            << "1. Search by atomic number." << endl
            << "2. Search by atomic mass." << endl
            << "3. Search by element name." << endl
            << "4. Search by element symbol." << endl;
    
       cin >> j;
    
       if (j == 1)
       	{
          	cout << "Enter an atomic number to search for." << endl;
            cin >> search;
         }
       else if (j == 2)
       	{
          	cout << "Enter an atomic mass to search for." << endl;
            cin >> search;
         }
       else if (j == 3)
       	{
          	cout << "Enter an element name to search for." << endl;
            cin >> search;
         }
       else
       	{
          	cout << "enter an element symbol to search for." << endl;
            cin >> search;
        }
         
    	
    
       return 0;
    }
    
    void split_string(const string &s, vector<string> & out, char split_at)
    {
    	// for searching the string
    	string::const_iterator stay, run;
    	// set stay to the start of the string
    	stay = s.begin();
    	// temporary
    	string t;
    	// loop run through the string, from front to back
    	for(run=stay; run != s.end(); ++run)
    	{
    		// if encountered split char...
    		if(*run == split_at)
    		{
    			// ...clear the temporary value...
    			t = "";
    			// (speeds it up a little)
    			t.reserve(run-stay);
    			// ..., copy current substring to temporary...
    			t.append(stay, run);
    			// ...and add it to the array.
    			out.push_back(t);
    			// Finally, let the next substring begin directly after the splitting char.
    			stay = run+1;
    		}
    	}
    	// same as above for the last piece (from last split char to end of string)
    	t = "";
    	t.reserve(run-stay);
    	t.append(stay, run);
    	out.push_back(t);
    }
    The split function is corned bee's function so I know that is not the problem.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    eANum[i] == hold[0];
    eAMass[i] == hold[1];
    eName[i] == hold[2];
    eSymbol[i] == hold[3];
    should be

    Code:
    eANum[i] = hold[0];
    eAMass[i] = hold[1];
    eName[i] = hold[2];
    eSymbol[i] = hold[3];
    = is the assignment operator, == is the comparison operator.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    I tried that, but it still crashes, any ideas?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Where does it crash? Do you know how to use a debugger?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    I have no idea why exactly it crashes but I do know it is the piece of code I initially posted.

    Edit:

    When I attempted to debug it it pointed me to this line:

    Code:
    basic_string<charT, traits, Allocator> &
    basic_string<charT, traits, Allocator>::operator= (
      const basic_string<charT, traits, Allocator>& str)
    {
        str.pref()->addReference();   // This line
        pref()->unLink(alloc_);
        data_ = str.data_;
        return *this;
    }
    Last edited by dsheller; Mar 31st, 2003 at 06:43 PM.

  8. #8
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    That's just the assignment operator for the standard string, so that's of no help since you already know that it crashes when you assign a certain value to one of those strings.

    As CB said, try to use a debugger and check what the variables are and on exactely what line it crashes
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Alright I will give it a try.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Well when I run debugger I get an error at the following line:

    eAMass[i] = hold[1];

    Also, when I do something like this:

    Code:
                   eANum[i] = hold[0];
                   cout << "Got to step 1\n";
                   eAMass[i] = hold[1];
                   cout << "Got to step 2\n";
                   eName[i] = hold[2];
                   cout << "Got to step 3\n";
                   eSymbol[i] = hold[3];
                   cout << "Got to step 4\n";
    It prints the "Got to step 1" line and then it crashes. So I think that after it first assigns to eANum it crashes.

  11. #11
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Code:
    eAMass[i] == hold[1];
    Are you sure there's a second element in the vector hold?

    The error is not at eANum since it does print got to step 1

    Also try to check what hold[1] contains when you try to add it to the string!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Yea this c++ business really frys my brain in VB I would have had this solved so fast. I will continue to try.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Well I am sure that it is filling the vector correctly because I tested it out without the loop and everything and it printed the entire file split at the semi-colon

  14. #14
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Yeah but it's not the prob at the first item of the vector, it occurs at the second (or have you used without the loop while accessing hold[1] ? )

    Also can you maybe attach the file you're trying to read? So that we can test it over here and see what the prob is?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Alright I will attach it, however, I think there is something weird going on because when I split it and read through each item in the vector it works fine but then assigning doesnt.
    Attached Files Attached Files

  16. #16
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Tried it here, runs without any crashes / runtime errors... what compiler are you using?

    also:
    Code:
       if (!infile)
       	{
          	cout << "Fatal Exception: File could not be opened." << endl
             	  << "Press any key to terminate...";
             while (!kbhit());
             return 1
          }
    You'd want the code to terminate

    And I recommend not to use conio.h, include <iostream> (for cin and cout) and use
    Code:
    system("pause");
    instead of
    Code:
    while (!kbhit());
    And consider using a switch statement rather than your if - elseif
    Last edited by Jop; Apr 1st, 2003 at 08:44 PM.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    I have no idea how you did that, but wow, thanks , I mean, it looks exactly like my old code in most respects I can't even find the major difference!

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    Sorry to burden you again, but say I want to search through and compare a string to a float value, how would I go about converting the string to float? I tried using atof but I didn't have much luck (errors).

  19. #19
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Something like this?

    Code:
    int main()
    {
    string s = "1.007";
    float f = atof(s.c_str());
    cout << f;
    }
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    I got it to work, I just decided to change the search variable to a string rahter than having it a float =/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width