How do I use AND OR and NOT in C++, because I need to use an and statement and I thought this was right but the compiler is flagging an error with it because I think it thinks it is a pointer reference thing.

if (val1 > 16777215 & val1 <= 4294967295)

thanks in advance

Recommended Answers

All 15 Replies

Ah, now you've said your problem. Solution is simple:
AND && (notice, two & signs)
OR ||
NOT !

& is a bitwise AND operator while && is a logical AND operator

ok im still getting mass errors and I have no idea what could be wrong. I am posting all my source code for the entire program. It is an assembler. NOTE: a lot of it isnt finished so dont comment on things like that. I compiled using G++, someone try and compile it and see if they get errors like that too. I just need some help on this.

#include <iostream>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std;

int main() 
{
    int counter = 0;
    string op;
	unsigned int val1;
	int val2;
	int val3;
	string val4;
	string val5;
	
    int ProgramSize = 0;
	
	string SourceCodeName;
	string OutputFileName;
	
	vector<char> BinaryVector;
	

	cout << "Assemble: ";
    cin >> SourceCodeName;
    system("cls");
    cout << "Output: ";
    cin >> OutputFileName;
    system("cls");
    ifstream sourcefile;
	sourcefile.open(SourceCodeName.c_str());
	
	if (!sourcefile.is_open()) 
    {
        system("cls");
		cout << "error: can't open source code" << endl;
		system("PAUSE");
	}
	
	while (sourcefile >> op) 
    {
    if (op == "storeByte") 
        {
			sourcefile >> val1;
			sourcefile >> val2;
            //ElementAmount += 3;
            ProgramSize += 3;
            
             if (val1 <= 255)
             {
             ProgramSize++;
             }
            
             if (val1 > 255 && val1 <= 65535)
             {
             ProgramSize += 2;
             }
             
             if (val1 > 65535 && val1 <= 16777215)
             {
             ProgramSize += 3;
             }
             
             if (val1 > 16777215 && val1 <= 4294967295)
             {
             ProgramSize += 4;
             }
             
            // storeByte 0 20
		}
		
  if (op == "storeWord") 
        {
			
			sourcefile >> val1;
			sourcefile >> val2;
            //ElementAmount += 3;
            ProgramSize += 4;
            
             if (val1 <= 255)
             {
             ProgramSize++;
             }
            
             if (val1 > 255 && val1 <= 65535)
             {
             ProgramSize += 2;
             }
             
             if (val1 > 65535 && val1 <= 16777215)
             {
             ProgramSize += 3;
             }
             
             if (val1 > 16777215 && val1 <= 4294967295)
             {
             ProgramSize += 4;
             }
            // storeWord 0 20
		}
		
  if (op == "storeDword") 
        {
            sourcefile >> val1;
            sourcefile >> val2;
  
			//ElementAmount += 3;
			
			 if (val1 <= 255)
             {
             ProgramSize++;
             }
            
             if (val1 > 255 && val1 <= 65535)
             {
             ProgramSize += 2;
             }
             
             if (val1 > 65535 && val1 <= 16777215)
             {
             ProgramSize += 3;
             }
             
             if (val1 > 16777215 && val1 <= 4294967295)
             {
             ProgramSize += 4;
             }
            // storeDword 0 20
		}
		
       
        if (op == "moveMR") 
        {
               //ElementAmount += 3;
			// moveMR Eax 23
		}

		if (op == "moveRM") 
        {
               //ElementAmount += 3;
			// moveRM 2 Eax
		}

		if (op == "moveRR") 
        {
               //ElementAmount += 3;
			// moveRR Eax Ebx
		}

		if (op == "add") 
        {
               //ElementAmount += 3;
			// add Eax Ebx
		}

		if (op == "sub") 
        {
               //ElementAmount += 3;
			// sub Eax Ebx
		}

    // ElementAmount += 2;
	
    }
    sourcefile.close();





	
	

	// Final Parse
    
	ifstream sourcefile1;
	sourcefile1.open(SourceCodeName.c_str());
	if (!sourcefile1.is_open()) 
    {
        system("cls");
		cout << "error: can't open source code" << endl;
		system("PAUSE");
	}

	while (sourcefile1 >> op) 
    {
          
          if (op == "fillBytes")
          {
           sourcefile1 >> val1;
           sourcefile1 >> val2;
           sourcefile1 >> val5;
  
                       while (val1 <= val2)
                       {
                        BinaryVector[val1] = val5;
                        val1++;
                       }
         }

		if (op == "storeByte") 
        {
			sourcefile1 >> val1;
            sourcefile1 >> val2;
            BinaryVector[counter] = 198;
            counter++;
            BinaryVector[counter] = 6;
            counter++;
            BinaryVector[counter] = val1;
            counter++;
            BinaryVector[counter] = val2;
            counter++;
            
		}
		
		if (op == "storeWord")
		{
 	        sourcefile1 >> val1;
            sourcefile1 >> val2;
            BinaryVector[counter] = 199;
            counter++;
            BinaryVector[counter] = 6;
            counter++;
            BinaryVector[counter] = val1;
            counter++;
            BinaryVector[counter] = val2;
            counter++;
        }
               
        if (op == "storeDword")
        {
       	    sourcefile1 >> val1;
            sourcefile1 >> val2;
            BinaryVector[counter] = 102;
            counter++;
            BinaryVector[counter] = 199;
            counter++;
            BinaryVector[counter] = 6;
            counter++;
            BinaryVector[counter] = val1;
            counter++;
            BinaryVector[counter] = val2;
            counter++;
        }

        if (op == "moveMR") 
        {
               sourcefile1 >> val3;
               sourcefile1 >> val1;
               if (val3 == "eax")
               {
                        
               }
               if (val3 == "ebx")
               {
                        
               }
               if (val3 == "ecx")
               {
                        
               }
               if (val3 == "edx")
               {
                        
               }
			
            // moveMR eax 23
		}

		if (op == "moveRM") 
        {
               sourcefile1 >> val1;
               sourcefile1 >> val3;
               
               if (val3 == "eax")
               {
                        
               }
               if (val3 == "ebx")
               {
                        
               }
               if (val3 == "ecx")
               {
                        
               }
               if (val3 == "edx")
               {
                        
               }
			// moveRM 2 eax
		}

		if (op == "moveRR") 
        {
               sourcefile1 >> val3;
               sourcefile1 >> val4;
               
               if (val3 == "eax")
               {
                        
               }
               if (val3 == "ebx")
               {
                        
               }
               if (val3 == "ecx")
               {
                        
               }
               if (val3 == "edx")
               {
                        
               }
               if (val4 == "eax")
               {
                        
               }
               if (val4 == "ebx")
               {
                        
               }
               if (val4 == "ecx")
               {
                        
               }
               if (val4 == "edx")
               {
                        
               }
			// moveRR eax ebx
		}

		if (op == "add") 
        {
               sourcefile1 >> val3;
               sourcefile1 >> val4;
			// add eax ebx
		}

		if (op == "sub") 
        {
               sourcefile1 >> val3;
               sourcefile1 >> val4;
			// sub eax ebx
		}
	

	}
    sourcefile1.close();
    
  
    // Binary Write
  
	ofstream BinaryFile;
	BinaryFile.open(OutputFileName.c_str(),binary);
	if (!BinaryFile.is_open()) 
    {
        system("cls");
		cout << "error: can't create output file" << endl;
		system("PAUSE");
	}
	BinaryFile.write(BinaryVector,ProgramSize);
	BinaryFile.close();
}

