G
googlinggoogler
Hi,
Im new to C++ and trying to self teach myself whilst I sit at my
unentertaining day job (thought i'd put that across before im accused
of cheating on my homework, im 45...)
Anyway I'm trying to dynamically assign a structure whilst I read from
a file, however my program crashes, and im not sure why other than that
its to do with my memory operations using new and delete.
Im pretty good at C so the reason for learning is so that I can truely
put C/C++ on my CV instead of just C... but also im interested in the
object orrientated aspect.
Could I use a class instead of a structure? whats best? where am I
going wrong?
Thanks
Heres my Code -
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct TOKEN
{
string linetoken;
};
int main()
{
TOKEN *Tokens;
int counter = 0;
ifstream ifs("data.txt");
string line;
while(getline(ifs,line))
{
Tokens = new TOKEN[counter + 1];
Tokens[counter].linetoken = line;
//cout << "[ " << line << " ]" << endl;
cout << Tokens[counter].linetoken << endl;
counter = counter + 1;
}
cout << "AAAAAAAA";
delete Tokens;
return 0;
}
Im new to C++ and trying to self teach myself whilst I sit at my
unentertaining day job (thought i'd put that across before im accused
of cheating on my homework, im 45...)
Anyway I'm trying to dynamically assign a structure whilst I read from
a file, however my program crashes, and im not sure why other than that
its to do with my memory operations using new and delete.
Im pretty good at C so the reason for learning is so that I can truely
put C/C++ on my CV instead of just C... but also im interested in the
object orrientated aspect.
Could I use a class instead of a structure? whats best? where am I
going wrong?
Thanks
Heres my Code -
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct TOKEN
{
string linetoken;
};
int main()
{
TOKEN *Tokens;
int counter = 0;
ifstream ifs("data.txt");
string line;
while(getline(ifs,line))
{
Tokens = new TOKEN[counter + 1];
Tokens[counter].linetoken = line;
//cout << "[ " << line << " ]" << endl;
cout << Tokens[counter].linetoken << endl;
counter = counter + 1;
}
cout << "AAAAAAAA";
delete Tokens;
return 0;
}