Adding strings to vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alicia89
    New Member
    • Apr 2012
    • 5

    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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    c
    Code:
    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.

    Comment

    • Alicia89
      New Member
      • Apr 2012
      • 5

      #3
      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_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(long double) [with _CharT = char, _Traits = std::char_trait s<char>]
      /usr/include/c++/4.2.1/ostream:229: note: std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(const void*) [with _CharT = char, _Traits = std::char_trait s<char>]
      /usr/include/c++/4.2.1/bits/ostream.tcc:120 : note: std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(std::basi c_streambuf<_Ch arT, _Traits>*) [with _CharT = char, _Traits = std::char_trait s<char>]
      make[2]: *** [build/Debug/GNU-MacOSX/A2.o] Error 1
      make[1]: *** [.build-conf] Error 2
      Thanks so much!
      -Alicia

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code:

        Code:
        cout << "You have just entered " << books.push_back(bookname) << endl;
        is not going to work because vector::push_ba ck returns void. That means the ostream::operat or<< can't be compiled.

        You might try:
        Code:
        cout << "You have just entered " << bookname << endl;

        Comment

        • Alicia89
          New Member
          • Apr 2012
          • 5

          #5
          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.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            D it in two steps: a)add to the vector, then b) display what you added:

            Code:
            books.push_back(bookname);
            cout << bookname;

            Comment

            • Alicia89
              New Member
              • Apr 2012
              • 5

              #7
              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.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                A simple menu system can operate from a switch statement:
                Code:
                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:

                Code:
                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.

                Comment

                Working...