Thread: Declaring variables below the script rather than at the beginning?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    23

    Declaring variables below the script rather than at the beginning?

    Cheers to my first ever post on the first ever programming forum I've ever been in!

    Anyways, for the context, I'm writing a quiz game in c++ along with my friend. It's text based and pretty simple.

    Do note that I just recently started learning about c++ so my technical knowledge is nil and I might not understand more advanced stuff but I get the gist.

    So I have say, 50 questions in the script right now. That means that there will be 50 question holding variables, 50 x 5 more variables for the options and 50 variables holding the answer.

    Variables are important because I want the order of questions to be completely random. I will post the code after it's done.



    So is it possible to declare the variables below all of the script? I know that you can put functions below the main function so I was thinking maybe there was a way to declare global variables via functions?

    That would be helpful because otherwise you would have to scroll over some 350 lines of variables just to get to the main function!


    So assuming that's not possible, can somebody explain to me how I can use modules in c++ for this purpose? I have never done anything related to importing in c++ so if it comes to this, explain it like you would to a 5 year old.

    Thanks for reading.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    716
    Hello. You really shouldn't use global variables.

    I suggest you learn a bit about structs. Use a struct to store a question, along with the 5 choices and the number of the correct choice.
    Code:
    struct Question
    {
        // question (string)
        // the 5 choices (array of strings)
        // the correct answer (integer, index of choice + 1)
    };
    For multiple repeating objects (the 50 quiz questions), some kind of array is a good choice.

    Once you learn about reading files, I would suggest you move to reading your questions/answers into your program at runtime from a text file.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    23
    Quote Originally Posted by -Adrian View Post
    Hello. You really shouldn't use global variables.

    I suggest you learn a bit about structs. Use a struct to store a question, along with the 5 choices and the number of the correct choice.
    Code:
    struct Question
    {
        // question (string)
        // the 5 choices (array of strings)
        // the correct answer (integer, index of choice + 1)
    };
    For multiple repeating objects (the 50 quiz questions), some kind of array is a good choice.

    Once you learn about reading files, I would suggest you move to reading your questions/answers into your program at runtime from a text file.
    Hey Adrian, thanks for the tips.

    I will definitely use structures for future codes, but I take it that even structures need to be put before the main function.

    I am using a char* array for each Questions, Answers, OptionA, OptionB.. etc.

    But you would still have to declare the members of the char* array separately right?


    I want to keep adding an external file as a last resort just to keep down the hassle. But if it's not possible then I guess I have no choice.

    Also are you saying that reading from a text file is more convenient than including or importing information?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,207
    Also are you saying that reading from a text file is more convenient than including or importing information?
    Yes, with the benefit of being able to change the questions and answers without the need to re-compile the program.

    I am using a char* array for each Questions, Answers, OptionA, OptionB.. etc.
    I suggest you consider using std::string instead of char* and std::vector instead of raw arrays.

    You should also avoid global variables except in certain rare circumstances.

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    23
    So it's possible to declare variables at the end of the script rather than at the beginning? And that would be by declaring a variable as global in a function I assume?

    So just out of curiosity, if I were to do that (declare variable at the bottom of the script), how would I do that?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,207
    So it's possible to declare variables at the end of the script rather than at the beginning?
    Variables and functions must be declared before they can be used. So no it is not possible to declare your variables at the end of the file and try to use those variables in that file.

    And that would be by declaring a variable as global in a function I assume?
    What? There are differences between functions and variables but using global variables is considered a very bad practice.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,592
    No, declaring variables at the bottom of the script is not useful and is confusing at best.

    There are a couple of things going on here. First, C++ is executed in roughly top down order, so you will need to declare things before you use them, not after. I realize that in functional languages, declarations might not matter where you place them, but C++ is not entirely functional, and more imperative in this respect. Second, you might be asking where to place things like structures or class declarations, and not just variables (which might be structure or class type). The truth is, these are normally placed in their own files, which is something that you can learn later. Maybe from this.

    If you are just talking about variables, then in C++, it is considered good practice to declare variables near their first use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with declaring variables
    By dgigga in forum C Programming
    Replies: 4
    Last Post: 03-08-2014, 09:12 PM
  2. Declaring variables in int main()?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 02:21 AM
  3. Declaring Global Variables
    By renanmzmendes in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2008, 01:50 PM
  4. Declaring Variables
    By Godders_2k in forum C Programming
    Replies: 10
    Last Post: 11-18-2007, 04:50 AM
  5. Declaring variables
    By Marlon in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2005, 04:16 AM

Tags for this Thread