void encrypt::transform()
{
	int length = 0;
	char *buffer;

	char ch = ' ';

	int i = 0;
	fileEncrypt.open("filter.txt", ios::in | ios::beg);
	
	fileEncrypt.seekg(0, ios::end);
	length = fileEncrypt.tellg();
	fileEncrypt.seekg(0, ios::beg);

	buffer = new char [length];
   
	fileEncrypt >> ch;

	while(!fileEncrypt.eof())
	{
		buffer[i] = ch;
		i++;
		fileEncrypt >> ch;
	}

	for(int j = 0; j < length; j++)
	{
		buffer[j] = static_cast<int> + 1;
	}
	

	
}

Everything compiles fine, and Ive tested my dynamic array and it is given the proper size etc... but in my static cast for loop, it is giving me a syntax error upon compile. What am I doing wrong?

Thanks

Dani AI

Generated

The compile error was just the symptom — the root causes and a few fragile details are worth calling out so the routine is correct and robust.

The syntax issue came from using the cast operator incorrectly; once you write the cast with parentheses it compiles. Beyond that, note how character arithmetic and assignments behave: char is promoted to an integer for arithmetic, so buffer[j] + 1 is already an integer expression. If you intend to store the result back into the char slot explicitly, cast it to the desired byte type (preferably unsigned char or uint8_t if treating bytes) to avoid implementation-defined results when values exceed the signed char range. See the rules for integral/arithmetic conversions: Integral conversions and promotions.

Also be careful with file I/O choices: formatted extraction (operator>>) skips whitespace, so using it to fill a buffer while using tellg() to size that buffer will give mismatches. For raw bytes use unformatted reads (read/get) and open in binary mode if you need exact bytes. Functions for seeking/reading are documented here: seekg / tellg and read.

A safer pattern: get the file size, allocate a container like std::vector<char> (or std::string), read the whole file, then modify bytes in-place. This avoids manual new[]/delete[], preserves whitespace, and makes intent clear.

Example outline (read-modify-write approach):

std::ifstream in("filter.txt", std::ios::binary);
in.seekg(0, std::ios::end);
auto n = in.tellg();
in.seekg(0, std::ios::beg);
std::vector<char> buf(n);
if (n) in.read(buf.data(), n);
for (char &c : buf) c = static_cast<unsigned char>(c) + 1;

Finally, check for open/read errors, handle zero-length files, and prefer STL containers over naked pointers. The suggestions from about promotions are correct; ’s syntax fix gets you compiling, but the refinements above will avoid subtle bugs.

Fixed it. It should be static_cast<int>(buffer[j] + 1)
right? Well thats what works

You need to static_cast<int>(something) you can't just have it by itself. In reality you may not even need it at all, remember that chars are one-byte ints. If anything at all you'd almost need static_cast<char> for that quantity to put it back into buffer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.