Someone help me understand this please!

acidrain

Executive Member
Joined
Jan 7, 2007
Messages
6,966
Reaction score
1,765
Location
At a computer
Ok so one of the questions in my assignment ask me to calculate the output of the following program:

#include <iostream>
using namespace std;

int main ( )
{
char c, e;
c = 'B';
e = 4 + c;

cout << 2 * c << " and " << e << " and " << e + c << endl;

system("PAUSE");
return EXIT_SUCCESS;
}


Now its all well and easy typing it into the program, running it and ending up with: 132 and F and 136 but i do not understand how it ends up with this conclusion.

I have found out how it gets 132 ( since the ASCII value ( Dec. ) for 'B' is 66 and 66 * 2 = 132 ) but i have no clue how it gets 'F' and '136'.

Help please!


EDIT: Ok i just worked out how it gets 136 ( e + c = 4 + 'B' + 'B' = 4 + 66 + 66 = 136 ) but now using the same method of sunstition e = 4 + c = 4 + 'B' = 4 + 66 = 70, but the program outputs F instead of 70.

Any Ideas why it doesnt output 70?
 
Last edited:
its a char not an int?

Ok, but 'c' has also been defined as a char aswell. Yet through c = B it can be converted to a number.

Just realised that the questions in the tut does not fall in line with the study guide, hence why i got confused... gotta do floats before i get to char.

Ne ways thanks, still not 100% clear of why but ill probably understand once ive done the chapter on char. If still unsure ill come back :D
 
cout << 2 * c << " and " << e << " and " << e + c << endl;

EDIT: Ok i just worked out how it gets 136 ( e + c = 4 + 'B' + 'B' = 4 + 66 + 66 = 136 ) but now using the same method of sunstition e = 4 + c = 4 + 'B' = 4 + 66 = 70, but the program outputs F instead of 70.

Any Ideas why it doesnt output 70?

Because it is still cast as a char.
The arithmetic operators are causing the other variables to be recast as ints.
 
Because it is still cast as a char.
The arithmetic operators are causing the other variables to be recast as ints.

Ok so because the statement c = 'B' was made, this immediately makes c an integer whereas no statement was made for e to = anything therefore remaining a character.

Ok, i finally get it now.... 'e' does = 70 in the equation but because it was declared as a char we have to use the ascii symbol for 70 which funny enough is 'F'.

Thanks guys,

Oh one final question. Do we always refer to the ascii table when declaring char's? Reason i ask is i never even knew i was supposed to use it until i phoned up my lecturer. I also hope they don't expect me to remember the whole ascii table :sick:
 
Oh one final question. Do we always refer to the ascii table when declaring char's? Reason i ask is i never even knew i was supposed to use it until i phoned up my lecturer. I also hope they don't expect me to remember the whole ascii table :sick:

Nope, you can strong quote like in the example you posted above:

int main ( )
{
char c, e;
c = 'B';
e = 4 + c;
.
.
.

Or you could define c like this:
Code:
char c = 66;

The result is the same. Try it out for yourself. Messing around with stuff like this is essential if you're ever going to understand the details.

As for remembering the ASCII table, you could write a small program that returns the ASCII code for a character if they won't let you use a table (just google and you'll find small programs that do this in several languages). Can't imagine why they'd not allow you to use a standard reference like an ASCII table in a lab or whatever.
 
the only person in the real world that would write code like that is someone trying to ensure job-security. I suppose it is good for demonstration purposes.

That looks a lot like the UNISA exercises... I need to get started on mine.
 
the only person in the real world that would write code like that is someone trying to ensure job-security. I suppose it is good for demonstration purposes.

That looks a lot like the UNISA exercises... I need to get started on mine.

I've been doing a lot of Assembly lately and it's actually quite common in Assembly language.

You can

mov al, '2'
or
mov al, 50

For the character 2, whereas the number 2 would simply be

mov al, 2

