Jump to content
  • Advertisement

Archived

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

cppnoob

homework question

This topic is 5619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
pimple, isn''t this dynamic memory?
char BookTitle[noBooks]; // creates array with number of Books - noBooks

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!