Thread: C++ begginer I need assistance with my program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    11

    C++ beginner I need assistance with my program

    I am writing a program for a phone directory. The user inputs a name and the program searches the file and either outputs the number or an error because the persons name is not in the file. The program should also ask the user if they would like to continue using the program and look up another number. So far it asks for the name and then prints the error message that I put in place saying that the name is not in the database and asks if I would like to look up another number. I am guessing that I must not really be having my program look in the file but not sure what to do also don't know how to get the program to run again if the user chooses to continue. Any help would be great thanks.




    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    using namespace std;
    char chr;
     
    int main()
    {
        string first;
        string last;
        string number;
        string firstfile;
        string lastfile;
        string numberfile;
        
    char cont;
        ifstream infile; 
        infile.open("name and numbers.dat"); //opening the file
        infile>>firstfile>>lastfile>>numberfile;
            cout<<"Enter a first and last name."<<endl; //Asking  user for the input
        cin>>first>>last;   
    //input the data
        {
        
    if(first==firstfile && last==lastfile)  //if the entered information matches the information in the file
        
            cout<<first<<" "<<last<<"'s number is "<<numberfile<<endl; //this is printed
        }
        
    if(!infile)
        {
            cout<<"Sorry that is not in our database."<<endl;   //if the information doesn't match this is printed
        }
        cout<<"Would you like to search for another name? Y or N"<<endl;  //user is asked if they would like to continue
        cin>>cont;
        
        
        infile.close(); 
    //close file
        
        cin>>chr;
        
    return 0;
    }
    
    Last edited by sdoyle; 11-05-2013 at 06:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ program assistance needed
    By Velocity in forum C++ Programming
    Replies: 31
    Last Post: 10-05-2008, 09:08 PM
  2. I need some assistance! (First C program)
    By charmander in forum C Programming
    Replies: 3
    Last Post: 08-05-2007, 01:16 PM
  3. Program in C... need assistance..
    By xero18 in forum C Programming
    Replies: 2
    Last Post: 10-21-2005, 06:58 AM
  4. Need assistance with program
    By DJ_Mittens in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 08:16 PM
  5. Help with begginer C++ program.
    By EZ15 in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2002, 01:00 AM