beginner C++ question (int increment)

I was under the impression C++ was an extension of C and that a C++ compiler could compile programs written in C
 
Your impression is mostly correct.
C++ compilers have historically just been patched up C compilers, which explains why many C++ features took ages to get integrated (and spec compliant.).
 
Mostly, but the op couldnt compile the C code with his C++ compiler and had to use a C compiler
And if I was correct and C++ compilers did compile C why would C compilers (made post C++ introduction) exist
 
had a go at the start of grey-scale image compressor ( did you write this code Ethatron?).

Hey, you found out what it is. (thumb up)
No, it's from Bernie Meyer. He was participating in a smallest code contest, that's why there is the macro code-implosion in it. He was also playing with image compression at the time, which he took to try himself in the contest. For quite some time this was the most efficient lossless image compressor available.
To me it's the most perfect source I've ever seen, it's funny (in various ways), it's artistic, it's efficient, it's marvelous (you ran it once? a possible commandline is like this: "program <input.pgm >output.coded"), it's also playful ironic (sarkastic) in regards to I-have-the-new-better-algorithm science. Like the eggs from Kinderüberraschung. :smile:
The variables give away its name if you want to search for it.

"P sqrt(P)" is a protoype BTW, no need to include math.h
"& is bitwise AND" is wrong, & is the get-reference-of-variable operator. In C that's about it (and reference means pointer), in C++ you can also declare variables to be references instead of containers (and they are distinct types). I think actually that would be a good start for learning a pure C++ syntax/concept. And if you learn that first you may not get the bad habit of using pointers all the time, it nicely goes with operator overloading as well, which I would recommend next.

All the other stuff you've shown is the same in C (?: is called ternary operator, ++ increment and -- decrement also are part of C), and a lot of other languages.
 
Mostly, but the op couldnt compile the C code with his C++ compiler and had to use a C compiler
And if I was correct and C++ compilers did compile C why would C compilers (made post C++ introduction) exist

Although most C programs are valid C++ programs, not all C programs are valid C++ programs. The most obvious examples are those with variable names that happens to be C++ keywords (that also influences the choice of C++ keyword names).
 
Hey, you found out what it is. (thumb up)
No, it's from Bernie Meyer. He was participating in a smallest code contest, that's why there is the macro code-implosion in it. He was also playing with image compression at the time, which he took to try himself in the contest. For quite some time this was the most efficient lossless image compressor available.
To me it's the most perfect source I've ever seen, it's funny (in various ways), it's artistic, it's efficient, it's marvelous (you ran it once? a possible commandline is like this: "program <input.pgm >output.coded"), it's also playful ironic (sarkastic) in regards to I-have-the-new-better-algorithm science. Like the eggs from Kinderüberraschung. :smile:
The variables give away its name if you want to search for it.

"P sqrt(P)" is a protoype BTW, no need to include math.h
"& is bitwise AND" is wrong, & is the get-reference-of-variable operator. In C that's about it (and reference means pointer), in C++ you can also declare variables to be references instead of containers (and they are distinct types). I think actually that would be a good start for learning a pure C++ syntax/concept. And if you learn that first you may not get the bad habit of using pointers all the time, it nicely goes with operator overloading as well, which I would recommend next.

All the other stuff you've shown is the same in C (?: is called ternary operator, ++ increment and -- decrement also are part of C), and a lot of other languages.

well im trying a pointer at the moment and getting a little confused.

what im actually trying to do is print decimal values as binary using _itoa_s in stdlib.h.
I have it working but i dont understand why it is working

if i have code like this

Code:
void bit_shift()
{
	using namespace std;

	clearscreen();
	int dec_number = 0;
	cout << " showing how bit shift works " << endl ;
	cout << "please enter an integer number  :";
	cin >> dec_number ;
	
	char *binary_value=0;

	//returns ptr to array
	binary_value = binary_conversion (dec_number);
	
	
	std::cout << &binary_value << endl;

}

char *binary_conversion(int i)
{
		//char *buffer_return = new char[33];
		static char buffer[33];
		_itoa_s (i,buffer,2);
	
	//std::cout << buffer_return ;
	return buffer;

}

std::cout << &binary_value << endl; returns a memory address which im fine with

but if i change that to std::cout << &binary_value[0] << endl; i get the contents of the entire array (the desired result) but i dont understand why .

if i change it to std::cout << *binary_value << endl; i just get the first element of the array ( unless 0 it will always be 1)

i tried something like
Code:
for ( int i =0 ; i < 34 ; i++)
{
std::cout << *binary_value[i] << endl
}

but it kept throwing compile errors and i couldn't get it to work. which from my limited understanding makes sense as the pointer should be just the address of the first element.
 
try

#include <iostream>
#include <string>
#include <cmath>

int main()
{
std::string binary;
double decimal = 0;

std::cout << "Enter the binary value you wish to convert into decimal: " << endl;
std::cin >> binary;

for(int counter = 0; counter < binary.size(); counter++)
if(binary.c_str()[counter] == '1')
decimal += pow(2, counter);

cout << "The decimal representation of the given binary is: " << decimal << endl;
return 0;
}

edit: may be missing
std::endl
std::cout
 
cheers :smile:

but my code works fine:

output:
showing how bit shift works
please enter an integer value :213
the number you entered 213 in binary is :11010101
with 1 bit shifted left 426 in binary is :110101010
with 2 bit shifted left 852 in binary is :1101010100
with 1 bit shifted right 106 in binary is :1101010
with 2 bit shifted right 53 in binary is :110101


its more i don't understand why &binary_value[0] returns the entire array when &binary_value returns a memory address and *binary_value returns a single element of the array
 
its more i don't understand why &binary_value[0] returns the entire array when &binary_value returns a memory address and *binary_value returns a single element of the array

That's because &binary_value[0] is a char* type, which is treated as a string in C. On the other hand, *binary_value is a char type, so it's a simple value.
 
Well i spend the last two nights before bed working pretty much on this...

At first i thought it would be cool. Now i am never doing anything as stupid pointless and insane again ;

