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.
Need some help with my first Program
Moderator: Committer
-
- Creative Contributor
- Posts: 1060
- Joined: Sun Jun 29, 2003 12:40 am
- Location: Tucson, Arizona USA
Need some help with my first Program
Fear is the Mind Killer - Frank Herbert -Dune
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.
'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.
-
- Creative Contributor
- Posts: 1060
- Joined: Sun Jun 29, 2003 12:40 am
- Location: Tucson, Arizona USA
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 )
Seems theirs always something new to learn.
Seems theirs always something new to learn.
Fear is the Mind Killer - Frank Herbert -Dune
- Geoff the Medio
- Programming, Design, Admin
- Posts: 13587
- Joined: Wed Oct 08, 2003 1:33 am
- Location: Munich
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.
-
- Creative Contributor
- Posts: 1060
- Joined: Sun Jun 29, 2003 12:40 am
- Location: Tucson, Arizona USA
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
Thanks for the Tutorial Geoff, I need all the tutorials I can get
Fear is the Mind Killer - Frank Herbert -Dune
- Geoff the Medio
- Programming, Design, Admin
- Posts: 13587
- Joined: Wed Oct 08, 2003 1:33 am
- Location: Munich
Let's see what's in the bookmarks...Impaler wrote:Thanks for the Tutorial Geoff, I need all the tutorials I can get
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.
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.
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.