Pls Tell me How to achive this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjuks
    New Member
    • Dec 2007
    • 72

    Pls Tell me How to achive this

    Hi,

    int main()
    {
    cout<<"World";
    return 0;
    }

    output should be Hello World.
    How to achive this. This was asked in interview. I could not answer. So please help me.

    Thanks,
    Manju
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    #2
    Originally posted by manjuks
    Hi,

    int main()
    {
    cout<<"World";
    return 0;
    }

    output should be Hello World.
    How to achive this. This was asked in interview. I could not answer. So please help me.

    Thanks,
    Manju
    I guess either u have done a mistake in posting a code or have not read it properly;
    it's written as cout<<"World"; so it's but obvious that it will show output as World!
    You will just have to write;

    int main()
    {
    cout<<"Hello World";
    return 0;
    }

    Comment

    • manjuks
      New Member
      • Dec 2007
      • 72

      #3
      Originally posted by Parul Bagadia
      I guess either u have done a mistake in posting a code or have not read it properly;
      it's written as cout<<"World"; so it's but obvious that it will show output as World!
      You will just have to write;

      int main()
      {
      cout<<"Hello World";
      return 0;
      }
      Hi,

      Thanks for your reply. What i posted is correct. In interview the interviewer asked me to do this if i know C++ better. I will put his words once again. He said there is no line after cout<<"World"; and before too. I said its not possible. He said its possible but didn't tell me the solution.

      Thanks,
      Manju

      Comment

      • Parul Bagadia
        New Member
        • Mar 2008
        • 188

        #4
        Originally posted by manjuks
        Hi,

        Thanks for your reply. What i posted is correct. In interview the interviewer asked me to do this if i know C++ better. I will put his words once again. He said there is no line after cout<<"World"; and before too. I said its not possible. He said its possible but didn't tell me the solution.

        Thanks,
        Manju
        yaeh that's right;
        there is no need of having a line before cout and after cout;
        cout or insertion operator, puts the value of right hand variable in left operand;
        in short the code should have been

        cout<<"Hello world";

        Comment

        • JonLT
          New Member
          • Jul 2007
          • 41

          #5
          you properly DO need a "newline" at the end of your cout line.
          It could be a "\n" or a <<endl;

          The reason is the the newline flushes the stdout buffer. The text isn't printed right away, but stored in the stdout buffer, so if the program terminates before the buffer is flushed the output isn't printet
          [code=cpp]
          cout<<"Hello World!"<<endl;
          [/code]
          or
          [code=cpp]
          cout<<"Hello World!\n";
          [/code]
          or you could flush the buffer by hand:
          [code=cpp]
          cout<<"Hello World";
          fflush(stdout);
          [/code]

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            The following code compiles with Dev-C++ (v 4.9.9.2) but some compilers require 'return 0;'
            Everything i have read and been told says the curly braces are mandatory in main() so a line is needed above and below the cout<< statement for them. If all the '\n' `s are deleted the code still compiles and runs as required.
            But
            Code:
            cout<<"World";
            //will not print Hello World only World.
            [CODE=ccp]
            #include<iostre am>
            using namespace std;

            int main()
            {
            cout<<"Hello World\n\n";
            cout<<"\n";
            system("pause") ;
            //return 0;
            } [/CODE=ccp]

            Comment

            Working...