Author Topic: problem with Cout statement  (Read 5113 times)

0 Members and 1 Guest are viewing this topic.

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
problem with Cout statement
« on: 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,

Code: (C++) [Select]
#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,

Code: (C++) [Select]
cout << " Whats up doc ? \n";

and it compiles perfectly. but if i do this to cout an expretions end result like,

Code: (C++) [Select]
#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),

Code: (C++) [Select]
#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
Code: [Select]
710 where i wanted to see
Code: [Select]
7
10

i tried alot to solve this myself but failed every time.
thanks, hope you wont be annoyed for this.
My secrets have secrets...

Offline Zesh

  • Royal Highness
  • ****
  • Posts: 699
  • Cookies: 42
    • View Profile
Re: problem with Cout statement
« Reply #1 on: March 24, 2013, 06:14:57 pm »
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".

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: problem with Cout statement
« Reply #2 on: March 24, 2013, 06:55:03 pm »
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.
My secrets have secrets...

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: problem with Cout statement
« Reply #3 on: March 24, 2013, 07:21:52 pm »
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.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: problem with Cout statement
« Reply #4 on: March 24, 2013, 08:02:04 pm »
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.
My secrets have secrets...

Offline Uriah

  • Sir
  • ***
  • Posts: 454
  • Cookies: 42
  • άξονας
    • View Profile
Re: problem with Cout statement
« Reply #5 on: March 24, 2013, 10:31:56 pm »
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.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: problem with Cout statement
« Reply #6 on: March 25, 2013, 09:04:39 am »
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.
Code: (C) [Select]
#include <iostream>
using namespace std;
int main(void) {
cout << 2 + 5 << "\n" ;
cout << 2 + 8 << "\n" ;
return 0;
}

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: problem with Cout statement
« Reply #7 on: March 25, 2013, 10:00:15 am »
Yes thank you paradox, but why did you put void parameter inside main() s declaration ? (or should i say main() directive O.o)
My secrets have secrets...

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: problem with Cout statement
« Reply #8 on: March 25, 2013, 10:27:53 am »
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.

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: problem with Cout statement
« Reply #9 on: March 26, 2013, 09:39:37 pm »
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?:

Code: [Select]
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:

Code: [Select]

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).
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline kelvinsilva-katokato

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: problem with Cout statement
« Reply #10 on: March 27, 2013, 10:13:05 pm »
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;

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #11 on: April 03, 2013, 06:58:08 pm »
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 .
« Last Edit: April 03, 2013, 07:02:05 pm by pllaybuoy »
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: problem with Cout statement
« Reply #12 on: April 03, 2013, 07:43:39 pm »
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.
My secrets have secrets...

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #13 on: April 03, 2013, 08:01:00 pm »
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
« Last Edit: April 03, 2013, 08:04:00 pm by pllaybuoy »
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"

Offline sudeep

  • Serf
  • *
  • Posts: 30
  • Cookies: -5
    • View Profile
Re: problem with Cout statement
« Reply #14 on: April 03, 2013, 09:01:38 pm »
I have changed your code a bit so that you can get the output what you want.
Code: (C) [Select]
#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 :
Code: (C) [Select]
#include<iostream.h>
void main()
{
cout<<2 + 5 <<endl;
cout<<5 + 8 <<endl;
}
You can try this also.
Better know it and don't need rather than need it and don't know it