So my probelm is as the title says. I need to fill up the stocklist array with data in putted from the keyboard. My code runs, but crashes after I enter data for the first member of the stocklist array…
Attached screen shots and code.
Any help is appreciated, thanks!
Code:
#include “stdio.h” #include “string.h”
const int IDLEN=10;
const int POLARITYLEN=3;
const int MAXSTOCKITEMS=10;
There are a couple of other things as well... For instance, the stocklist array will be length 10, so it should start at index 0 (since that's where arrays start) and end at 9, so i < 10 or i <= 9. On top of that, you should just initialize the loop variable in the initialization of the for loop it gives you, like I did, instead of outside, since you're not using it outside. It makes it a little more concise. Same for the second function / for loop.
Yep that did it! Thanks heaps for that, I've never used the iostream header before but after a bit of fiddling I got it working :) The mystery about why scanf wasn't working is for another day!
You do not need to do this this for your char array arguments (Transistor::manufacturersID and Transistor::polarity) because in C/C++ the name of an array is equivalent to the address of it's first element:
Now we have that sorted, I was wondering - is this part of a class, and if so, are you being taught C or C++? They are actually different languages (although closely related) and in my opinion the code you have written is C, not C++ (as it uses no C++ features).
I ask because I would happily give you some tips on how to write this as much better formed C++ code, but I don't want to tell you loads of stuff you can't possibly know (as far as your lecturer is concerned) :).
Ah yes I'm still getting my head around pointers but I've made a fair bit of progress in the last couple of days so what you said makes a lot of sense, thanks for that :) Yes this is for a class, the class is on mainly C but the lecturer will talk talk about C++ quite a bit. So you are probably right that this code is more C than C++. Thanks for the offer, but I'm sure as the semester progresses I will become more proficient in C++ :)