Hi,
I am a newbie at programming. I was given a project to program a library catalog. One of the aspects is that we have to allow an administrator to add, modify, and delete books from the catalog. It was recommended to me to use vectors. So I initialized by hand a default book list, and now I want to be able to have an adminisistrator add books and then print the modified book list. Here is what I have got:
main () {
char yesorno;
string bookname;
vector<string> books;
books.push_back("The Jungle, Upton Sinclair");
....
books.push_back("A Wrinkle in Time, Madaline L'Engle");
while (yesorno = 'y'){
cout << "Enter another book? (y/n)" << endl;
cin >> yesorno;
{
cout << "Enter the title, author."<< endl;
getline(cin, bookname);
cout << "You have just entered " << books.push_back(bookname) << endl;
I keep getting error messages. I am unsure what to do to fix this.
Any help would be greatly appreciated.
Sincerely,
Alicia
7 2331
This code:
c - har yesorno;
-
string bookname;
-
vector<string> books;
-
books.push_back("The Jungle, Upton Sinclair");
-
....
-
books.push_back("A Wrinkle in Time, Madaline L'Engle");
-
while (yesorno = 'y'){
-
cout << "Enter another book? (y/n)" << endl;
-
cin >> yesorno;
-
{
-
creates yesorno as a char. It is never initialized before the while statement. In the while stateent I see yesorno = 'y' which assigns 'y' to yes or no. The 'y' will always be true so the while loop runs forever.
Try to use your debugger and step through the code.
Sorry, I copied my code wrong. Suppose I have (yesorno == 'y').
How do I get it to run? When I debug, I get the same response as when I run the program.
I get alot of this message:/usr/include/c++/4.2.1/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:229: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:120: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
make[2]: *** [build/Debug/GNU-MacOSX/A2.o] Error 1
make[1]: *** [.build-conf] Error 2
Thanks so much!
-Alicia
This code: - cout << "You have just entered " << books.push_back(bookname) << endl;
-
is not going to work because vector::push_back returns void. That means the ostream::operator<< can't be compiled.
You might try: - cout << "You have just entered " << bookname << endl;
-
Yes that would print the book name, but it wouldn't add it to the vector. I want to add the book to my vector.
D it in two steps: a)add to the vector, then b) display what you added: - books.push_back(bookname);
-
cout << bookname;
Yes I know that, how do you get a user to enter a new book? Here is my latest code:
cout << "Enter book to add: "<< endl;
getline(cin, book);
books.push_back(book);
the error says that there is no function call to getline. ??? I have all the appropriate header files.
A simple menu system can operate from a switch statement: - void Process(int choice)
-
{
-
switch(choice)
-
{
-
case 1: //Add
-
AddFunction();
-
break;
-
case 2: //Remove
-
RemoveFunction();
-
break;
-
case 3: //Exit
-
ExitFunction(); //does not return
-
default:
-
//bad choice
-
} //end of switch
-
} //end of Process
-
Then you put this switch inside an infinite loop: - while(1)
-
{
-
choice = GetChoice();
-
Process(choice);
-
-
-
}
So you select the Add choice and that adds one book and returns. That makes Process() return which causes you to cycle to the top of the infinite loop and then yu can select another choice. Maybe Add again so you add a second book.
Your exit function should not return but just terminate your program by calling something like terminate();
Other menu choices might be to display all the books in the vector. Or maybe sort them by title. Each time you add a menu choice you add a new case to the switch inside Process().
There are many approaches to this problem. This one is good for beginners.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Shannan Casteel via AccessMonster.com |
last post by:
I have a table called tblParttest. It has fields PartNo, PartNo2, PartNo3, ..
.., PartNo25 and Quantity, Quantity2, Quantity3, ...Quantity25.
If I enter "1234" into the PartNo field and "4" into...
|
by: Thierry |
last post by:
Hi,
I've got a problem with adding strings with a quote in SQL.
Dim LocationName As String = "for' example"
|
by: zexpe |
last post by:
I have an extremely cpu/data intensive piece of code that makes heavy
use of the following function:
void convertToDouble(const std::string& in, double& out)
{
out = atof(in.c_str());
}
I...
|
by: Stan Horwitz |
last post by:
I am working on a perl program where I extract a variable from a line of
input and I want to keep a running count of that variable. I am using
the substr function to extract the variable. For...
|
by: tommy |
last post by:
Hi all,
I' m adding strings to some fields in my table via Access. The strings
sometimes have trailing spaces and I really need to have it that way,
but Access truncates trailing spaces. How can...
|
by: Kim Hellan |
last post by:
I come from C# development, but I have to make a .NET WinForm application in
C++.
I'm having some troubles handling strings in C++, which seems a lot more
problematic than in C#.
Lets say I want...
|
by: Paddy |
last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and
followed up on the use of sum to do the flattening.
A solution was:
I would not have thought of using sum in this way. When...
|
by: japuentem |
last post by:
Hi
I have a main Vector and i'm adding another vectors inside the main, the question is ¿how to get each value of the secondary vectors?.
I hope you understand the question.
|
by: C++ Newbie |
last post by:
Hi,
Consider the following program from "Accelerated C++", Koenig & Moo
:
Case :
=====================================
#include <iostream>
#include <string>
|
by: Laura Wilkinson |
last post by:
I've got many differences between vectors/strings and arrays but I need more details. I guess I don't really understand it enough to do this but this is what I've concluded so far;
An array is a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |