EvilZone
Programming and Scripting => C - C++ => : rasenove March 24, 2013, 05:58:02 PM
-
Hi guys, i started learning c++, just needed a little help with a problem of mine.
Im having some problem with the "cout" statements, (im doing this with code::block)
if i code this,
#include <iostream>
int main()
{
using namespace std;
cout << " Whats up doc ? " << end1;
return 0;
}
the compiler complains that "end1" was not declared, where i learned that end1 is included in "namespace std"
so i rewrite line the #5 like this,
cout << " Whats up doc ? \n";
and it compiles perfectly. but if i do this to cout an expretions end result like,
#include <iostream>
int main()
{
using namespace std;
cout << 2 + 5 \n;
return 0;
}
i get error msg "stray '\' in program" (Dont even know what that means)
so i compiled it without using anything at the end of line 5 (and 6),
#include <iostream>
int main()
{
using namespace std;
cout << 2 + 5;
cout << 2 + 8;
return 0;
}
compiles okay but, shows the result(in comand line) like
710
where i wanted to see
7
10
i tried alot to solve this myself but failed every time.
thanks, hope you wont be annoyed for this.
-
It isn't end1 but endl.
Why is "using namespace std;" within "int main()"?
The reason why it appears as "710" is because you haven't included a end line or new line keyword after the "2 + 5".
-
Hi Zesh,
thanks for the fast reply, i got it now. i was confused with l and 1 becaus the fonts used there made l look like 1.
anyways what do you mean by 'Why is "using namespace std;" within "int main()"?'
its like that everywhere from learncpp.com to c++ primer plus.
-
anyways what do you mean by 'Why is "using namespace std;" within "int main()"?'
He means that the namespace directive should be outside of the function, with the headers.
-
I did that becaus the books and resourses showed me to use that statement that way.(putting namespace std in every function that has cout/cin in it ) But this is a much better way, thanks for the tip.
-
Nobody answered the part where you asked about: i get error msg "stray '\' in program" (Dont even know what that means)
\n can only be called within a string. So when it says stray \, it means its not supposed to be there. had you concatenated the equation with a string, "\n", then it should have worked.
-
Nobody answered the part where you asked about: i get error msg "stray '\' in program" (Dont even know what that means)
\n can only be called within a string. So when it says stray \, it means its not supposed to be there. had you concatenated the equation with a string, "\n", then it should have worked.
Uriah is right, \n can be applied only inside the quotes(double or single, that's upto you what you use).
For your doubts,
1.) In the first program,there's nothing like end1 that is endl.
2.)You should use using namespace std outside any function so that any function can use it not main() specifically.
3.)What you want the program to output will be if you'll add endl or \n in every cout statement cause if you don't do that C++ compiler thinks of both the statements as single so you'll have to add \n or endl statement.I have changed your code a bit so that you can get the output what you want.
#include <iostream>
using namespace std;
int main(void) {
cout << 2 + 5 << "\n" ;
cout << 2 + 8 << "\n" ;
return 0;
}
-
Yes thank you paradox, but why did you put void parameter inside main() s declaration ? (or should i say main() directive O.o)
-
Yes thank you paradox, but why did you put void parameter inside main() s declaration ? (or should i say main() directive O.o)
As stated by 0poiter
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.
-
2.)You should use using namespace std outside any function so that any function can use it not main() specifically.
I disagree, if you only use cout in one function don't declare it outside of everything, only declare that you are using it within that function because:
1) It's neater and easier to say when looking at the code "Oh okay, every time I see x function here I'm using the std version"
2) As hinted at in my last point, what if you have this example?:
namespace space1
{
void test() {}
};
namespace space2
{
void test() {}
};
using namespace space1;
using namespace space2;
test(); //Which does it call?
The answer is you will get an error on this code. You will have to do one of the following options:
space1::test(); //Option 1 use the full name
{
using namespace space1;
test();
} //Option 2 use a block
My point here is that you should never tell someone to just "use it only at the beginning" because there are circumstances where that advice is wrong.
@OP Follow what your book is doing. Unless you specifically know there won't be conflicts then only use it in blocks. When you start to get into namespace declarations this will start to be very evident that your book taught you good ways of coding (at least in this respect idk never read the book).
-
for using namespace std;
It really depends where you want to put it. It deals with scope.
putting using namepsce std; above int main() makes it file scope, meaning that the std namespace will be applied to everything outside the main function (other functions as well).
Putting it inside the main, means that it will be only applied WITHIN the main function. And not to any other functions.
The best practice in reality is to do std::cout <<
Since that is too hard to type std:: you can do this:
using std::cout;
and put that in your main().
Reason for this is that then there will be no namespace conflicts. Since you are working with small and simple projects, you may only want to keep it at using namespace std;
Once you start working on large projects with multiple namespaces, you will want to restrict your use of using namespace std; and start doing things like std::cout<< or using std::cout;
-
it is endl not end1 .
"using namespace std;" gives std file scope , means anything in that file can use the names declared in std namespace without mentioning . If you don't use 'using namespace std' then you will have to manually do this :
std::cout //because cout declared as an object of ostream in namespace std
std::cin//same reason , an object of istream
std::endl//same reason
If you are not going to use other namespaces or at this stage(beginning) you can use "using namespace std" and then just go with
cout
cin
endl
etc
But it is not recommended for later .
-
Is it our old friend? welcome back.
were have ya bin?
also about the "std::", i read about it that its the standard way and "using namespace std; " is considered as the lazy way. But dont know why, would be great if you(or somebody) cleared that up.
-
Is it our old friend? welcome back.
were have ya bin?
also about the "std::", i read about it that its the standard way and "using namespace std; " is considered as the lazy way. But dont know why, would be great if you(or somebody) cleared that up.
Thanks, lol .
Coming back to "using namespace std;" , yes its a lazy approach . As I said , it is okay and convenient to use this at beginner level but it is NOT recommended later(on intermediate/advanced level).You create your own namespaces and maybe you might have another cout object of some other class derived from osteam. Then you will not be "using namespace std" but instead
std::cout;//cout of std
my_namespace::cout; //cout declared in your own namespace
But since he is just beginning , that doesn't really matter (at this stage) .
EDIT:
:: is the scope resolution operator , std::cout means cout 'belongs' to namespace 'std' . It is used for nested classes too(if the nested class is in public section)
base_class::nested_class(arguments);
A namespace is where you 'declare' names of variables and classes
-
I have changed your code a bit so that you can get the output what you want.
#include <iostream>
using namespace std;
int main(void) {
cout << 2 + 5 << "\n" ;
cout << 2 + 8 << "\n" ;
return 0;
}
I am also learning C++ at the college, but they never used "using namespace std".
The program that I learnt was very simple and something like this :
#include<iostream.h>
void main()
{
cout<<2 + 5 <<endl;
cout<<5 + 8 <<endl;
}
You can try this also.
-
I am also learning C++ at the college, but they never used "using namespace std".
The program that I learnt was very simple and something like this :
#include<iostream.h>
void main()
{
cout<<2 + 5 <<endl;
cout<<5 + 8 <<endl;
}
You can try this also.
I bet your code can 'not' work . You didn't use either "using namespace std" or "std::cout/endl" . There is no way that code should work .
-
@ Sudeep, Your code is Wrong because,
1. main() always returns a value 0 to the caller (OS) at the end if everything goes okay. which is why we use "int", because main() returns an integer value (0) to the caller. but void functions dont return any value.
2. This is c++ where you dont have to include the header files (that comes with the compiler) extensions. my minGW compiler complains about that.(most compilers will too)
yor using the "c" standard in c++.
3. you didnt include the std, not including it makes cout and endl undefined so the compiler shouts at that too.
4. You didnt put any return statement at the end of main(). the
return 0;
is for informing the caller that everything went fine, if not it returns another integer value. this is a very bad mistake, because the compilers dont give an error msg for not stating return 0;
Seems like your college is teaching the old standards of c++.
-
@ Sudeep, Your code is Wrong because,
1. main() always returns a value 0 to the caller (OS) at the end if everything goes okay. which is why we use "int", because main() returns an integer value (0) to the caller. but void functions dont return any value.
2. This is c++ where you dont have to include the header files (that comes with the compiler) extensions. my minGW compiler complains about that.(most compilers will too)
yor using the "c" standard in c++.
3. you didnt include the std, not including it makes cout and endl undefined so the compiler shouts at that too.
4. You didnt put any return statement at the end of main(). the
return 0;
is for informing the caller that everything went fine, if not it returns another integer value. this is a very bad mistake, because the compilers dont give an error msg for not stating return 0;
Seems like your college is teaching the old standards of c++.
Using the "void" before function main means that the function main() does not return any value so there is no need of the return() inside the main.
I use Turbo C++.
-
@ Sudeep, Your code is Wrong because,
1. main() always returns a value 0 to the caller (OS) at the end if everything goes okay. which is why we use "int", because main() returns an integer value (0) to the caller. but void functions dont return any value.
2. This is c++ where you dont have to include the header files (that comes with the compiler) extensions. my minGW compiler complains about that.(most compilers will too)
yor using the "c" standard in c++.
3. you didnt include the std, not including it makes cout and endl undefined so the compiler shouts at that too.
4. You didnt put any return statement at the end of main(). the
return 0;
is for informing the caller that everything went fine, if not it returns another integer value. this is a very bad mistake, because the compilers dont give an error msg for not stating return 0;
Seems like your college is teaching the old standards of c++.
Since the main function is defined with return type void , returning a value here does not make sense.(but yes a bad habit I believe ).And you don't include header files extensions ? Did you mean the iostream ? I've been including it because it would give the following error when iostream is not inlcuded
"cout undefined , cin undefined " - because ostream and istream classes are defined in iostream.h/iostream.But I used devcpp(which is outdated) back then and made a habit to include it . I shall check this out with visual c++ if that is really the case and see if not including iostream works .
-
Using the "void" before function main means that the function main() does not return any value so there is no need of the return() inside the main.
I use Turbo C++.
Most compilers wont let you get away with it if you dont state main() as an int function.
@ pllaybuoy; Yes i ment <iostream> when i said " header files that are provided by the compilers". modern compilers wont recognise <iostream.h> .(e.g. my minGW didnt)
-
Most compilers wont let you get away with it if you dont state main() as an int function.
@ pllaybuoy; Yes i ment <iostream> when i said " header files that are provided by the compilers". modern compilers wont recognise <iostream.h> .(e.g. my minGW didnt)
Hi , I am using visual studio IDE nd have mingw and turbo compilers on my hand
#include <iostream.h> gave error but #include <iostream> worked perfectly fine .
-
Hi , I am using visual studio IDE nd have mingw and turbo compilers on my hand
#include <iostream.h> gave error but #include <iostream> worked perfectly fine .
Thats my point. what do you want to say now?
-
Thats my point. what do you want to say now?
Well , I am not really getting your point here ,bro . The .h extension was reserved for those header files which were imported from C without any change while the C++ header files were decided to have no extension . So you can either use <iostream.h>(old C++ books have iostream.h in their text) or you can do <iostream> .
Either way you have to include this header file(in most cases) so it is safe to assume that without this you will get compiling error .
-
Consider this code snippet,
#include <iostream> // header file that comes with compiler.
#include <iostream> // old c++ style (out dated), modern c style
#include "header.h" // header file that the coder wrote.
blah..........
blah............
blah...............
Im saying you should drop your old programming habits and start using the (modern) standards, hope youll get me now.
-
Consider this code snippet,
#include <iostream> // header file that comes with compiler.
#include <iostream> // old c++ style (out dated), modern c style
#include "header.h" // header file that the coder wrote.
blah..........
blah............
blah...............
Im saying you should drop your old programming habits and start using the (modern) standards, hope youll get me now.
Alright , dude