heres the code
Code:
void OR_XOR()
{
	using namespace std;
	int dec_number_1 = 0;
	cout << " showing how XOR, OR and AND works " << endl ;
	cout << "please enter an integer number  :";
	cin >> dec_number_1 ;
	
	int dec_number_2 = 0;
	cout << "please enter a second integer number  :" ;
	cin >> dec_number_2 ;
	cout <<  endl << endl <<endl ;

	//libary fuction to convert dec number into binary for first number
		char buffer_1[33];
	_itoa_s (dec_number_1,buffer_1,2);
	
	//counts the number of binary digits with the array, 
	//aka elements with either 1 or 0 in them
	int count_1= 0;
	for each (char i in buffer_1)
	{
		
		//std::cout << i ;
		if (i ==48 || i ==49) {
			count_1++ ; 
			
		}
	}

	
	//libary fuction to convert dec number into binary for second number
	 char buffer_2[33];
	_itoa_s (dec_number_2,buffer_2,2);
	
	//counts the number of binary digits with the array, 
	//aka elements with either 1 or 0 in them
	int count_2=0;
	 for each (char j in buffer_2)
	 {
		 if (j ==48 || j==49)
		 {
			 count_2++;
		 }

	 }

	// used to figure out which "number" is longer in binary length
	// positive number = first is longer, neg number second is longer
	int bit_length_difference=0;
	bit_length_difference = count_1 - count_2;
	int XOR_count =0;
	int OR_count =0;
	int AND_count=0;
	if ( count_1 > count_2)
	{
		XOR_count =count_1;
		OR_count =count_1;
		AND_count=count_1;
	}
	else
	{
		XOR_count =count_2;
		OR_count =count_2;
		AND_count=count_2;
	}

	// final array for first number converted from char to int and will be 
	// same binary lenth as second_number_final
	int first_number_final[33];
	int second_number_final[33];
	int XOR_array[33];
	int XOR_DEC_value=0;
	int OR_array[33];
	int OR_DEC_value=0;
	int AND_array[33];
	int AND_DEC_value=0;
	//used as a counter for first_number_final an second_number_final array
	int count_fnf=0;
	int count_snf=0;
	
	//if b_l_d is less then 0 then second number is longer in binary digits then first
	// create a new array and populate its lowest elements with int 0 until addition 0's plus buffer_1 binary length is 
	// the same as buffer_2.
	if ( bit_length_difference < 0)
	{
		
		//adds the value 0 to elements in first_number_final until
		// bit_length_difference = 0;
		while ( bit_length_difference != 0)
		{
			first_number_final[count_fnf] = 0;
			bit_length_difference++;
			count_fnf++;
		}
		//used in whileloop to track elements in buffer_1
		int i = 0;
		while ( count_1 != 0)
		{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_1[i] == 48)
			{
			first_number_final[count_fnf]=0;
			i++;
			count_fnf++;
			count_1--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_1[i] == 49)
			{
			first_number_final[count_fnf]=1;
			i++;
			count_fnf++;
			count_1--;
			}
		}
		
		//used in whileloop to track elements in buffer_2
		int k = 0;
		//used to convert the second number to binary
		while ( count_2 != 0)
			{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_2[k] == 48)
			{
				second_number_final[count_snf]=0;
				k++;
				count_snf++;
				count_2--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_2[k] == 49)
			{
				second_number_final[count_snf]=1;
				k++;
				count_snf++;
				count_2--;
			}

		}
	}else if (bit_length_difference > 0) 
	{
		//adds the value 0 to elements in second_number_final until
		// bit_length_difference = 0;
		while ( bit_length_difference != 0)
		{
			second_number_final[count_snf] = 0;
			bit_length_difference--;
			count_snf++;
		}
		//used in whileloop to track elements in buffer_1
		int i = 0;
		while ( count_2 != 0)
		{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_2[i] == 48)
			{
				second_number_final[count_snf]=0;
				i++;
				count_snf++;
				count_2--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_2[i] == 49)
			{
				second_number_final[count_snf]=1;
				i++;
				count_snf++;
				count_2--;
			}
		}

		//used in whileloop to track elements in buffer_1
		int k = 0;
		//used to convert the second number to binary
		while ( count_1 != 0)
			{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_1[k] == 48)
				{
				first_number_final[count_fnf]=0;
				k++;
				count_fnf++;
				count_1--;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_1[k] == 49)
				{
				first_number_final[count_fnf]=1;
				k++;
				count_fnf++;
				count_1--;
			}

		}
	//both values have same binary lenght
	}
	else
	{
		//used in whileloop to track elements in buffer_1 and buffer_2 as they are the same length
		int k = 0;
		
		//used to convert the second number to binary
		while ( count_1 != 0)
		{
			//if char element in buffer array ==48 (int 0) then add to first_number_final array
			// in element number tracked by int var temp.
			// int i tracks buffer_1 element position
			
			if (buffer_1[k] == 48)
			{
				first_number_final[count_fnf]=0;
				count_fnf++;
				count_1--;
				std::cout << count_fnf;
			
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_1[k] == 49)
			{
				first_number_final[count_fnf]=1;
				count_fnf++;
				count_1--;
				std::cout << count_fnf;
			}
			if (buffer_2[k] == 48)
			{
				second_number_final[count_snf]=0;
				count_snf++;
				std::cout << count_snf << std::endl;
			}
			// if char element ==49 (int 1) then add to first_number_final array
			if (buffer_2[k] == 49)
			{
				second_number_final[count_snf]=1;
				count_snf++;
				std::cout << count_snf << std::endl;
				
			}
			k++;
			
		}

	}
	std::cout << " the first number you entered in binary is : \t \t " ;
	int i=0;

	while (i != count_fnf)
	{
		std::cout << first_number_final[i] ;
		i++;
	}

	std::cout << std::endl;
	std::cout << " the second number you entered in binary is : \t \t " ;
	int k=0;
	while (k != count_snf)
	{
		std::cout << second_number_final[k] ;
		k++;
	}
	std::cout << std::endl;
	
	int j =0;
	int power = XOR_count -1;
	int power_temp = 0;
	float power_of_2 = 2;
	while (j != XOR_count)
	{
		
		XOR_array[j] = first_number_final[j] ^ second_number_final[j];
		
		if ( XOR_array[j] == 1)
		{
			if ( j == (count_fnf-1))
			{
				XOR_DEC_value = XOR_DEC_value + 1;
			}
			else
			{
			
			double power_temp =pow(power_of_2,power);
			XOR_DEC_value = XOR_DEC_value + power_temp ;
			
			
			}
		
		}
		power--;
		j++;
	}
	std::cout << "XOR'd number based off your numbers in binary is :  \t " ;
	int l=0;
	while (l != XOR_count)
	{
		std::cout << XOR_array[l] ;
		l++;
	}
	std::cout << std::endl;
	std::cout << "the dec value of the XOR number is : \t \t \t " << XOR_DEC_value << std::endl;


	int M =0;
	int power_OR = OR_count -1;
	int power_OR_temp = 0;
	while (M != OR_count)
	{
		
		OR_array[M] = first_number_final[M] | second_number_final[M];
		
		if ( OR_array[M] == 1)
		{
			if ( M == (count_fnf-1))
			{
				OR_DEC_value = OR_DEC_value + 1;
			}
			else
			{
			
			double power_OR_temp =pow(power_of_2,power_OR);
			OR_DEC_value = OR_DEC_value + power_OR_temp ;
			
			
			}
		
		}
		power_OR--;
		M++;
	}
	std::cout << "OR'd number based off your numbers in binary is :  \t " ;
	int N=0;
	while (N != XOR_count)
	{
		std::cout << OR_array[N] ;
		N++;
	}
	std::cout << std::endl;
	std::cout << "the dec value of the OR number is : \t \t \t " << OR_DEC_value << std::endl;

	int O =0;
	int power_AND = AND_count -1;
	int power_AND_temp = 0;
	while (O != OR_count)
	{
		
		AND_array[O] = first_number_final[O] & second_number_final[O];
		
		if ( AND_array[O] == 1)
		{
			if ( O == (count_fnf-1))
			{
				AND_DEC_value = AND_DEC_value + 1;
			}
			else
			{
			
			double power_AND_temp =pow(power_of_2,power_AND);
			AND_DEC_value = AND_DEC_value + power_AND_temp ;
			
			
			}
		
		}
		power_AND--;
		O++;
	}
	std::cout << "AND'd number based off your numbers in binary is :  \t " ;
	int P=0;
	while (P != AND_count)
	{
		std::cout << AND_array[P] ;
		P++;
	}
	std::cout << std::endl;
	std::cout << "the dec value of the AND number is : \t \t \t " << AND_DEC_value << std::endl;
	std::cout << std::endl << std::endl << std::endl << std::endl << std::endl;
}

