Code eror

Discussion in 'Mac Programming' started by Kwokho, Aug 21, 2008.

  1. Kwokho macrumors newbie

    Joined:
    Aug 20, 2008
    #1
    I am new so please tell me what had I done wrong. I am doing some tutorials from a book and this is the code

    // Listing 2.2 using std::cout
    #include <iostream>
    int main()
    {
    std::cout << "Hello there.\n";
    std::cout << "Here is 5: " << 5 << "\n";
    std::cout << "The manipulator std::end1 ";
    std::cout << "Write a few line to the screen.";
    std::cout << std::end1;
    std::cout << "Here is a very big number:\t" << 70000;
    std::cout << std::end1;
    std::cout "Here is a sum of 8 and 5:\t";
    std::cout << 8+5 << std end1;
    std::cout << "Here's a fraction:\t\t";
    std::cout << (float) 5/8 << std::end1;
    std::cout << "And a very big number:\t";
    std::cout << (double) 7000 * 7000 << std::end1;
    std::cout << "Don't forgot to replace the Jeese Liberty";
    std::cout << "With your name...\n";
    std::cout << "Kwokho is a C++ programmer!\n";
    return 0;
    }



    And it give me errors like

    UsingCout.cpp: In function 'int main()':
    UsingCout.cpp:9: error: 'end1' is not a member of 'std'
    UsingCout.cpp:11: error: 'end1' is not a member of 'std'
    UsingCout.cpp:12: error: expected `;' before string constant
    UsingCout.cpp:13: error: expected primary-expression before 'end1'
    UsingCout.cpp:13: error: expected `;' before 'end1'
    UsingCout.cpp:15: error: 'end1' is not a member of 'std'
    UsingCout.cpp:17: error: 'end1' is not a member of 'std'

    Please tell me what had i done wrong.
    thanks
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
  3. Kwokho thread starter macrumors newbie

    Joined:
    Aug 20, 2008
  4. Kwokho thread starter macrumors newbie

    Joined:
    Aug 20, 2008
    #4
    it got a new eror

    UsingCout.cpp: In function 'int main()':
    UsingCout.cpp:12: error: expected `;' before string constant
    UsingCout.cpp:13: error: expected primary-expression before ';' token
    UsingCout.cpp:13: error: 'endl' was not declared in this scope
     
  5. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    Since you changed the code, post your new code so we can see it. There are numerous errors (compile-time and at least one runtime) with your first posting.

    If you add a
    Code:
    using namespace std ; 
    
    to the top of your source under the #include, the error with endl will most likely go away. Or, you can code std::endl like you did with all your cout instructions to fix it too.

    And BTW, use [code] & [/code] tags to wrap your code please.
     
  6. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    Hi

    You're missing a << in this line:-

    Code:
    std::cout "Here is a sum of 8 and 5:\t";
    
    Also,

    Code:
    std::cout << "Kwokho is [B]nearly[/B] a C++ programmer!\n";
    
    ;)

    b e n
     
  7. darkwing macrumors 65816

    Joined:
    Jan 6, 2004
    #7
    My co-worker and I always felt that using "using" was lazy. Of course, you're not likely to have collisions with names in std but it does remove a layer of encapsulation that aids in readability. Just my .02. :)
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    I go both ways on this. For my C++ stuff, I usually use it and occasionally don't (and use the full std::... syntax.). I haven't ventured into other namespaces yet, so from that perspective, using using is simple.

    In Java, (which could be argued has nothing to do with this), I try to keep my import statements minimal, if at all, and use full blown path names to Classes and methods. I figured it would help me learn the API's better. And it has to an extent.

    Todd
     
  9. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    It's obviously lazy, the question is: Is it useful or not?

    I personally think the more information there is in the code that tells me exactly on the first glance what some identifier exactly means, the better. Writing std::endl instead of endl takes me about half a second. Not realising that say "cos" is not the standard cosine function but something completely different can take a long, long time. Especially when you are looking for a bug in your code, and you know exactly that something in your code doesn't do what you think it does or doesn't mean what you think it means, so you have to check everything, in that situation adding std:: can really save you time.

    In the original post, I think it is just about possible that a program uses variables end1, end2, end3 for example. So while std::end1 definitely gives an error, just writing end1 might compile (and likely do something unexpected).
     
  10. darkwing macrumors 65816

    Joined:
    Jan 6, 2004
    #10
    Todd, I used to use the import statements in Java quite often because I wasn't aware there was any other way. Once I learned better, I was doing the same as you.

    gnasher, I agree with what you're saying. Making your own "endl" for example would be weird, and could possibly make your code a bit harder to read. However, if you always spell out the namespace you avoid that problem. It also becomes easier to read, imho. Especially for a beginner who may know about "cout" and "cin" but not "cerr" or something.
     

Share This Page