Sunday, July 12, 2009

Beginner's C++ Programming Output?

Hey, all you smart people. I have a simple question for you that i cannot understand. I was reading my C++ reference guide at it gives me the following statement:





n = 7


cout %26lt;%26lt; n %26lt;%26lt; n+9;





It then lists the output as: 716





Is this an error by the publisher, or am I misunderstanding something because I think the answer is 16. Tell me if i am wrong, where I am going and give another example, please. Thanks in advance for all the help and those who answer. I look forward to reading your answers.





Waiting in anticipation,


anishannayya

Beginner's C++ Programming Output?
Yes, I bet this is your very first C++ program right?





let's analyze your simple statement of code:


n=7;


so...


substitute all n's to 7...


therefore


cout %26lt;%26lt; 7 %26lt;%26lt; 7 + 9


cout means display...


display %26lt;%26lt; 7 %26lt;%26lt; 16


since there are no escape sequence (e.g. \n or \b or \t) or a space (" ") then


716 is the result ...





In order for you to separate the two numbers, you can....


1. cout %26lt;%26lt; n %26lt;%26lt; " " %26lt;%26lt; n + 9; // result is 7 16


2. cout %26lt;%26lt; n %26lt;%26lt; "\n" %26lt;%26lt; n + 9;


// result is


// 7


// 16
Reply:cout %26lt;%26lt; n; this means to output the value of 'n'


cout %26lt;%26lt; n + 9; // this means to output the value of 'n' + 9


cout %26lt;%26lt; n %26lt;%26lt; n+9; // this means to output the value of n, and then


output the value of 'n+9'.





See, try this:


cout %26lt;%26lt; n %26lt;%26lt; "\n" %26lt;%26lt; n + 9;


// This will put 'n+9' on the next line. :)





Good Luck!


-Andy
Reply:yeah it is "concatenating" the statement.





it outputs 7 and 16.
Reply:You're right its 16


No comments:

Post a Comment