d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Just Started Learning C++ > I Don't Know Why This Program Doesn't
Add Reply New Topic New Poll
Member
Posts: 7,986
Joined: May 11 2011
Gold: 1,000.00
Aug 29 2013 07:17pm
I just started learning c++
i'm using microsoft visual c++ 2010 express (the free one)
I bought a book "sams teach yourself c++ fifth edition"

when I try to run this program it says endl; and end; are errors I know what the program does, but I do not know why it shows errors in the program i copied it straight from the book

#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl ;
std::cout << x + y << " " << x * y;
std::cout << endl ;
return 0;
}

will give fg for the answer!

I know endl is suppose to make it on a different line, but it does not work!

This post was edited by STL4jsp on Aug 29 2013 07:21pm
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Aug 29 2013 07:38pm
You have to put the std:: in front of endl also with microsoft visual 2010

Or you can just put
Code
using namespace std;
after the include so you don't need to put std:: every time

This post was edited by ShadowFiend on Aug 29 2013 07:39pm
Member
Posts: 7,986
Joined: May 11 2011
Gold: 1,000.00
Aug 29 2013 08:11pm
Quote (ShadowFiend @ Aug 29 2013 07:38pm)
You have to put the std:: in front of endl also with microsoft visual  2010

Or you can just put
Code
using namespace std;
after the include so you don't need to put std:: every time


oh I see that now thank you yea I think this book is old my brother used it a few years back thanks you!
Member
Posts: 1,087
Joined: May 2 2005
Gold: 75.00
Oct 2 2013 10:22pm
using namespace std; Is ok for playing around on simplistic tasks but if you do not wish to run into conflicts down the road you should learn about conflicts that arise from using statements.

When you use the #include <iostream> it is a preprocessor directive that is including the library that houses i/o features.

the :: is called the scope resolution operator and is used to let the program know where you are getting the information from.

std::cout is stating that it is from the standard library.
std::endl is also coming from the same place.

if i wrapped some of my global functions in a namespace I would have to do something similiar to use them

namespace example
{
//Functions in here
}

When I went to use the item in main for instance I would be typing example::Function_Name;
So lets say cout was in the example namespace (its not but for demonstration purposes), I could then write example::cout << "text";
Alternatively I could you a using statement to remove the example portion to just use cout but it could have cnoflicts if there was multiple files with functions named cout

If you are insistent about using statements then make sure to use them in a local scope not a global scope or use fully qualified names like std::cout
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll