Thursday, July 30, 2009

C++ question?

Input stream buffer contains


22 34.5\nHello A\nWorld 34 45 //\n represents new line chara


What is the output to the terminal when the code segment is executed





Code


int m; float x; string text; char ch;


cin %26gt;%26gt; m %26gt;%26gt; x;


getline(cin,text);


cin.get(ch);


cout %26lt;%26lt; m %26lt;%26lt; "-" %26lt;%26lt; x %26lt;%26lt;"-" %26lt;%26lt; text %26lt;%26lt; "-" %26lt;%26lt; ch %26lt;%26lt; endl;





Can one of you programmers help with this

C++ question?
You could just compile %26amp; run this and get the answer.





1) I typed up this program.


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


int m; float x; string text; char ch;





cout %26lt;%26lt; "Enter stuff:\n";


cin %26gt;%26gt; m %26gt;%26gt; x;





getline(cin,text);





cin.get(ch);





cout %26lt;%26lt; m %26lt;%26lt; "-" %26lt;%26lt; x %26lt;%26lt;"-" %26lt;%26lt; text %26lt;%26lt; "-" %26lt;%26lt; ch %26lt;%26lt; endl;





return 0;


}





2) I compiled it with g++.





3) I ran the executable and I had the answer within seconds.
Reply:Trace it or run it.


Tracing it is easy. match the variables in with the stream of characters. EX:


m will get 22


x will get 34.5 Notice x is also a float (real number)


So trace and tell the result.


Why didn't you try running it/????


No comments:

Post a Comment