Undeclared Identifier: Three Questions

//**********************************************************************
LOVELY ERROR MESSAGE:
.\Nike.cpp(123) : error C2065: 'book' : undeclared identifier
.\Nike.cpp(123) : error C2065: 'title' : undeclared identifier
.\Nike.cpp(124) : error C2065: 'book' : undeclared identifier //**********************************************************************
// Disclaimer, I have just started C++, Yes I am a NOOB, but I really want
// to learn this programming stuff.
// Question 1:
// I was working on this project inside a C++ book to help me learn some
// C++, the objectives are listed below, according to the book.
// However, when I tried to get the lookUpBook function to
// accept incoming parameters I am getting this: error C2065.
// Question 2:
// Search the bookTitle array with a linear search to see if the book parameter exists.
// Okay, I am stumped, what does that mean, and how do you do that?
// Question 3:
//If it does exist (the Book), return the subscript ( or position in the array ) that was found.
Else, If it doesn't exist (the Book), return a -1 ( meaning it wasn't found ).
// Okay, how would I even set the format of this or go about attacking this program, the book gives examples but they are not very helpful to say the least,
either that or I have to decipher somehow, what they are trying to get me to do.

void lookUpBook(string bookname[]); // Function Protoype Looks up a book
// within the Inventory Database.


void invmenu()
{
int choice = 0;
while(choice!=5)
{
choice++;
cout <<"\t\t\t Serendipity Booksellers"<< endl;
cout <<"\t\t\t Inventory Database\n"<< endl;
cout <<"\t\t\t 1. Look Up a Book\n "<< endl;
cout <<"\t\t\t 2. Add a Book\n "<< endl;
cout <<"\t\t\t 3. Edit a Book's Record\n "<< endl;
cout <<"\t\t\t 4. Delete a Book\n "<< endl;
cout <<"\t\t\t 5. Return to the Main Menu\n\n"<< endl;
cout <<"\t\t\t Enter Your Choice: ";
cin >> choice;
}
switch (choice)
{
case 1: cout << " Enter book title: ";
getline(cin, title);
book = lookUpBook(title);
bookinfo(book);
break;
case 2: addBook();
break;
case 3: addBook();
break;
case 4: deleteBook();
break;
case 5: cout << "\t\t\t\tYou entered 5.\n";
break;
cout << "\t\t\t\t You did not enter 1., 2.,3.,4 or 5.!\n";
}
}
void lookUpBook(string bookname[]) // Stub Function
{
cout <<"\t\t\tYou selected Look Up Book.";
}
Last edited on
'undeclared identifier' means that you are using a name that you haven't told the compiler about. Specifically, it means you are trying to use variables ('book' and 'title') that don't exist. You must declare them before you use them.

You need to be very careful about the type of things. For example, string bookname[] declares an unbounded array of strings. If you just want one string (since books have just one name), just say string bookname.

Also be careful mixing >> and getline(). What you currently have will fail (in a confusing way). When interacting with the user, you can generally expect him to press ENTER after every input. (It is what the user expects he has to do.) So just getline() everything and convert to the proper type, or once you get the type with >> make sure to get rid of everything left over, up-to and including the newline (ENTER key). I'll provide an example of both below. (Choose one.)

Finally, be careful to indent your code so that you can see what is attached to what. For example, everything that belongs to a while loop should be indented under the keyword "while".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;

...

unsigned lookUpBook( string bookname );
// Finds a book that matches the given name.
// Returns the index into the inventory database of the book found.
// (For this example, I made it an integer. It could be anything convenient.)
//

void invMenu()
  // Interacts with the user to manage the book inventory database.
  {
  int choice = 0;
  while (choice != 5)
    {
    cout << "...";

    // Method one
    string s;
    getline( cin, s );
    if (!(stringstream( s ) >> choice))
      {
      cout << "\t\t\t Invalid choice. Please try again: ";
      continue;
      }

    // Method two
    if (!(cin >> choice))
      {
      cin.clear();
      cout << "\t\t\t Invalid choice. Please try again: ";
      }
    // Get rid of everything remaining on the line, including the ENTER/newline:
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
    if (choice == 0) continue;

    switch (choice)
      {
      case 1:  // look up a book by title
        {
        string title;
        int    book;
        cout << "Enter the book title: ";
        getline( cin, title );
        book = lookUpBook( title );
        bookinfo( book );
        break;
        }

      case 2:  // add a book
        {    // These braces aren't strictly necessary, but I put them in when I
        ...  // want to use local variables, like 'title' and 'book', which only exist
        }    // for a specific switch-case.

      ...
      } // end switch (choice)
    } // end while (choice != 5)
  } // end invMenu() 

Hope this helps.

PS. Please use [code] tags.
Er, sorry, here's for questions 2 and 3:

1. see last post

2. linear means in a straight line. In this case, the search algorithm should just start at the beginning of your array of books and work its way towards the end. If it gets to the end and the book isn't found, return -1. Otherwise return the index into the array of the book.

3. Make your lookUpBook() function return an int.

int lookUpBook( string bookname )

Sorry.
Thank you, for the help, I really appreciate this.

Now, I was wondering are there any good books to learn C++ that
you guys in this forum would recommend
for a noob?

The one I have sucks, and that's why I keep asking all these questions.
Everything I am reading in the current C++ textbook that I have is full of questions I would like to ask the author.

I will probably go to Amazon and find out what the reviews are like in terms of
C++ and their textbooks.
Oh, any pointers and ad dons are welcome also.
Grey Wolf recommends Code Complete
http://www.cplusplus.com/forum/lounge/1953/page2.html

It is a very good book.
Also, the tutorials on this site are very good.

Asking questions is good. :-)
Sweet, just got my Code Complete 2 edition, pdf.
Now for some fun, I actually got a bunch of other C++ PDF's too.
Thanks Grey Wolf.
You could also look around on http://www.freecomputerbooks.com
WoW, ummm, I will see you guys in like X months after reading and studying.
There is a lot of information and ground to cover, I just hope I don't go blind with all this lovely C++ text.

And mikeb570, you rock, man-that's oodles of books to keep the brain obese,
thanks.

Hopefully, after reading all this I will be able to go to sleep and dream about
C++, and later on GCC.
Topic archived. No new replies allowed.