Simple C++ review, errors

J

James

I have a couple errors that display when building any sample C++ code I
test.
(using Codeblocks)

Sample app:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int v,u,a,t;
cout << "Enter the velocity, acceleration, time as integers : " << endl;
cin>>u>>a>>t;
v=u+a*t;
cout << "The final velocity is " << v << "." << endl;
getch();
}

Result:
Process returned 0 (0x0) execution time : 0.000 s
Press any key to continue.

Build Messages/Errors:
C:\Program
Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\backward\backward_warning.h|32|warning:
#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the
C++ standard. Examples include substituting the <X> header for the <X.h>
header for C++ includes, or <iostream> instead of the deprecated header
<iostream.h>. To disable this warning use -Wno-deprecated.|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP|5|error:
`main' must return `int'|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP||In function
`int main(...)':|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP|6|error:
`clrscr' was not declared in this scope|
||=== Build finished: 2 errors, 1 warnings ===|

Regardless what I change it won't run properly, not prompting for
"Enter the velocity, acceleration, time as integers : "

Any suggestions appreciated.
 
P

Peter Remmers

Am 18.04.2011 16:50, schrieb James:
I have a couple errors that display when building any sample C++ code I
test.
(using Codeblocks)

Sample app:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int v,u,a,t;
cout<< "Enter the velocity, acceleration, time as integers : "<< endl;
cin>>u>>a>>t;
v=u+a*t;
cout<< "The final velocity is "<< v<< "."<< endl;
getch();
}

Result:
Process returned 0 (0x0) execution time : 0.000 s
Press any key to continue.

Build Messages/Errors:
C:\Program
Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\backward\backward_warning.h|32|warning:
#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the
C++ standard. Examples include substituting the<X> header for the<X.h>
header for C++ includes, or<iostream> instead of the deprecated header
<iostream.h>. To disable this warning use -Wno-deprecated.|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP|5|error:
`main' must return `int'|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP||In function
`int main(...)':|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP|6|error:
`clrscr' was not declared in this scope|
||=== Build finished: 2 errors, 1 warnings ===|

Regardless what I change it won't run properly, not prompting for
"Enter the velocity, acceleration, time as integers :"
Read the compiler's output and understand what the errors are...
Any suggestions appreciated.

The two errors are:
- `main' must return `int'
- `clrscr' was not declared in this scope

The first is easy to resolve. Make your main return an int :) I.e.
change that void to int and put a return statement at the end of the
function. Unless you plan to run your program from a script, it should
not really matter what value you return. Normally you should return just
0 or, if you want to be correct, return EXIT_SUCCESS, which is defined
in <cstdlib>.

The second error means that the compiler does not know about any
function named clrscr(). Console I/O is not standardized, so <conio.h>
is non-standard and does not necessarily define a function named
clrscr(). Yours does not seem to do so, so you're out of luck. Read your
compiler's documentation about what functions conio.h contains.

There's another warning that you should take heed of. It even tells you
what you've done wrong and what you should do instead.

General advice: *read* and *understand* what the compiler tries to tell
you. I know, sometimes it's hard to dissect that blob of gibberish, but
most of the time it's worth it, because the solution is in there.


Peter
 
R

red floyd

I have a couple errors that display when building any sample C++ code I
test.
(using Codeblocks)

Sample app:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int v,u,a,t;
cout << "Enter the velocity, acceleration, time as integers : " << endl;
cin>>u>>a>>t;
v=u+a*t;
cout << "The final velocity is " << v << "." << endl;
getch();

}

Result:
Process returned 0 (0x0)   execution time : 0.000 s
Press any key to continue.

Build Messages/Errors:
C:\Program
Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\backward\backward_warning.h|32|warning:
#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the
C++ standard. Examples include substituting the <X> header for the <X.h>
header for C++ includes, or <iostream> instead of the deprecated header
<iostream.h>. To disable this warning use -Wno-deprecated.|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP|5|error:
`main' must return `int'|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP||In function
`int main(...)':|
C:\Documents and Settings\Sid\Desktop\Celsius to Farenheit.CPP|6|error:
`clrscr' was not declared in this scope|
||=== Build finished: 2 errors, 1 warnings ===|

Regardless what I change it won't run properly, not prompting for
"Enter the velocity, acceleration, time as integers : "

Any suggestions appreciated.

The deprecation message is for <iostream.h>. The Standard defines no
such
header, use <iostream> instead. The <conio.h> and getch() is
unnecessary.
 
O

osmium

red said:
The deprecation message is for <iostream.h>. The Standard defines no
such
header, use <iostream> instead. The <conio.h> and getch() is
unnecessary.

Also, a modern compiler uses "namespaces" and cin and cout will have to be
qualified by some means. One way is to add "using namespace std;" as the
first statement after "main".

getch() is a workaround for a problem with some compilers.
cin.get() is a decent replacement for getch(), you may need two of them..
int for type in a problem such as this is probably not a good idea; double
is better.
 
R

red floyd

getch() is a workaround for a problem with some compilers.

Not quite. getch() is a workaround for a problem with some *IDEs*.
The compiler doesn't do anything wrong. If you fire up a command
prompt,
and run the program, you see how superfluous the getch() is.

Compiler != IDE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top