Variables

This is a discussion on Variables within the C++ Programming forums, part of the General Programming Boards category; I compiled my program only to see that I had to recreate my variables... And when I do they don't ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Variables

    I compiled my program only to see that I had to recreate my variables... And when I do they don't have the information... They aren't to seperate functions so... I don't know what's wrong... Can someone help me? I'm using Borland C++.
    Attached Files Attached Files

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It could be caused by all the gotos you have. Try to structure your program with while/for loops instead. Gotos are not wrong, but ugly.

    And you should try to have a hiearchy system in your code, like this:
    Code:
    int main()
    {
      int A=3;
      if(A==3)
      {
        printf("Hello");
      }
      return 0;
    }
    instead of this:
    Code:
    int main()
    {
    int A=3;
    {
    printf("Hello");
    }
    return 0;
    }
    it makes it easier to read.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Update

    Ok, here's the updated code:
    Attached Files Attached Files

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Error: rpgbasse.cpp(193,12):Undefined symbol 'hp'
    Error: rpgbasse.cpp(193,16):Undefined symbol 'lvl'
    Error: rpgbasse.cpp(193,19):Undefined symbol 'at'
    Error: rpgbasse.cpp(193,22):Undefined symbol 'gp'
    Error: rpgbasse.cpp(193,26):Undefined symbol 'nme'

    You declare these variables in a sub-function, and therefore you cannot access them from main. The easiest solution would be to make them global. Declare them outside main, like this:
    Code:
    int hp, mp,....
    
    int Load()
    {
      ...
    }
    
    int main()
    {
      ...
    }
    ...and that should solve your problem!

  5. #5
    Unregistered
    Guest
    You should use a struct instead, and then have the function

    Code:
    int Load(const Struct_Name &t)
    {
    //...
    }
    so you can make one in main and then pass it to Load, avoiding global variables

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question ...

    I don't know much about classes... could you apply it, in some source code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Replies: 6
    Last Post: 01-02-2004, 12:01 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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