Like Tree2Likes
  • 1 Post By quzah
  • 1 Post By quzah

Regarding scope

This is a discussion on Regarding scope within the C Programming forums, part of the General Programming Boards category; Hi all, I dug up an old C project of mine that I want to use to apply new principles ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    7

    Regarding scope

    Hi all,

    I dug up an old C project of mine that I want to use to apply new principles to in C++. It's a simple text-based game that I can't seem to get working.

    Basically, I wrote all the original code in Dev-C++ and have found the Visual C++ 2010 Express Edition to be much better and more likable to use. The problem is, the code won't compile in it. I believe it's a concern with scope, as I'm getting warnings, errors and red lines under things like variable names, functions, etc.

    Some errors include:
    Undeclared identifier
    'close' undefined; assuming extern returning int.
    'name' unidentified, depite the fact 'name' is declared as global.

    "close" is also red lined and is an unknown function to the compiler despite the fact I have included the correct headers, which are:
    Code:
    
    #include<stdio.h>
    
    #include<ctype.h>
    
    #include<stdlib.h>
    
    #include<time.h>
    
    #include<windows.h>
    
    #include<string.h>
    
    I can't understand this. The Dev-C++ setup was thus: write an 'includes' header file and put all my #includes in there, then include this header file in main(). This seemed to allow every source file in the program to have access to anything I required it to have access to, such as <time.h>.

    This doesn't work in VC++. Even if I manually include all the header files and necessary includes within each source file, it still won't run my code and some attempts have even told me I've used too many header files.

    I was under the impression that C code is the same wherever you go, but apparently not?

    Can anyone help me out a little? I'm considering just forgetting this environment and going back to Dev-C++, but I really don't want to.

    Thanks for reading.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are in a maze of twisty forums, all alike.
    You see a bit of illumination to the east.
    >


    Quzah.
    anduril462 likes this.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    Thanks for the post, but I'm not after help with C++. As I mentioned, it's an old and forgotten C project I am looking to update with newly learned C++ code as I go, but in its current form it's all C code which I am having trouble compiling in the new development environment.

    Unless you were referring to the fact I am using VC++, but I doubt that as the compiler will accept all standard C code.

    Thanks.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by George88 View Post
    Thanks for the post, but I'm not after help with C++
    Oh? Then what does this mean:
    Quote Originally Posted by George88 View Post
    C project I am looking to update with newly learned C++ code
    You don't compile C with a C++ compiler, because they are two different languages. If you want C, use a C compiler. If you want C++, write it in C++, and use a C++ compiler.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    7
    I'm sorry for the confusion.

    There are a few reasons I'm using the C++ compiler.

    1. I'm going to be applying C++ code and principles to the project as & when I learn it.

    2. The original code was written in a C++ compiler (Dev-C++).

    3. Visual C++ contains a C compiler.

    So as I said, I don't want help with C++ code as I don't have any yet, it's the compiler errors and warnings I don't understand and just wanted a little advice.

    Thanks again.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right, so like I said, you want the C++ board since you are going to be writing C++.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    close is not a standard C function. If I recall correctly, it's an old IBM/DOS function that uses integers in place of file pointers. You shouldn't be using it in modern C, and certainly not in C++.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not restricted to DOS. Unix has close / open / read / write as well.


    Quzah.
    Subsonics likes this.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > I dug up an old C project of mine that I want to use to apply new principles to in C++.
    Yes, and when you're done, it will surely look like that as well.

    But if you want a GOOD C++ solution to the problem, you won't get there by mauling an old C program 1 line at a time.
    It will be like trying to rebuild a house one brick at a time. Whilst no doubt possible, it will take longer and the result won't be as good. Sooner or later, the only smart thing to do is demolish and start again.

    C++ done properly is a completely different mind-set to C. If you want to make use of all those lovely 'Object Oriented' techniques, and all the goodies in the STL.

    Before you do anything on the C++ side, you're going to need some idea of what classes will be involved.
    Class diagram - Wikipedia, the free encyclopedia

    Oh, and as for the close thing.
    _close (CRT)
    MS put a leading underscore for everything "POSIX".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope
    By beereo in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2010, 09:38 AM
  2. Class Scope
    By strokebow in forum C++ Programming
    Replies: 7
    Last Post: 12-17-2008, 04:45 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Scope
    By Extol in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 01:14 PM
  5. Scope
    By Witch_King in forum C Programming
    Replies: 6
    Last Post: 09-06-2001, 12:11 AM

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