Mar 19, 2012 at 4:36pm UTC
Hello!
I'm having trouble with a piece of code. Basically I have a struct with two variables which are both strings. When I run my code I use 'ordinary' strings to search a text and split it into two. Once that is done I try to put the separate strings into the struct variables. Here is when the problem occurs. I have no clue what causes this because I'm only working with strings, although two are in structs. Would really appreciate any input on this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
struct movieData{
string mType;
string mName;
};
void writeData(vector<movieData>& inData);
int main()
{
vector<movieData> inData;
writeData(inData);
system("Pause" );
return 0;
}
void writeData(vector<movieData>& inData){
string tmp;
ifstream inFile;
inFile.open("filmdb.txt" );
if (!inFile){
cout << "Could not find the file.\n" ;
}
else {
inData.clear();
movieData tmpData;
while (getline(inFile, tmp)){
string tmpData;
tmpData = tmp;
int pos = tmpData.find('\t' , 0);
string dType = tmpData.substr(0, pos);
int start = pos+1;
int langd = tmpData.length()-start;
string dName = tmpData.substr(start, langd);
cout << "dType: " << dType << endl;
tmpData.mType = dType; //trouble 1 here
cout << "dName: " << dName << endl;
tmpData.mName = dName; //trouble 2 here
//inData.push_back(tmpData);
}
}
if (inData.empty()){
cout << "\nNo data found!\n" ;
}
else {
cout << "\nWriting output:\n" ;
for (int i = 0; i < inData.size(); i++){
cout << inData.at(i).mType << '\t' << "- " << inData.at(i).mName << endl;
}
}
}
Last edited on Mar 19, 2012 at 4:37pm UTC
Mar 19, 2012 at 4:43pm UTC
Fiff0 wrote:Here is when the problem occurs.
Mind telling us what the problem actually is? You've made it clear
where the problem is, but not
what the problem is.
Last edited on Mar 19, 2012 at 4:44pm UTC
Mar 19, 2012 at 4:49pm UTC
tmpData is of type std::string. It looks like you want it to be of type movieData.
Mar 19, 2012 at 4:55pm UTC
Edit:
I think I found a huge problem. Using the variable tmpData twice. Gosh so retarded of me. Sorry for this post.
Last edited on Mar 19, 2012 at 4:58pm UTC
Mar 19, 2012 at 5:02pm UTC
Line 36: movieData tmpData;
Line 40: string tmpData;
same variable but two types.
Last edited on Mar 19, 2012 at 5:02pm UTC