and here is the output

Code:
 showing how XOR, OR and AND works
please enter an integer number  :1243
please enter a second integer number  :123



 the first number you entered in binary is :             10011011011
 the second number you entered in binary is :            00001111011
XOR'd number based off your numbers in binary is :       10010100000
the dec value of the XOR number is :                     1184
OR'd number based off your numbers in binary is :        10011111011
the dec value of the OR number is :                      1275
AND'd number based off your numbers in binary is :       00001011011
the dec value of the AND number is :                     91

Why is it so stupidly big, because all the calculations within the code are binary, it will work for upto 32 bit numbers. I could have done a lot of code reuse but because of the issues above i couldn't do that,

Now its 1:00am and time for bed..... work in 7 hours :smile:
 
Last edited by a moderator:
You have to think a bit different in C++ than in perl and you're also much nearer the hardware, but you'll eventually write shorter code than that if you continue exercising. :)
 
You have to think a bit different in C++ than in perl and you're also much nearer the hardware, but you'll eventually write shorter code than that if you continue exercising. :)

While im sure that code could be written much better, the premise of the task was just dumb and wrong :p.

But i haven't really moved past C yet. I figured it's worth writing a bit of code to get myself familiar. Then move onto the more complex c++ paradigms. I guess at that point before i get to ingrained with bad practices it would be worth learning how to write code that's intrinsically parallel.

