Cprogram errrors in code::blocks in main.cpp

Status
Not open for further replies.

priyasood

Distinguished
Jan 24, 2011
1
0
18,510
Hello,
m begginer to c++, i've a problem in program in codeblocks..my program is simple and i've built it in main.cpp.its about circumference nd volume of the circle:
here is the program:
#include<stdio.h>
#include<conio.h>
#define PI 3.1427
void main()
{
int r;
float cir, vol;
cout<<"Enter the radius of sphere=";
cin>>r;
cir=4*PI*r*r;
vol=4/3*PI*r*r*r;
cout<<"\n circum of the sphere is="<<cir;
cout<<"\n volume of the sphere is="<<vol;
getch();
}
and the errors are:
'cout' was not declare in the scope
'cin' was not declared in the scope
error::'main' must return 'int'
in function 'int main()'

is there like to define objects in main.cpp and doing program in other file under sources.. plz help!!
heartlly thanks and sorry if i've done mistakes in wriiting msg..
 
1. You need to include the header file in which "cout" and "cin" are declared. This is "iostream". So at the head of the program you need the line:

#include <iostream>

You don't need to include "stdio.h" as you aren't using any functions declared there.

2. (Presuming your compiler is a reasonably recent one.) You need to specify the namespace for "cout" and "cin". This means that either you have to refer to them at "std::cout" and "std::cin" or you can include the line:

using namespace std;

after the "#includes".

3. Declare "main" as "int main()" not "void main" and add "return 0;" before the final closing brace.
 

_RoRo_

Distinguished
Dec 18, 2010
20
0
18,510
^ what that guy said will fix your syntax and allow you to compile.
As a general rule you shouldn't use .h libraries unless you actually need the functions there, most of those are C style.
Some more general pointers

1) You might want to use << endl instead of including the newline char in your strings
2) What's that getch() doing there?

For your other question I am assuming what you want to use separate compilation. For an example such as this it is hard to justify but here are the guidelines:

1) Make a new pair of files Calculation.cc and Calculation.h (for example)
2) Inside the .h you will have the function stubs so for example
float findCircum (float rad);
3) Inside the .cc you implement these functions, do not write a main() function there
4) In your original file include Calculation.h at the top
5) Now compile Calculation.C into an object using the -c option (you now have original 3 files + Calculation.o)
6) Link the original file with the .o to get the final executable
Alternatively a makefile can be used to compile using the make command.

Hopefully that is detailed enough for you =)
 
Status
Not open for further replies.