I'm having a little trouble with some code. I'm writing a library management program. I need to be able to input title, author, isbn, etc. in the program. When the program asks for the isbn it behaves as expected. When I enter any words the program terminates, but if I only enter a single letter it does what I want it to do. To make this post shorter, how do I tell the program to allow a word instead of just a single character? Thanks for the help.

Recommended Answers

All 12 Replies

I'm having a little trouble with some code. I'm writing a library management program. I need to be able to input title, author, isbn, etc. in the program. When the program asks for the isbn it behaves as expected. When I enter any words the program terminates, but if I only enter a single letter it does what I want it to do. To make this post shorter, how do I tell the program to allow a word instead of just a single character? Thanks for the help.

Without seeing the code, my guess is that the variable that the user input is going into is a different type than the data being entered. For example, if you do something like this:

int isbn;
cin >> isbn;

and enter the word "cow", you are going to have problems because the program expects an integer, not a string. Change it to this:

string isbn;
cin >> isbn;

and enter the word "cow" and the program can handle it. The "string" type allows more than one character.

You can also use Character Arrays

char title[30];
cin.getline(title,30);

This will even take in spaces between words too.

You can also use Character Arrays

char title[30];
cin.getline(title,30);

This will even take in spaces between words too.

Too 'C' for me. How about combining the two posts and do it like:

string title;
getline(cin, title);

Now we can still use std::strings AND it takes in spaces, without having to use char[] or fixed sizes.

Thanks for the advice. I'm using Borland Turbo C++ and it's not recognizing the "string" data type. How do I get it to see this? Here is a sample of the code:
//Give the Author
string author;
cout << "Who is the Author: ";
cin >> author;

Use String with a capital 'S' or AnsiString.

I don't use Borland Turbo C++ (and neither should you), but try this:

#include <string>
#include <iostream>

int main()
{
    std::string s = "works!";
    std::cout <<  s << "\n";
    return 0;
}

I strongly suggest that you upgrade to a new compiler. That way you can program standard c++ just like the rest of us.
For a list of free compilers click here

when using char arrays isn't it recommend to use 256 blocks of memory or bytes?

char blah[256]={0};

i read something about adding ={0}; can help to prevent memory leaking but i dont quite understand how, feel free to correct me if what i have said is wrong as it more than likely is (been learning c++ for around 2~3 days now)

when using char arrays isn't it recommend to use 256 blocks of memory or bytes?

char blah[256]={0};

Never heard of that. Looks to me as a waste of memory.
If you said: 4 bytes then I would somewhat agree (on a 32 bits OS that is).

i read something about adding ={0}; can help to prevent memory leaking but i dont quite understand how,

You're giving a initial value to every array element, so int blah[256] = {0); will result in:

[0][1][2][3][4][5][etc]
 0  0  0  0  0  0  00000

That is important because what would this program do?

char a[2];
a[0]+=a[1];

With that all said: don't hijack someone else's thread. If you have any question, feel free to start your own thread

I don't use Borland Turbo C++ (and neither should you.....

I strongly suggest that you upgrade to a new compiler. That way you can program standard c++ just like the rest of us....

I was under the impression that Borland Turbo C++ is supplied with the standard C++ library. If this is the case, surely it can be used to program "standard" C++ as well as any other compiler?

Turbo is not compliant with the C99-standard but the other compilers I mentioned are.
C99 is been the standard since 1999 (duh), so: No, turbo can't be used to program standard c++

I don't really like Turbo C++ either. But, alas, I am taking a programming fundamentals course in the fall and we have to use it. I figured that I might as well get used to it.

I don't really like Turbo C++ either. But, alas, I am taking a programming fundamentals course in the fall and we have to use it.

I don't get these school where they teach C++ with an outdated compiler.
How did they teach you to add two numbers? With an abacus?
Perhaps you could ask your teacher why they insist on using borland turbo.

I figured that I might as well get used to it.

Please don't. If you have to use, then use it, but always keep in mind that the thing you're learning isn't real c++, but just an outdated version of it with some borland-specific functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.