0

I make a program that reads a file and then it stores the information in two char.The problem is related to the char Elements and symbol. What is causing errors in my program? Thanks in advance.

#include "stdafx.h"
#include <iostream>
#include <fstream>

char* Elements;//The problem
char * symbol;

using namespace std;

bool read(char* Filename){

	int maxlines;
	ifstream file;

	file.open(Filename);

	if(!file) return false;

	while(file.eof()){

		file >> maxlines;
		Elements = new char[maxlines];
		symbol = new char[maxlines];

	for(int i = 0; i < maxlines; i++)
		{
			file >> Elements[i] >> symbol[i];
	}
	}
	return true;
}
int main(){

	read("Periodic Table.txt");
	cout << Elements[2] << endl;

	system("pause");
	
	return 0;
}
3
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by Mr. K
0

How many characters are on each of the lines in the file? Just 2 characters per line? That's all your program is reading. Post the first few lines of the text file so that we can see what's wrong with your program.

0

In my file there are two words separated by a space.
For example:

Hydrogen H
Helium He

0

in line #21, are you reading in 'maxlines' every loop iteration? is this necessary? also, lines #22 and #23 you are re-allocating memory for 'element' and 'symbol' each loop iteration. Instead, try allocating the memory once, when the variable is declared. Then, every time you make a read operation, store the results into another container, perhaps a vector<char*>.... or even declare a 2D array:

//2D dynamic array of character arrays
//Element[number_of_elements][longest_word_length]

//Initialize a 'pointer to a pointer'
char** element = new char*[number_of_elements];

//allocate memory
for(int i=0; i<number_of_elements; i++)
{
     element[i] = new char[longest_word_length];
}

//read in from text file
int counter = 0;
while(infile)
{
     infile >> element[counter];
     counter++;
}

Make sure the second dimension of the array can accommodate the longest word + 1 null terminating character. you may even consider and extra amount of 'padding' just to ensure there are no problems exceeding the bounds of your arrays.

also, i would recommend using string class objects in order to avoid having to do all this memory allocation crap.

Edited by Clinton Portis: n/a

0

I tried to create a 2d array. Thanks for helping me in line 21.
Error 1 error C2440: '=' : cannot convert from 'char *' to 'char' c:\documents and settings\administrator\my documents\visual studio 2008\projects\allegro\allegro\allegro.cpp 25
Error 2 error C2440: '=' : cannot convert from 'char *' to 'char' c:\documents and settings\administrator\my documents\visual studio 2008\projects\allegro\allegro\allegro.cpp 26

#include "stdafx.h"
#include <iostream>
#include <fstream>

char** Elements;
char** symbol;

using namespace std;

bool read(char* Filename){

	int maxlines;
	ifstream file;

	file.open(Filename);

	if(!file) return false;

		file >> maxlines;
		char* Elements = new char[maxlines];//initialize pointer to pointer
		char* symbol = new char[maxlines];

	for(int i = 0; i < maxlines; i++)
	{//error
		Elements[i] = new char[15];//largest word length
		symbol[i] = new char[2];
	}

	//read from file to text
	int counter = 0;
	while(file){

		file >> Elements[counter] >> symbol[counter];
		counter++;
	}
	return true;
}
int main(){

	read("Periodic Table.txt");

	system("pause");
	
	return 0;
}
0

lines #20 and #21....

Elements = new char*[maxlines];//initialize pointer to pointer
symbol = new char*[maxlines];

also, you are not allowing space for a null terminating character when you declare symbol = new char[2] because periodic symbols are usually two characters.. leaving no space for a null terminator in your c-string. it should at minimum be symbol = new char[3].

Edited by Clinton Portis: n/a

0

Thanks for helping me. I fix the problem related to the pointers.
Now I found a new error. I don´t know what is causing it.What does it mean?

Unhandled exception at 0x104e76f1 in allegro.exe: 0xC0000005: Access violation writing location 0xfdfdfdfd.

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

bool read(char* Filename){

	int maxlines;
	ifstream file;

	file.open(Filename);

	if(!file) return false;

		file >> maxlines;

		char** Elements = new char*[maxlines];//initialize pointer to pointer
		char** symbol = new char*[maxlines];

	for(int i = 0; i < maxlines; i++)
	{
		Elements[i] = new char[20];//largest word length
		symbol[i] = new char[3];
	}

	//read from file to text
	int counter = 0;
	while(file){

		file >> Elements[counter];
		counter++;
	}
	return true;
}
int main(){

	read("Periodic Table.txt");
	system("pause");
	
	return 0;
}

Edited by Mr. K: n/a

0

The error might be in line 31. It reads the word and store in a col. Does it really store it in a col? Lines 31 looks like an array and not like a 2d array.

0

if you ever take assembly language, you'll get a little insight on how arrrays and 2D arrays are really handled in memory. it does appear that line #31 looks like a 1d array; but i think it is ok. you could just as easily write it as: file[elements][0] but it will resolve to the same memory location as file[elements].

each element[] is just a pointer to the starting memory position of a 15 char buffer.


element[starting_memory_address][15_char_buffer]

so after your array is loaded, you could just easily do this and list all the elements of the periodic table:

for(int i=0; i<number_of_elements; i++)
{
     cout << element[i] << endl;
}

because you are outputting a cstring at it's starting memory position. output will stop at the null terminating character.

perhaps the problem is compiler dependent; try changing line #31 to element[0] and see if that helps.

Edited by Clinton Portis: n/a

0

should be:

//display
for(int i=0; i<number_of_elements; i++)
{
     cout << *element[i] << endl;
}

Edited by Clinton Portis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.