473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Adding strings to vectors

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
Apr 19 '12 #1
7 2331
weaknessforcats
9,208 Expert Mod 8TB
This code:
c
Expand|Select|Wrap|Line Numbers
  1. har yesorno;
  2.  string bookname;
  3.  vector<string> books;
  4.  books.push_back("The Jungle, Upton Sinclair");
  5.  .... 
  6. books.push_back("A Wrinkle in Time, Madaline L'Engle");
  7.  while (yesorno = 'y'){
  8.  cout << "Enter another book? (y/n)" << endl;
  9.  cin >> yesorno;
  10.  {
  11.  
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.
Apr 19 '12 #2
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
Apr 19 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. cout << "You have just entered " << books.push_back(bookname) << endl;
  2.  
is not going to work because vector::push_back returns void. That means the ostream::operator<< can't be compiled.

You might try:
Expand|Select|Wrap|Line Numbers
  1. cout << "You have just entered " << bookname << endl;
  2.  
Apr 19 '12 #4
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.
Apr 19 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
D it in two steps: a)add to the vector, then b) display what you added:

Expand|Select|Wrap|Line Numbers
  1. books.push_back(bookname);
  2. cout << bookname;
Apr 19 '12 #6
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.
Apr 20 '12 #7
weaknessforcats
9,208 Expert Mod 8TB
A simple menu system can operate from a switch statement:
Expand|Select|Wrap|Line Numbers
  1. void Process(int choice)
  2. {
  3. switch(choice)
  4. {
  5.   case 1:  //Add
  6.            AddFunction();
  7.            break;
  8.   case 2:  //Remove
  9.            RemoveFunction();
  10.            break;
  11.   case 3:  //Exit
  12.            ExitFunction();   //does not return
  13.   default:
  14.            //bad choice
  15. }   //end of switch
  16. }  //end of Process
  17.  
Then you put this switch inside an infinite loop:

Expand|Select|Wrap|Line Numbers
  1. while(1)
  2. {
  3.    choice = GetChoice();
  4.    Process(choice);   
  5.  
  6.  
  7. }
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.
Apr 20 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
3
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"
30
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...
2
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...
17
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...
12
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...
52
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...
4
japuentem
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.
5
by: C++ Newbie | last post by:
Hi, Consider the following program from "Accelerated C++", Koenig & Moo : Case : ===================================== #include <iostream> #include <string>
2
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...
0
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,...
0
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$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.