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

0 Members and 1 Guest are viewing this topic.

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #15 on: April 03, 2013, 09:10:10 pm »
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.

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 .
"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 #16 on: April 04, 2013, 07:08:27 am »
@ 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++.
My secrets have secrets...

Offline sudeep

  • Serf
  • *
  • Posts: 30
  • Cookies: -5
    • View Profile
Re: problem with Cout statement
« Reply #17 on: April 04, 2013, 08:55:38 am »
@ 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++.
Better know it and don't need rather than need it and don't know it

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #18 on: April 04, 2013, 10:17:04 am »
@ 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 .
"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 #19 on: April 04, 2013, 03:25:29 pm »

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)
My secrets have secrets...

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #20 on: April 06, 2013, 06:25:14 pm »
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 .
« Last Edit: April 06, 2013, 06:27:04 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 #21 on: April 06, 2013, 06:43:00 pm »
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?
My secrets have secrets...

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #22 on: April 06, 2013, 09:12:41 pm »
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 .
"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 #23 on: April 07, 2013, 07:05:39 am »
Consider this code snippet,
Code: (c++) [Select]
#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.
My secrets have secrets...

Offline pllaybuoy

  • Peasant
  • *
  • Posts: 70
  • Cookies: -3
    • View Profile
Re: problem with Cout statement
« Reply #24 on: April 07, 2013, 10:46:23 am »
Consider this code snippet,
Code: (c++) [Select]
#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
"Monsters are real , ghosts are real too . They live inside us and sometimes they win"