Archived

This topic is now archived and is closed to further replies.

cppnoob

homework question

Recommended Posts

My assignment is to create a program that asks the user for a list of book titles (using arrays of 10 char) and display the list back to the user. I must first ask how many books to input. Here's an example: How many books? 20 Book Title? MyBook Book Title? YourBook Book Title? Some Other Guy's Book ....... you get the idea.... The output will be Titles ============================== MyBook YourBook Some Othe ................. The problem is I am not allowed to use anything but loops and maybe functions. I can't think of a way to resolve this without using dynamic memory or file I/O. A hint will be greatly appreciated! [edited by - cppnoob on March 2, 2003 10:43:22 PM]

Share this post


Link to post
Share on other sites
First, let me state that this is not the place to ask homework questions (see FAQs for details).

If C++, can you use standard containers ?
If not, can you alter the list order (to use recursion)
If not, use a fixed-size buffer and reject requests for too many books.


[ 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 ]

Share this post


Link to post
Share on other sites
What about a massive array of some number you could never reach? Maybe char bookTitles[999][11]. Not the best way to do it, but for a simple project it would work

Peon

Share this post


Link to post
Share on other sites
If you must give the user an absolute option of how many books you could write the list as it goes on to a file and then read it once you''re done getting them all. Seems like a lame way but thats the only way I can think of without making a big 2D array. Although I think I would do the array.

Share this post


Link to post
Share on other sites
Hint: If you actually use this, your teacher will most likely fail you.


      
#include <iostream>

void a(int c=-1){if(c<0){std::cout<<"How many books? ";int b;std::cin>>b;a(--b);
return;}char s[11];std::cout.write("Book Title: ",12);std::cin>>s;if(c)a(c-1);else
std::cout<<"Titles\n=============\n";std::cout<<s<<std::endl;}int main(){a();}


I didn't even need a goto to make it hard to follow.

[edited by - smart_idiot on March 2, 2003 11:28:41 PM]

Share this post


Link to post
Share on other sites
i like the way you did that, interesting, you''re right though, i dont think even a bright up and coming computer science student, would do it that way.

Share this post


Link to post
Share on other sites
smart_idiot, thanks for the hint (and the cryptic code), but recursion isn't allowed for this assignment.

GravtyKlz, as I mentioned above, file I/O is not my option either.

I guess the only way to do this is to limit the user's input by declaring a huge 2D array.

Fruny, I am sorry. It won't happen again.


This is a C++ assignment, btw

[edited by - cppnoob on March 2, 2003 12:25:32 AM]

Share this post


Link to post
Share on other sites
just try this:

#include <iostream.h>

void main()
{
int noBooks; // number of books
int loop = 0; // this will loop until all books have titles
int x = 0; // this will be used to move through the array with the book titles in it

cout << "How many books? ";
cin >> noBooks;

char BookTitle[noBooks]; // creates array with number of Books - noBooks

while(loop < noBooks)
{
cout << "\nBook Title? ";
cin >> BookTitle[x];
x++;
loop++;
}

loop = 0 // so that we can loop through the whole array
x = 0 // so that we can print out every book in the array

cout << "Titles\n";

while(loop < noBooks)
{
cout << BookTitle[x];
x++;
loop++;
}
}

[edited by - pimple on March 2, 2003 12:56:17 AM]

Share this post


Link to post
Share on other sites
quote:
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 ]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
[quote]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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites