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

  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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are only ever reading one name from the file (specifically the first one). If you expect your file to contain more than one name, you should put the read-n-check section in a loop (and loop until the file is has been read completely).

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Would a sentinel-controlled loop work? Would I put the while right be for the cout<<"Enter a first and last name."<<endl; ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sdoyle View Post
    Would a sentinel-controlled loop work? Would I put the while right be for the cout<<"Enter a first and last name."<<endl; ?
    A sentinel-controlled loop would work, if there's a sentinel in your data. If there isn't, it won't. You wouldn't want to ask for the name more than once, though.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    What loop would you recommend?
    Last edited by sdoyle; 11-05-2013 at 06:24 PM.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    I am trying to use a EOF loop so that the program will look through the whole file for the names. But I am having a problem with where to put the while and what to put in the () I have tried putting while(infile) and while(first==firstfile && last==lastfile) which have not worked. I put the while() after the cin>>first>>last; I am not sure what else to try. thanks for the help


  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should almost certainly be checking whether the read itself worked, so the read statement is going to be your loop condition.

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Is the read statement the infile>>firstfile>>lastfile>>numberfile;? How do I check and see if the read is working?
    Sorry I am sure that is something I should know.
    Last edited by sdoyle; 11-05-2013 at 07:55 PM.

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    This is what my program looks like and it still only prints out the first name in the files number.

    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;
        cont='Y'||'y';
        ifstream infile; 
        
        infile.open("names and numbers.dat"); //opening the file
        
        
    while(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<<firstfile<<" "<<lastfile<<"'s number is "<<numberfile<<endl; //this is printed
        
    elseif(!(first==firstfile && last==lastfile))
        
            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;
        
    if (!cont=='y'||'Y')
            
    return 0;
        }
        
        infile.close(); 
    //close file
        
        cin>>chr;
        
    return 0;
    }
    

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. "cont='Y'||'y'" is not at all doing what you want. You can't assign a truth value to a character. (Well, you can, obviously, since you are, but the result will not be an actual printable character.)

    2. You have to loop after you ask for the name to check, because you want to check that one single solitary name against all the names in the file. Inside the loop you should just check if it's equal (and if so, print appropriate message and stop the loop).

    3. You can't print the "sorry" message inside the loop, because you don't know if you're sorry yet (you won't be sorry until you've checked all the names and none of them match).

  11. #11
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Yippy all the names and numbers are working. However, the "sorry" message is always printing and I used a while(true) right before the cout<<"Enter a first and last name."<<endl; and the program repeats but when you enter a second name the number doesn't print and I can't seem to get the program to end. Thanks so much for your help.

  12. #12
    Registered User
    Join Date
    Nov 2013
    Posts
    11
    Changed my while(true) to bool again= true;
    char cont;
    ifstream infile;
    infile.open(
    "names and numbers.dat"); //opening the file
    while(again)
    {
    cout<<
    "Enter a first and last name."<<endl; //Asking user for the input

    But it is still not showing the number for the second name input. and the "sorry" still printing all the time

    Last edited by sdoyle; 11-05-2013 at 10:14 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