CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  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

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Phone Number Program

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#']

    I am stuck on this assignment for my programming fundamentals
    You don't actually say what is the problem?

    Code:
    cout << "Please enter a Number: ";
    cin >> userNumber;
    
    if (userNumber.length() != NUM_LENGTH)
    	cout << "The phone number may contain 10 digits only. \n";
    You are entering the phone number as a string and then checking if the length of the entered string is the required 10. However, the assignment is to check that the entered string contains 10 digits, not that its total length is 10.

    Also note that string stream extraction does not extract as one string characters containing a space. The extraction will only obtain the characters prior to the space. To obtain the whole entered string use .getline(). See http://www.cplusplus.com/reference/s...tring/getline/

    Code:
    userNumber.insert(0, "(");
    userNumber.insert(4, ")");
    userNumber.insert(8, "-");
    
    for (int i = 0; i < userNumber.length(); i++)
    {
    	if (isdigit(userNumber[i]))
    	{
    		userNumber = NUM_LENGTH;
    	}
    }
    userNumber is of type string and NUM_LENGTH is of type int. You can't assign an type int to a type string. Also, before the loop you are inserting characters into the string and then checking for non-digits in the string!

    I would suggest that you first work out on paper how to determine if a given string is a valid phone number, then produce a program design and then code the program from the design.
    Last edited by 2kaud; December 14th, 2016 at 05:51 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2016
    Posts
    16

    Re: Phone Number Program

    Quick scanning...

    your Boolean check flag is not taken care of. Your while loop is so the second check flag is never triggered...

    maybe I'm not good with strings, but user number input seems to exceed 10 438-848-2415.. unless condesned like 5142528448, most cases

    also

    userNumber = NUM_LENGTH

    seems a looping condition rather than a string building instructions

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