Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kwokho

macrumors newbie
Original poster
Aug 20, 2008
6
0
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
 

Kwokho

macrumors newbie
Original poster
Aug 20, 2008
6
0
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
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
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.

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. :)
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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. :)

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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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. :)

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).
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.