and to convert it to the character representation (assuming it's a single digit number, multiple digits are more complex)

add al, 48
OR
add al, '0'

If it makes any sense... The ASCII table is simply numbers that are "converted" to characters so that concept is carried over to C and therefore to C++

If you can, then do a computer architecture module, very enlightening as to how it really works.
 
Last edited:
I've been doing a lot of Assembly lately and it's actually quite common in Assembly language.

You can

mov al, '2'
or
mov al, 50

For the character 2, whereas the number 2 would simply be

mov al, 2

and to convert it to the character representation (assuming it's a single digit number, multiple digits are more complex)

add al, 48
OR
add al, '0'

If it makes any sense... The ASCII table is simply numbers that are "converted" to characters so that concept is carried over to C and therefore to C++

If you can do a computer architecture module, very enlightening as to how it really works.


Ok so when ever something is put in ' ' eg. 'B'... it is referring to its ascii value and not just simply B.
 
Ok so when ever something is put in ' ' eg. 'B'... it is referring to its ascii value and not just simply B.

Yep ;)

Remember characters have no meaning to a computer since it is a boolean logic machine, so the way they use to store characters is to define a table of numbers that will refer to certain characters (ASCII table).

So basically in C++ you could do the following:

Code:
#include <iostream>

int main()
{
	char characterH = 72; 
        /*ASCII Code for the character H*/
	std::cout << characterH << "\n";
        /*will print H*/

	int numberSeventyTwo = 'H'; 
        /*Now we'll do the reverse with a integer.*/
	std::cout << numberSeventyTwo << "\n"; 
        /*This will print 72 to the screen. The Integer is converted to it's ASCII representation by compiler.*/

	/*Now I'd like to do a simple sum 4 + 4.*/
	char simpleSum = 4;
        /* Use a char as a integer.
	simpleSum += 4;
  	/*simpleSum will now contain the integer value 8 or 1000 in binary*/

	std::cout << simpleSum << "\n"; 
	/*simpleSum will equal 8 which is the ASCII character backspace, :( but we want it to print 8 to the screen*/
	simpleSum += 48;
	/*If you check the ASCII table you'll notice that the ASCII character for a number is 48 ahead of it's actual integer value. In other words the ASCII value for 0 is 48, 1 is 49, 2 is 50, etc. etc.*/
	std::cout << simpleSum << "\n"; 
        /*prints 8 to the screen.*/

	/*Now to write a number from the ASCII table directly to the screen:*/
	std::cout << (char) 100 << "\n"; 
        /*This will print a 'd' which is at ASCII position 100.*/

	/*pause before exiting.*/
	std::cin.get();
	return 0;
}

Hopefully this explains better than what I did in the above example ;)
C/C++ converts integers to their character representations automatically when you print to screen but in Assembly you're not that lucky :) So if you have the number 13 you'll notice adding 48 to it won't yield characters 1 and 3, so this is where you'll need to do some arithmetic to convert that to a it's ASCII character representation.
 
Last edited:
Yep ;)

Remember characters have no meaning to a computer since it is a boolean logic machine, so the way they use to store characters is to define a table of numbers that will refer to certain characters (ASCII table).

So basically in C++ you could do the following:

#include <iostream>

int main()
{
char characterH = 72; //ASCII Code for the character H
std::cout << characterH << "\n"; //will print H
int numberSeventyTwo = 'H'; //Now we'll do the reverse with a integer.
std::cout << numberSeventyTwo << "\n"; //This will print 72 to the screen. The Integer is converted to it's ASCII representation by compiler.
/*Now I'd like to do a simple sum 4 + 4.*/
char simpleSum = 4;
simpleSum += 4;
/*simpleSum will now contain the integer value 8 or 1000 in binary*/
std::cout << simpleSum << "\n";
/*simpleSum will equal 8 which is the ASCII character backspace, :( but we want it to print
8 to the screen*/
simpleSum += 48;
/*If you check the ASCII table you'll notice that the ASCII character for a number is 48 ahead of it's actual integer value. In other words the ASCII value for 0 is 48, 1 is 49, 2 is 50, etc. etc.*/
std::cout << simpleSum << "\n"; //will print 8
/*Now to write a number from the ASCII table directly to the screen:*/
std::cout << (char) 100 << "\n"; //will print a 'd' which is at ASCII position 100.
//pause before exiting.
std::cin.get();
return 0;
}

Hopefully this explains better than what I did in the above example ;)
C/C++ converts integers to their character representations automatically when you print to screen but in Assembly you're not that lucky :) So if you have the number 13 you'll notice adding 48 to it won't yield characters 1 and 3, so this is where you'll need to do some arithmetic to convert that to a it's ASCII character representation.

Ok cool, got a little lost in some of the code but i at least i got what you meant. Shot :D
 
Ok cool, got a little lost in some of the code but i at least i got what you meant. Shot :D

:D k cool, yes c++ can be confusing at times :( And the formatting all went to hell when I copy pasted to the forum, so might make a bit of a hard read.
 
....And the formatting all went to hell when I copy pasted to the forum, so might make a bit of a hard read.

lol, ye i think i would fail if i submitted a prac with that kind of style format... i also see now why indents and spacing is key in a good programmer.
 
:D k cool, yes c++ can be confusing at times :( And the formatting all went to hell when I copy pasted to the forum, so might make a bit of a hard read.
If you use code tags, it maintains the formatting :)
 
If you use code tags, it maintains the formatting :)

:o k my bad, fixed it, looks a bit better now.

The next step would be to use methods with cleaner comments, but I find copy pasting into your favorite IDE works better ;) Then you can actually see the code working and all the other features provided by the IDE such as code highlighting, etc.
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X