Thread: Matching user info with data in file

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    20

    Matching user info with data in file

    I have a file with first and last names and phone numbers. Now, I need to writte a program that gets a name from a user, and if the name is in the file, display the corresponding number, but if the name is not in the file, the program writes a message saying that the name is not in the file. Also, the program needs to repeat the process until the user indicates that they would like to stop. Here is my code/psudocode...

    Code:
    #include <iostream>
    
    using namespace std;
    
    void GetFirstName and GetLastName( string)              // Function prototypes
    void Printphonenumber (int )
    void PrintQuestion (char)
    int main()
    {
        string FirstName;    // The outside temperature
        string LastName;
        int PhoneNumber
        char question;
        GetName(FirstName & LastName);                   // Function call
        PrintPhoneNumber(PhoneNumber;  
        PrintQuestion(Question);           // Function call
        
        
        
        // Declare and open input file
        ifstream inData;         // Input file of readings
        inData.open("phonedir.dat");
        if ( !inData )           // Did input file open correctly?
        {   // no
            cout << "Can't open input file." << endl;
            return 1;            // Terminate program
            
            
        }return 0;
    }
    
    //******************************************************************
    
    void GetFirstName and GetLastName ( string FirstName and LastName )                   // Reference parameter
    
    // This function prompts for a first name and last name to be entered
    
    {
        cout << "Enter first name" << endl;
        cin >> FirstName;
        cout << "Enter Last name " << endl;
        cin >> LastName;
    }
    
    //******************************************************************
    
    void Printphonenumber( string phonenumber )              // Value parameter
    
    // This Function outputs the corresponding number or indicates that the name
    // is not in the directory.  
    
    If the first and last name are found in the file, then output corresponding number.
    else
        cout << "The name could not be found in the directory
    
    //*******************************************************************
    
    void (PrintQuestion) (char)
    
    //This function prints a question asking the user to find another number.  
    //If no, terminate program.
    
    cout << "Do you want to lookup another number? (Enter Y or N)" << 
    cin >> char;
           If (char == 'Y')
           return();
           If (char == 'N')
           return 1;          //Terminate Program
    Would this be valid?? if (FirstName && LastName=="phonedir.dat")
    cout << phonenumber;
    else
    cout << "The name is not in the file";

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're missing a semicolon here:
    Code:
    int PhoneNumber;
    Code:
    If (char == 'Y')
           return();
    C++ is case sensitive, so If isn't the same as if. char isn't a valid variable name.
    Code:
    return 1;          //Terminate Program
    You can't return 1 from a void function. Nor will doing so terminate the program; only when you return from main() or call exit() will that happen.
    Code:
    void (PrintQuestion) (char)
    That isn't a valid function definition; the ()s around the function name are extraneous (unnessesary), and you need to specify a variable name for the parameter (unlike in function prototypes).
    GetName(FirstName & LastName); // Function call
    I really think that that doesn't do what you want. To pass two parameters to a function, you need to declare the function as such.

    You should read some of the tutorials, especially the function tutorial.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    Code:
    //******************************************************************
    // Activity program
    // This program outputs the phone number for a given name, 
    //if the name is found in a file
    //******************************************************************
    #include <iostream>
    
    using namespace std;
    
    void GetFirstName and GetLastName( string, string)              // Function prototypes
    void PrintPhoneNumber (int )
    void PrintQuestion (char)
    int main()
    {
        string FirstName;    // The outside temperature
        string LastName;
        int PhoneNumber
        char question;
    
        GetFirstName and GetLastName(FirstName,LastName);                   // Function call
        PrintPhoneNumber(PhoneNumber);  
        PrintQuestion(question);           // Function call
        
        
        
        // Declare and open input file
        ifstream inData;         // Input file of readings
        inData.open("phonedir.dat");
        if ( !inData )           // Did input file open correctly?
        {   // no
            cout << "Can't open input file." << endl;
            return 1;            // Terminate program
        }
    	return 0;
    }
    
    //******************************************************************
    
    void GetFirstName and GetLastName ( string FirstName, string LastName )                   // Reference parameter
    
    // This function prompts for a first name and last name to be entered
    
    {
        cout << "Enter first name" << endl;
        cin >> FirstName;
        cout << "Enter Last name " << endl;
        cin >> LastName;
    }
    
    //******************************************************************
    
    void Printphonenumber( string PhoneNumber )              // Value parameter
    
    // This Function outputs the corresponding number or indicates that the name
    // is not in the directory.  
    
    if the first and last name are found in the file, then output corresponding number.
    else
        cout << "The name could not be found in the directory
    
    //*******************************************************************
    
    void (PrintQuestion) (char)
    
    //This function prints a question asking the user to find another number.  
    //If no, terminate program.
    
    cout << "Do you want to lookup another number? (Enter Y or N)" << 
    cin >> question;
           if (question == 'Y')
           //repeat process
    	   {
    		   GetFirstName and GetLastName(FirstName,LastName);                   // Function call
    	       PrintPhoneNumber(PhoneNumber); 
    	   }
           if (question== 'N')
           return 1;          //Terminate Program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM