CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Dec 2016
    Posts
    1

    Phone Number Program

    Hello everyone, I am new to this forum and am looking for some assistance, any help would be greatly appreciated! I am stuck on this assignment for my programming fundamentals 1 class and am desperate at this point as I have been stuck for hours on end. Thank you.

    Here is the prompt:
    Many websites ask for phone numbers. The problem is that there are so many
    different ways to represent a phone number. Examples include 817-555-1234,
    817 555 1234 (c), and (817) 555-1234 x23. Write a C++ program which inputs
    a string containing a phone number in any format and outputs it in the standard
    format. For this assignment, the standard format is (817)555-1234.
    Your c++ program should:
    1. Input a string including the number
    2. Copy only the digits from the input string into another string
    3. Issue an error message if the input string does not contain exactly 10
    digits
    4. Output the phone number in standard format

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cctype>
    using namespace std;
    
    const int NUM_LENGTH = 10;
    
    string ReadAndValidateUserNumber(string userNumber);
    
    int main()
    {
    	string userNumber;
    
    	ReadAndValidateUserNumber(userNumber);
    
    	system("PAUSE");
    	return 0;
    }
    
    string ReadAndValidateUserNumber(string userNumber)
    {
    	bool check = false;
    
    	while (!check)
    	{
    		check = true;
    
    		cout << "Please enter a Number: ";
    		cin >> userNumber;
    
    		if (userNumber.length() != NUM_LENGTH)
    			cout << "The phone number may contain 10 digits only. \n";
    		
    		else
    		{
    			userNumber.insert(0, "(");
    			userNumber.insert(4, ")");
    			userNumber.insert(8, "-");
    
    			for (int i = 0; i < userNumber.length(); i++)
    			{
    				if (isdigit(userNumber[i]))
    				{
    					userNumber = NUM_LENGTH;
    				}
    			}
    		}
    
    		if (!check)
    		{
    			cout << "Invalid Entry! Please try again." << endl;
    		}
    	}
    
    	return userNumber;
    }
    Last edited by 2kaud; December 14th, 2016 at 02:28 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured