-
[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.
-
could u show the rest of the code?
-
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.
-
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.
-
I tried that, but it still crashes, any ideas?
-
Where does it crash? Do you know how to use a debugger?
-
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;
}
-
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
-
Alright I will give it a try.
-
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.
-
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!
-
Yea this c++ business really frys my brain in VB I would have had this solved so fast. I will continue to try.
-
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
-
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?
-
1 Attachment(s)
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.
-
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
instead of
And consider using a switch statement rather than your if - elseif
-
I have no idea how you did that, but wow, thanks :D, I mean, it looks exactly like my old code in most respects I can't even find the major difference!
-
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).
-
Something like this?
Code:
int main()
{
string s = "1.007";
float f = atof(s.c_str());
cout << f;
}
-
I got it to work, I just decided to change the search variable to a string rahter than having it a float =/