Position of code

This is a discussion on Position of code within the C++ Programming forums, part of the General Programming Boards category; In the following snippett why do the 4 lines starting 'HANDLE hOut;' have to go inside 'int main()'. I would ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    96

    Position of code

    In the following snippett why do the 4 lines starting 'HANDLE hOut;' have to go inside 'int main()'. I would have thought they should run before it as well but they don't, it gives an error saying 'hOut does not name a type'?


    Code:
    #include <iostream>
    #include <windows.h>
    #include "ShortColours.h"
    
    using namespace std;
    
    int main()
    {
        HANDLE hOut;
        hOut=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute (hOut, BWI | FBI);
        system ("CLS");

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    You can only call functions from within another function.


    Jim

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Thanks Jim, so that is a function really provided by windows.h.

    Pretty obvious I suppose when it is pointed out!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    The following are all function calls .
    Code:
    GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute (hOut, BWI | FBI);
    system ("CLS");
    Only the first two are prototyped by some include file that is included by windows.h. The final one is prototyped in stdlib.h./

    Jim

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    <cstdlib>, actually.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    C++ : <cstdlib>
    C : <stdlib.h>

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    So is there no way I can make the function universally available in all my functions without including all 4 lines at the start of each function?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    You could include this 4 files in .h file and then include only one file .h
    But this 4 lines seem to be no problem at all to me

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    You need to pass the HANDLE to the functions that need it as a parameter.

    Jim

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    If I put those 4 lines in a funtion called colour and pass front and back colours to it I could then use it everywhere with just one line; would system (CLS) work by itself in a function?

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    We do not include libraries into a function

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    First why are you using system() at all. You already seem to be aware of the Windows API, so use it to properly clear the screen. See this link for more information. MSDN Clearing the Screen, use example two.

    To use system() in any function you must include the proper include file cstdlib/stdlib.h in the file that contains this call.


    Jim

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Hi Jim,

    it isn't just the system call to clear the screen but also I sometimes want to change the text colour which is why I use this, I have looked at the method you suggest and it is quite possible to put that in my program with no bother but I would still need the other one to uses SetTextAttribute wouldn't I?

    I have been using the sys call but haven't got the cstdlib in my include files, from what you say I should think it shouldn't be working should it?

    Sorry if this isn't going in as easily as it should but I am getting on a bit!!!

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    2,684
    The include file for system() may be included by windows.h, however it is considered a bad practice to rely on some other file including a standard include file for you. Always include the proper include file for the functions you are going to use.

    but I would still need the other one to uses SetTextAttribute wouldn't I?
    What other one? To use SetTextAttribute all you should need to include is windows.h.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. position of max
    By -chr- in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2009, 12:18 PM
  2. Available position
    By virspb in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2006, 02:28 AM
  3. Available position
    By taha54 in forum Projects and Job Recruitment
    Replies: 7
    Last Post: 03-17-2006, 03:05 PM
  4. Replies: 3
    Last Post: 11-03-2002, 01:14 AM
  5. program position
    By monkeymon in forum Windows Programming
    Replies: 6
    Last Post: 05-23-2002, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21