Like Tree2Likes

C++ Phone Dialer Program

This is a discussion on C++ Phone Dialer Program within the C++ Programming forums, part of the General Programming Boards category; A working Solution... Finally: Code: # include <iostream> # include <string> # include <cctype> # include <iomanip> using namespace std; ...

  1. #31
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    A working Solution... Finally:

    Code:
    # include <iostream>
    # include <string>
    # include <cctype>
    # include <iomanip>
    using namespace std;
    // prototypes
    int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5,char &d6, char &d7, char &d8);
    char Todigit(char &d);
    void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);
     
    // function main
    int main()
    {
    	char d1, d2, d3, d4, d5, d6, d7, d8;
    	int ReturnValue = 0;
    	while (ReturnValue != -5)
    	{
    		ReturnValue = ReadDials (d1, d2, d3, d4, d5, d6, d7, d8);
    		switch (ReturnValue)
    		{
    		case -1: cout << "Error- An invalid character was entered" << endl; break;
    		case -2: cout << "Error- Phone number cannot begin with 0" << endl; break;
    		case -3: cout << "Error- Phone number cannot begin with 555" << endl; break;
    		case -4: cout << "Error- Hyphen is not in the correct position" << endl; break;
    		default: AcknowledgeCall (d1, d2, d3, d4, d5, d6, d7, d8);
    		}
    	}
     
    	return 0;
    }
     
    // function ReadDial
    int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
    {
        int ReturnValue = 0;
        cout << "Enter a phone number (Q to quit): ";
        cin >> d1; 
        if(d1 == 'Q' || d1 == 'q') return -5; 
        cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;  
        ReturnValue = Todigit(d1);
        if (ReturnValue == -1)
            return ReturnValue;
        ReturnValue = Todigit(d2);
        if (ReturnValue == -1)
            return ReturnValue;
        ReturnValue = Todigit(d3);
        if (ReturnValue == -1)
            return ReturnValue;
        if (d4 != '-')
            return -4;
        ReturnValue = Todigit(d5);
        if (ReturnValue == -1)
            return ReturnValue;
        ReturnValue = Todigit(d6);
        if (ReturnValue == -1)
            return ReturnValue;
        ReturnValue = Todigit(d7);
        if (ReturnValue == -1)
            return ReturnValue;
        ReturnValue = Todigit(d8);
        if (ReturnValue == -1)
            return ReturnValue;
        if (d1 == '0')
            return -2;
        if (d1 == '5' && d2 == '5' && d3 == '5')
            return -3;
        return 0;
     
    }
     
     
    //function ToDigit
     
    char Todigit(char &d)
    {
    d = toupper(d);	
    switch(d)
        {
            case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': break;
            case 'A': case 'B': case 'C': d = '2'; break;
            case 'D': case 'E': case 'F': d = '3'; break;
            case 'G': case 'H': case 'I': d = '4'; break;
            case 'J': case 'K': case 'L': d = '5'; break;
            case 'M': case 'N': case 'O': d = '6'; break;
    		case 'P': case 'Q': case 'R': case 'S': d = '7'; break;
            case 'T': case 'U': case 'V': d = '8'; break;
    		case 'W': case 'X': case 'Y': case 'Z': d = '9'; break;
            default: return -1;
        }
    }
     
     
    // function AcknowledgeCall
    void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8)
    {
     
     
    cout << "Phone Number Dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << endl << endl;
    }

  2. #32
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    thanks for all your help!

  3. #33
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Since you are done, I have something similar:
    Code:
    int ReadDials (char &, char &, char &, char &, char &, char &, char &, char &);
    int ToDigit (char &);
    void AcknowledgeCall(char, char, char, char, char, char, char, char);
    
     int main()
     {
        char d1, d2, d3, d4, d5, d6, d7, d8;
        int ReturnValue = 0;
    
        while (ReturnValue != -5)
        {
            ReturnValue = ReadDials(d1, d2, d3, d4, d5, d6, d7, d8);
            switch(ReturnValue)
            {
                case -1: cout <<"ERROR- An invalid character was entered." << endl; break;
                case -2: cout <<"ERROR- Phone number can't begin with 0." << endl; break;
                case -3: cout <<"ERROR- Phone number can't begin with 555." << endl; break;
                case -4: cout <<"ERROR- Hyphen is not in the correct posistion." << endl; break;
                case -5: cout <<"Good bye!" << endl; break;
                default: AcknowledgeCall(d1, d2, d3, d4, d5, d6, d7, d8);
            }
        }
        return 0;
     } 
    
     int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
    {
        int i = 0;
        int result = 0;
        cout << "Enter a phone number in the following format (123-4567): ";
        cin >> d1;
        if(d1 == 'Q' || d1 == 'q')
            return -5;
        cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
    
        
        if(d1 == '0')
            return -2;
        else if(d1 == '5' && d2 == '5' && d3 == '5')
            return -3;
        else if(d4 != '-')
            return -4;
    
        result = ToDigit(d1);
        if(result != 0)
            return result;
        result = ToDigit(d2);
        if(result != 0)
            return result;
        result = ToDigit(d3);
        if(result != 0)
            return result;
        result = ToDigit(d4);
        if(result != 0)
            return result;
        result = ToDigit(d5);
        if(result != 0)
            return result;
        result = ToDigit(d6);
        if(result != 0)
            return result;
        result = ToDigit(d7);
        if(result != 0)
            return result;
        result = ToDigit(d8);
        if(result != 0)
            return result;
        
    
        return 0;
    }
    
    int ToDigit(char &d)
    {
        int i = 0;
    
        for(i = 0; i < 8; i++)
        {
            if(static_cast<int>(d) < 48 || static_cast<int>(d) > 57)
            {
                switch(toupper(d))
                {
                case 'A': case 'B': case 'C':
                    d = '2';
                    break;            
                case 'D': case 'E': case'F':
                    d = '3';
                    break;
                case 'G': case 'H': case 'I':
                    d = '4';
                    break;
                case 'J': case 'K': case 'L':
                    d = '5';
                    break;
                case 'M': case 'N': case 'O':
                    d = '6';
                    break;
                case 'P': case 'Q': case 'R': case 'S':
                    d = '7';
                    break;
                case 'T': case 'U': case 'V':
                    d = '8';
                    break;
                case 'W': case 'X': case 'Y': case 'Z':
                    d = '9';
                    break;
                case '-':
                    //skip, hyphen is OK
                    break;
                default:
                    //error, invalid input
                    return -1;
                }
            }
        }
        return 0;
    }
    
    
    void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8)
    {
        cout << "Phone Number Dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << endl;
    }

  4. #34
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,606
    >>int ReadDials (char &, char &, char &, char &, char &, char &, char &, char &);
    You may want to read SourceForge.net: Do not remove parameter names - cpwiki

    >>if(static_cast<int>(d) < 48 || static_cast<int>(d) > 57)
    A reader will very likely have no idea what this means. Don't use these so called "magic numbers," because they mean nothing.
    Worse still, you are relying on ANSI, which may or may not be the case. Why not just handle the cases you need, and fail on the rest?
    It's essentially what you are doing already, just with a pointless if statement.

    Also, you are passing in one character and processing it 8 times. Doesn't make much sense.
    AndrewHunter likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Thanks for the link, I'll implement that code with my prototypes from now on.

    Also, I should have looked over that code a little better. I originally used a string and converted back to using chars because a string wasn't suppose to be used. That loop in ToDigit() doesn't do anything for this code.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phone Conversion Program
    By jweidm02 in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2010, 09:31 AM
  2. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  3. making a phone call in a program
    By jverkoey in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2004, 04:09 PM
  4. phone directory program
    By cdummy in forum C Programming
    Replies: 1
    Last Post: 04-13-2002, 11:23 AM
  5. Dialer to connect my ISP through Modem
    By zahid in forum C++ Programming
    Replies: 0
    Last Post: 11-13-2001, 02:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21