I get warnings, not errors on lines like this:

if (val1 > 16777215 && val1 <= 4294967295)

[Warning] this decimal constant is unsigned only in ISO C90

You are using && correctly. You've declared val1 as a signed integer and 4294967295 is out of the bounds of a signed integer in a 32-bit system so if you are going to be comparing an integer to 4294967295, it's warning you that you'll have a problem. If you need to compare something as to whether it is less than or equal to 4294967295, compare it using an unsigned integer. I don't have a calculator handy, but is 4294967295 equal to 2^32 - 1? If so and you're on a 32-bit system, even when you compare an unsigned int to that number, it's always going to be less than or equal to it so it's sort of a pointless comparison (that is, if it is indeed 2^32 - 1). What is the purpose of this test?

val1 <= 4294967295

it was to check to see if it was a 32 bit memory address for my programming language and if it was to prepare for 4 bytes or w.e. but can you tell me what the actual errors are being caused by like that pointer to integer error i keep getting.

it was to check to see if it was a 32 bit memory address for my programming language and if it was to prepare for 4 bytes or w.e. but can you tell me what the actual errors are being caused by like that pointer to integer error i keep getting.

What line number and what is the exact error message? You need to format your code so it is readable. Right now nothing lines up. You are possibly mixing tabs and spaces, which plays havoc when posting on Daniweb (a tab is treated as something like eight spaces here, whereas in most editors it's four spaces. Best to stick with just spaces. That's the same everywhere.)

Post with C++-specific code tags, which adds line numbers, and give the line number to look at.

[code=cplusplus] // paste code here.

[/code]

Ok I have cleaned up the code and narrowed it all down to one error which I think is because it doesnt want to let me use a vector when outputting to a binary file or something.
Heres the error.

Line 296 - no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::write(std::vector<char, std::allocator<char> >&, int&)'

And heres the new source code.

#include <iostream>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std;

int main() 
{

int counter = 0;
string op;
int val1;
int val2;
int val3;
string val4;
string val5;
	
int ProgramSize = 0;
	
string SourceCodeName;
string OutputFileName;
	
vector<char> BinaryVector;
	

cout << "Assemble: ";
cin >> SourceCodeName;
system("cls");
cout << "Output: ";
cin >> OutputFileName;
system("cls");
    
ifstream sourcefile;
sourcefile.open(SourceCodeName.c_str());
	
if (!sourcefile.is_open()) 
{
 system("cls");
 cout << "error: can't open source code" << endl;
 system("PAUSE");
}
	
while (sourcefile >> op) 
{

if (op == "storeByte") 
{
sourcefile >> val1;
sourcefile >> val2;
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
ProgramSize += 3;
            
	if (val1 <= 255)
	{
	ProgramSize++;
	BinaryVector.push_back(0);
	}
            
	if (val1 > 255 && val1 <= 65535)
	{
	ProgramSize += 2;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
             
	if (val1 > 65535 && val1 <= 16777215)
	{
	ProgramSize += 3;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
             
	if (val1 > 16777215 && val1 <= 4294967295)
	{
	ProgramSize += 4;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
// storeByte 0 20
}
		
if (op == "storeWord") 
{
sourcefile >> val1;
sourcefile >> val2;
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
ProgramSize += 4;
            
	if (val1 <= 255)
	{
	ProgramSize++;
	BinaryVector.push_back(0);
	}
            
	if (val1 > 255 && val1 <= 65535)
	{
	ProgramSize += 2;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
             
	if (val1 > 65535 && val1 <= 16777215)
	{
	ProgramSize += 3;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}

	if (val1 > 16777215 && val1 <= 4294967295)
	{
	ProgramSize += 4;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
            // storeWord 0 20
}
		
if (op == "storeDword") 
{
sourcefile >> val1;
sourcefile >> val2;
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
BinaryVector.push_back(0);
			
	if (val1 <= 255)
	{
	ProgramSize++;
	BinaryVector.push_back(0);
	}
            
	if (val1 > 255 && val1 <= 65535)
	{
	ProgramSize += 2;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
             
	if (val1 > 65535 && val1 <= 16777215)
	{
	ProgramSize += 3;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
             
	if (val1 > 16777215 && val1 <= 4294967295)
	{
	ProgramSize += 4;
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	BinaryVector.push_back(0);
	}
            // storeDword 0 20
}
		
if (op == "moveMR") 
{
               //ElementAmount += 3;
			// moveMR Eax 23
}

if (op == "moveRM") 
{
               //ElementAmount += 3;
			// moveRM 2 Eax
}

if (op == "moveRR") 
{
               //ElementAmount += 3;
			// moveRR Eax Ebx
}

if (op == "add") 
{
               //ElementAmount += 3;
			// add Eax Ebx
}

if (op == "sub") 
{
               //ElementAmount += 3;
			// sub Eax Ebx
}

    
}
sourcefile.close();

// Final Parse
    
ifstream sourcefile1;
sourcefile1.open(SourceCodeName.c_str());

if (!sourcefile1.is_open()) 
{
system("cls");
cout << "error: can't open source code" << endl;
system("PAUSE");
}

while (sourcefile1 >> op) 
{
          
if (op == "fillBytes")
{
sourcefile1 >> val1;
sourcefile1 >> val2;
sourcefile1 >> val3;
  
	while (val1 <= val2)
	{
	BinaryVector[val1] = val3;
	counter++;
	val1++;
	}
}

if (op == "storeByte") 
{
sourcefile1 >> val1;
sourcefile1 >> val2;
BinaryVector[counter] = 198;
counter++;
BinaryVector[counter] = 6;
counter++;
BinaryVector[counter] = val1;
counter++;
BinaryVector[counter] = val2;
counter++;
}

if (op == "storeWord")
{
sourcefile1 >> val1;
sourcefile1 >> val2;
BinaryVector[counter] = 199;
counter++;
BinaryVector[counter] = 6;
counter++;
BinaryVector[counter] = val1;
counter++;
BinaryVector[counter] = val2;
counter++;
}
               
if (op == "storeDword")
{
sourcefile1 >> val1;
sourcefile1 >> val2;
BinaryVector[counter] = 102;
counter++;
BinaryVector[counter] = 199;
counter++;
BinaryVector[counter] = 6;
counter++;
BinaryVector[counter] = val1;
counter++;
BinaryVector[counter] = val2;
counter++;
}

}

sourcefile1.close();
    
// Binary Write
  
ofstream BinaryFile;
BinaryFile.open(OutputFileName.c_str(),ios :: binary);

if (!BinaryFile.is_open()) 
{
system("cls");
cout << "error: can't create output file" << endl;
system("PAUSE");
}

BinaryFile.write(BinaryVector,ProgramSize);
BinaryFile.close();

}

i still dont understand how to make a char vector into a char array. I tried this

char* Binary[] = BinaryVector[];

but it doesnt work. Please help.

i still dont understand how to make a char vector into a char array. I tried this

char* Binary[] = BinaryVector[];

but it doesnt work. Please help.

It's going to be more complicated than that, I think. I don't think C++ has any function that converts a vector to an array for you like Java does, so you probably have to write your own deep copy function:

char* ConvertVectorToArray (vector <char> charVector, int& size)
{
     size = charVector.size ();
     char* charArray = new char[size];
     for (int i = 0; i < size; i++)
         charArray[i] = charVector[i];

     return charArray;
}

This may not actually be necessary, come to think of it. You may not need to convert the vector to an array since the vector will contain a char array. You can probably just do this:

char* charArrayStart = &BinaryVector[0];
BinaryFile.write(charArrayStart, ProgramSize);

I haven't actually tested it, but it compiles.

is that a reference to the start of my vector?

is that a reference to the start of my vector?

&BinaryVector[0] is the address of the first char ELEMENT of your ARRAY in the vector. I don't know how a vector is stored in memory, but part of that storage is the array. The vector also needs to store the capacity and the size somewhere. You are interested in isolating the array part of the vector when you use the write command.

Yes, it is, but it's not a reference, it's a pointer to the 1st element of a vector contents (until you invoke any op changed vector's size or capacity).
The C++ Standard guarantees that std::vector contents occupies contiguous and properly aligned memory area.

ok thanks for ur help

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.