Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,906 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,760 people online right now. Registration is fast and FREE... Join Now!




problem about creating functions.

 
Reply to this topicStart new topic

problem about creating functions.

travigne
16 Mar, 2008 - 09:58 PM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2008
Posts: 3

the question for this program is :write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers in a file conatining a list of names and phone numbers. The user should be prompted to enter a first and last name, and the program then outputs the the corresponding number, or indicates that the name isn't in the directory.

for this program, i'm supposed to use functions to code the problem. The code works but it runs incorrectly. When I enter name, it asks me to repeat the process instead of giving me the number. when i type 'n', it gives me the number. Really appreciate your help! Thank you.

CODE
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

bool checkName(string, string, int, string, string);
bool cont(char);

int main()
{
    ifstream inData;  
    inData.open("phone.dat");

    if(!inData)
      {
      cout << "Invalid file. Please check it again" << endl;
      return 1;
      }

      string firstName, lastName, first, last;
      int number;
      char yes = 'y';

      cout << "Enter a first and last name: "<< endl;
      cin >> first >> last;
      while(cont(yes))
       {  inData >> firstName >> lastName >> number;
           while (inData)
          {
           checkName(firstName,lastName,number,first,last);
          // inData >> firstName >> lastName >> number;
          }          
           inData.close();
           inData.open("phone.dat");
       }
        if (inData)
        cout << number;
     else
        cout << "The name isn't in the directory" << endl;  
      return 0;
}

bool checkName(string firstName,
               string lastName,
               int number,
               string first,
               string last)
{
     bool Mybool = true;

     if ( first == firstName && last == lastName)
        return 1;
     else
        return 0;
}

bool cont(char yes)
{    
      bool myBool = true;
      cout << "Do you want to look up another number(y/n)?"<< endl;
      cin >> yes;
      if (yes == 'y')
         return 1;
      else return 0;    
        
}







User is offlineProfile CardPM
+Quote Post

travigne
RE: Problem About Creating Functions.
17 Mar, 2008 - 10:26 AM
Post #2

New D.I.C Head
*

Joined: 21 Feb, 2008
Posts: 3

no one helps sad.gif, i just wanna know which part is wrong in my code cuz i'm very new to C++.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Problem About Creating Functions.
17 Mar, 2008 - 10:53 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,228



Thanked: 40 times
Dream Kudos: 25
My Contributions
Can you elaborate as to what exactly you want the program to do? What should happen when you enter a name? Your while loop is currently structured to ask if the user wants to continue, while your code to display the number is outside of the loop.
User is offlineProfile CardPM
+Quote Post

travigne
RE: Problem About Creating Functions.
17 Mar, 2008 - 02:33 PM
Post #4

New D.I.C Head
*

Joined: 21 Feb, 2008
Posts: 3

when i enter the name that exists in the file, the program will output the corresponding number, then it also asks me if i want to look up another name, if yes, the program repeats the process, if no, the program terminates. also, If i enter the name that is not in the file, the program will indicate that the name is not in the file and asks me if I want to look up another name.

the file that i create is somewhat like this:

David Cook 123456
Jen Well 34567
......

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 09:48PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month