Need some help with my first Program

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
Impaler
Creative Contributor
Posts: 1060
Joined: Sun Jun 29, 2003 12:40 am
Location: Tucson, Arizona USA

Need some help with my first Program

#1 Post by Impaler »

I have been learning some Basic C++ and making a simple Program for calculating Market Prices (its a back and forther process as I learn something new I try to incorporate it in the program and when I get errors in the Program I go about learning why).

Well its nearly done but I have hit upon a big snag, I am trying to Declare Arrays with another Variable as the length. I have the Variable declared globaly right at the start of the program and initialized at zero. In Main() I then run a Function that prompts the user to enter new values for the variables, after that function is done running I try to create the Arrays using the form...

int Array_Name[int Variable_Name] = {0};

My Compilers (rhide in Djgpp) says I have a Parse befor before ]

I am still rather new to all this and this is probably a very lame question, I hope its not viewed as inapropriate to be asking this here I dont want to use up anyones valuable time teaching me things that are considered rather basic. If I can get this program working I will Post the whole source code here for critiquing.
Fear is the Mind Killer - Frank Herbert -Dune

Tyreth
FreeOrion Lead Emeritus
Posts: 885
Joined: Thu Jun 26, 2003 6:23 am
Location: Australia

#2 Post by Tyreth »

You cannot create arrays with variable sizes. You need to use the 'new' and 'delete' operators in C++ to create dynamically sized arrays. Without that you need to tell the program the exact size - simply declaring the size globally at the beginning is insufficient, because the compiler doesn't know if the variable will change size before the array is created.

'new' and 'delete' are another thing altogether, so probably best that you read about how they work before you start using them. It's important to use them properly, cleaning up after using them, otherwise you can end up with memory leaks.

Impaler
Creative Contributor
Posts: 1060
Joined: Sun Jun 29, 2003 12:40 am
Location: Tucson, Arizona USA

#3 Post by Impaler »

Thanks Tyreth, I will read up on the proper use of "new" and "delete". I had also been getting parse errors with the deletes I was trying to use wipe the whole slate (shake the Etcheschetc if you like) and get ready for a new set of calculations perhaps I need to have "new" before I can delete, I understand that anything you make you should delete or else all these left over variables would sit their and use up memmory hense memory leaks (EVIL VERY EVIL :twisted: )

Seems theirs always something new to learn.
Fear is the Mind Killer - Frank Herbert -Dune

Yoghurt
Programmer
Posts: 376
Joined: Sat Jun 28, 2003 8:17 pm
Location: Heidelberg, Germany

#4 Post by Yoghurt »

Use the STL's vector and map. Much more flexible than arrays and you don't have to keep track of memory.

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

#5 Post by Geoff the Medio »

Check out http://www.cplusplus.com/doc/tutorial/ if you haven't already, particularily the dynamic memory section. But if you just want a variable-sized array, consider Yoghurt's suggestion of using one of the STL's containers... map, vector, set, list, etc. Each is useful in different situations.

leiavoia
Space Kraken
Posts: 167
Joined: Sun Jul 20, 2003 6:22 pm

#6 Post by leiavoia »

In fact, learn about STL immediately and your life will be simpler. I wish i knew about that when i started. Arrays bite :-(

Impaler
Creative Contributor
Posts: 1060
Joined: Sun Jun 29, 2003 12:40 am
Location: Tucson, Arizona USA

#7 Post by Impaler »

I think I will stick with plain old (diting) Arrays for this program so I can atleast learn about them, as I learn better memory structures I might never use them again but for now its one thing at a time. I'm now using the new and delete with a pointer to create the array and it seems to be working ok. Just a few more parse errors to clean up assosiated with a switch statement (I am always forgeting the ; ) and it should compile (atwhich point I can start adressing the Run-time Errors).

Thanks for the Tutorial Geoff, I need all the tutorials I can get :)
Fear is the Mind Killer - Frank Herbert -Dune

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

#8 Post by Geoff the Medio »

Impaler wrote:Thanks for the Tutorial Geoff, I need all the tutorials I can get :)
Let's see what's in the bookmarks...

http://www.brpreiss.com/books/opus4/
http://www.mochima.com/tutorials/STL.html
http://www.amman.edu/cpp/CppBasics/CppBasics.html
http://www.doc.ic.ac.uk/lab/cplus/c++.rules/ (old)
http://people.cs.vt.edu/~kafura/cs2704/Notes.html

... hmm... thought I had more, but it turns out they're mostly application focused. I you want some links to N-Body gravity simulation pages, I can probly hook you up. :?

Marijn
Space Squid
Posts: 65
Joined: Thu Feb 19, 2004 10:26 am
Location: Nijmegen (NL)

#9 Post by Marijn »

http://www.mindview.net/Books/TICPP/Thi ... CPP2e.html - Online books giving a good introduction to the language. The first one is better than the second.

http://www.relisoft.com/book/ - Another online book. Focuses both on the language itself and on 'programming' in general. I found it a very good book.

http://www.parashift.com/c++-faq-lite/ - A rather big FAQ full of common c++ problems, will probably not be very useful until you learn more but it is full of important stuff.

Post Reply