Thread: undeclared (first use this function)

  1. #1
    Brand New
    Join Date
    Mar 2005
    Location
    South Carolina
    Posts
    3

    undeclared (first use this function)

    Hello all. I just started trying to learn how to program and I have been studying the tutorials here and reading C++ Programming in easy steps by Mike McGrath. I have the first three lessons down pretty well, but I get in over my head about halfway through Functions. So I went back to Lesson 2: If Statements and tried to write my own little program for my out-of-town gf.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    { 
        int name;
        
        cout<<"Please give your full name. Don't forget your capitalization.: ";
        cin>> name;
        cin.ignore();
        if ( name == Jennifer ***** ******** ) {
             cout<<"Hello love.\n";
        }
        else {
             cout<<"Hey! What have you done with my girlfriend?!\n";
        }
        cin.get();
    }
    I get these errors:
    `Jennifer' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)


    If I change this line
    Code:
     if ( name == Jennifer ***** ******** ) {
    To this
    Code:
    if ( name == "Jennifer ***** ********" ) {
    I get this error
    ISO C++ forbids comparison between pointer and integer

    This is what I was using as a reference
    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            // Most important part of the program!
    {
      int age;                            // Need a variable...
      
      cout<<"Please input your age: ";    // Asks for age
      cin>> age;                          // The input is put in age
      cin.ignore();                       // Throw away enter
      if ( age < 100 ) {                  // If the age is less than 100
         cout<<"You are pretty young!\n"; // Just to show you it works...
      }
      else if ( age == 100 ) {            // I use else just to show an example 
         cout<<"You are old\n";           // Just to show you it works...
      }
      else {
        cout<<"You are really old\n";     // Executed if no other statement is
      }
    
      cin.get();
    }
    I took out the else..if because in my case it is either right or wrong, and I really don't know what else to try. I know this is extremely basic but I am having trouble grasping any of it.

    Thanks for any help.

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    First of all for an input of a character, name has to be declared as a char type instead of int, int is for numbers. second:

    Code:
    if ( name == Jennifer ***** ******** )
    whats with the *? if you just want to see if its Jennifer,
    cant you do this?

    Code:
     if (name == 'Jennifer')
    Last edited by JoshR; 03-28-2005 at 04:47 PM. Reason: spelling

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to store the users response in a data type that is appropriate to what you expect. In this case, you cannot store a person's name in an integer variable, integers store numbers and are not suited to storing a string of characters. What you are probably looking for is a string container.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    { 
        string name;
        
        cout<<"Please give your full name. Don't forget your capitalization.: ";
        getline(cin,name);
        cin.ignore();
        if ( name == "Jennifer ***** ********" ) {
             cout<<"Hello love.\n";
        }
        else {
             cout<<"Hey! What have you done with my girlfriend?!\n";
        }
        cin.get();
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    couldnt you use a switch also? sorry i am unfamiliar with strings, learning them in my next lesson :-P

  5. #5
    Brand New
    Join Date
    Mar 2005
    Location
    South Carolina
    Posts
    3
    Quote Originally Posted by JoshR

    whats with the *?

    The * are so I don't post her full name on the internet.

    So the getline (cin,name); takes the user's input (cin) and compares it to what I have specified, which is the string name;?

    Thanks for the help

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    getline simply reads a line from the stream 'cin', and stores what it read in into the variable 'name' (in this case). The comparison is done inside the conditional 'if ( ... comparison-here ... )', which is demonstrated by hk_mp5kpdw.

    A switch will only work for integral types: int, char, enum, etc.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Brand New
    Join Date
    Mar 2005
    Location
    South Carolina
    Posts
    3
    Taken directly from lesson 9:

    If you want the user to input his or her name, you must use a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Consumer program
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 05:21 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM