Simple C++ review, errors

Discussion in 'C++' started by James, Apr 18, 2011.

  1. James

    James Guest

    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.
     
    James, Apr 18, 2011
    #1
    1. Advertising

  2. 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
     
    Peter Remmers, Apr 18, 2011
    #2
    1. Advertising

  3. James

    red floyd Guest

    On Apr 18, 7:50 am, "James" <> wrote:
    > 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.
     
    red floyd, Apr 18, 2011
    #3
  4. James

    osmium Guest

    red floyd wrote:

    > On Apr 18, 7:50 am, "James" <> wrote:
    >> 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.


    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.
     
    osmium, Apr 18, 2011
    #4
  5. James

    red floyd Guest

    On Apr 18, 9:39 am, "osmium" <> wrote:

    > 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.
     
    red floyd, Apr 19, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,027
    Mark Goldin
    Jan 17, 2004
  2. www
    Replies:
    51
    Views:
    1,515
  3. Paul Rubin
    Replies:
    3
    Views:
    317
  4. luser- -droog
    Replies:
    3
    Views:
    168
    luser- -droog
    Jul 31, 2013
  5. Udyant Wig
    Replies:
    88
    Views:
    385
    Malcolm McLean
    Apr 21, 2014
Loading...

Share This Page