homework question

Started by
12 comments, last by cppnoob 21 years, 1 month ago
If its a beginning C++ class, your teacher would probably accept you making a sufficiently huge array of book titles (1000?) and testing for that limit upon the user's input. I.e.

How Many Books? 12345
ERROR: Maximum number of books is 1000. Try again.
How Many Books? 1001
ERROR: Maximum number of books is 1000. Try again.
How Many Books? 999
Book Title?

If not that, is a vector allowed? Times like this really make you appreciate the STL. A lot.

std::vector < std::string > titles;

With this you can put in as many characters per title and as many titles as you want, given enough memory on your computer

500
edit: HTML versus templates: Round 1001

[edited by - Rick Scott on March 3, 2003 10:21:58 AM]
Advertisement
Really, the best way to do this is by using a pointer. Figure out how many books you need from the user and set it to a pointer. Then you can create an array of that many books and loop. I don''t know if your professor will let you do that though. But if you are allowed to use array''s logic would constitute that you should be allowed to use pointers as well...

koolboarder007
quote:Original post by Fruny
Original post by cppnoob
pimple, isn''t this dynamic memory?
char BookTitle[noBooks]; // creates array with number of Books - noBooks


Not even, it''s invalid C++ code. Array sizes must be determined at compile-time.

Some compilers let you do that (g++), but that''s a language extension.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]



I think it IS ALLOWED. If you create an array with a functionparameter, then it''s illegal.

.lick
I just had another thought:


    char var_on_stack;char *array = &var_on_stack+256;int words;cin >> words;while(--words) cin >> array+word*10;    


Of course this is making alot of assumptions and isn't very safe, but you wouldn't need to use new and delete for dynamic memory allocation . Anyone know how much stack istream::operator >> uses?

Again, if you do that, you will probably fail.

[edited by - smart_idiot on March 3, 2003 11:08:20 AM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement