Question:

Could someone help with this C++ problem?

by  |  earlier

0 LIKES UnLike

Just doing an exercise. This is the code:

===

#include <iostream.h>

int main()

{

int x = 5;

int y = 7;

cout "\n";

cout << x + y << " " << x * y;

cout "\n";

return 0;

}

===

I keep getting the following error when compiling it using the following command in Unix:

g++ guess.cpp

or

c++ guess.cpp

Error:

guess.cpp: In function `int main()':

guess.cpp:6: parse error before string constant

====

Any help would be appreciated! 10 Points for the best answer! ;-)

 Tags:

   Report

4 ANSWERS


  1. some systems require you to add

    int main();

    to show that it is the first function to be used.

    also, before you write &#039;cout&#039; you must have std:: so your code should look like this

    #include &lt;iostream.h&gt;

    int main();

    int main()

    {

    int x = 5;

    int y = 7;

    std::cout &lt;&lt; &quot;\n&quot;;

    std::cout &lt;&lt; x + y &lt;&lt; &quot; &quot; &lt;&lt; x * y;

    std::cout &lt;&lt; &quot;\n&quot;;

    return 0;

    }

    i know the std:: thing can get annoying so you can also do this:

    #include &lt;iostream.h&gt;

    int main();

    int main()

    {

    using namespace std;

    int x = 5;

    int y = 7;

    cout &lt;&lt; &quot;\n&quot;;

    cout &lt;&lt; x + y &lt;&lt; &quot; &quot; &lt;&lt; x * y;

    cout &lt;&lt; &quot;\n&quot;;

    return 0;

    }

    that way you never have to add std:: infront of your cout or cin commands


  2. I woule help if I knew what the heck that even means. Sorry

  3. There should be an &lt;&lt; before those &quot;\n&quot;s on lines 6 and 8.

    Edit: about the std namespace:

    You&#039;re including &lt;iostream.h&gt;, which is a backwards compatibility header that automatically places everything in the global scope. It&#039;s a bad idea to use &lt;iostream.h&gt;, though, since it&#039;s not guaranteed to exist on all compilers. Better to use &lt;iostream&gt; and then use &quot;using std::cout;&quot; or similar things.

  4. Your &#039;cout&#039; statements require &#039;&lt;&lt;&#039;. e.g.

    #include &lt;iostream.h&gt;

    int main()

    {

    int x = 5;

    int y = 7;

    cout &lt;&lt; &quot;\n&quot;;

    cout &lt;&lt; x + y &lt;&lt; &quot; &quot; &lt;&lt; x * y;

    cout &lt;&lt; &quot;\n&quot;;

    return 0;

    }

    Might also be worth adding:

    using namespace std;

    after you include iostream.h.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.