I guess another useful thing to do might be to write some simple code compile it and then decompile it and see what it did to create efficient machine code.
 
But i haven't really moved past C yet.

Learning C before C++ is a sure way to dangerous territory (yes, I know Unis and a throng of books do that, and show you naked ptrs and arrays before they show you vectors). My humble opinion is that for your goals it'd be better to employ one of the better C++ learning books, for example Koenig's Accelerated C++ or Lippman's Essential C++...if C++ is your focus.
 
Davros, I appreciate lulz as much as the next guy, but beyond a certain threshold just lulzing for the lulz of lulz turns into noise. Moreover, the premise of the thread is serious, whereas "Teach yourself XYZ in 24 hours" is not.
 
Davros, I appreciate lulz as much as the next guy, but beyond a certain threshold just lulzing for the lulz of lulz turns into noise. Moreover, the premise of the thread is serious, whereas "Teach yourself XYZ in 24 hours" is not.

I've seen basically the whole world praise Accelerated C++ as a book. But i thought it was more an intermediate level knowledge kind of book? Do you think its worth jumping straight into it? I have also heard quite a few people praise this book Beginning C++ Through Game Programming which a lot of people like because all the examples are game orientated.
 
I've seen basically the whole world praise Accelerated C++ as a book. But i thought it was more an intermediate level knowledge kind of book? Do you think its worth jumping straight into it? I have also heard quite a few people praise this book Beginning C++ Through Game Programming which a lot of people like because all the examples are game orientated.

It's not intermediate level knowledge (I would argue), and IMHO it's a pretty good book for starting up with C++. I would argue that it embeds good practices from the get go. I've not read that particular book, so I won't comment on it. Another reasonable book would be Bjarne's Programming Principles and Practice, but that's likely to cost you a wee bit more, and is far chunkier (it also gets you doing more stuff). You can get a hint WRT what's in the latter book over here: http://www.stroustrup.com/Programming/lecture-slides.html.

Also Lippman's C++ Primer 5th Ed. is supposed to come out soon and it's supposed to be fully C++11, but that's even more chunky and I'm not sure you'd care to go through so much, whereas Accelerated C++ and Essential C++ are pretty slim at under 300 pages (last time I checked). Basically, I believe that Bjarne is right when he talks about how modern C++ should be taught / written, and starting from C doesn't necessarily give you the most bang for the buck when aiming to learn C++.
 
Bought a copy of Accelerated C++ , did a couple of chapters last night. After the very first hello world program it started using data types and operations i haven't used yet. The way the book explains side effects and the semicolon confused me a little but it seems very good and you are right its not an "intermediate level book at all". At least so far.
 
i've been going through Accelerated C++ and one thingy i have noticed without going into to much detail is the writers constant use of "constant". Is there any other advantage to it other then to stop yourself overwriting the initial value?


cheers
 
It establishes a contract in the interface, i.e. when I look at the function definition in the header file I know that the function doesn't change a value.
Theoretically it gives the compiler an opportunity to do optimizations, practically this is rarely actually true.
"const" is one if the least well though out parts of C++, the problem is if you're not absolutely const correct at the bottom, you run into issues being const correct above.
It's not uncommon on larger projects to notice a function argument ought to be const, try and make the change, spend an hour or more trying to fix the fallout , then just revert the change because you end up fixing half the code base.
Then you run into the issue with member functions which are semantically const, but not actually const, the classic example being a member function that caches a result. Of course that is what the mutable keyword is for...

IMO everything should be const unless it's declared mutable.

In practice I consider myself a const pragmatist, I'll declare it the way it should be unless it's painful to make the changes required.
 
Back
Top