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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "stdafx.h"
#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;
}
Couple problems I see. First, why are you passing it userNumber? It doesn't have a value. 2 options 1. would be to make the function a void and pass it via reference and because it is a void you don't return anything 2. you don't pass it anything and you just return userNumber.

If you end up passing it via reference then your call to the function is good but if just return userNumber then you have to set userNumber in main equal to the function call.

Your code would look something like this for passing it via reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const int NUM_LENGTH = 10;

void ReadAndValidateUserNumber(string &userNumber);

int main()
{
	string userNumber;

	ReadAndValidateUserNumber(userNumber);

	cout << "The number is: " << userNumber << endl;

	return 0;
}

void 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;
		}
	}

}


and this is for just returning the number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const int NUM_LENGTH = 10;

string ReadAndValidateUserNumber();

int main()
{
	string userNumber;

	userNumber = ReadAndValidateUserNumber();

	cout << "The number is: " << userNumber << endl;

	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;
}
Hello bmoney,

Instead of a lengthy explanation here is the changes I made with comments of what I did. If you do not understand anything let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <iomanip>  //  setw()
#include <conio.h>  // _getch()
#include <Windows.h>
#include <thread>  // For wait time code. Used in the last if in the function.
#include <chrono>  // For wait time code Used in the last if in the function.
#include <cctype>

#define CLS system("cls")


std::string ReadAndValidateUserNumber(); // Removed the parameter, its not needed her.

const int NUM_LENGTH = 10;

int main()
{
	std::string userNumber{ "" };

	userNumber = ReadAndValidateUserNumber();  //  userNumber set equal to the return of the function.

	std::cout << userNumber << std::endl;

    std::cout << "\n\n\n\nFin Press any key to continue -- > ";
    while (!_kbhit()) {}
    return 0;
}

std::string ReadAndValidateUserNumber() //  Removed the paramter, not neded here.
{
	bool check = false;
	std::string temp{ "" }; //  Used to extract userNumber digits into.
	std::string userNumber{ "" };  // Defined here because only a copy was passed to the function.

	while (!check)
	{
		CLS;

		std::cout << "Please enter a Number: ";
		std::getline(std::cin, userNumber);

		if (userNumber.length() < NUM_LENGTH) //  May need to adjust the number for NUM_LENGTH?
			                                  //  At this point "userNumber.length() needs to be at lease
			                                  //  10 or more to check the number entered.
			std::cout << "The phone number may contain 10 digits only. \n";

		else
		{
			std::cout << userNumber << std::endl;
			
			/*
			   userNumber.length() returns an unsigned int this causes a mismatch between i and
			   userNumber.length(). int i should be either unsigned int or size_t which is an unsigned int.
			   But it is not important it just creates a warning no error.
			*/
			for (int i = 0; i < userNumber.length(); i++)
			{
				if (isdigit(userNumber[i]))
				{
					temp += userNumber[i];
				}
			}
		}

		if (temp.length() == 10)  //  if temp length = to 10 insert the punctuation otherwise needs an else.
		{
			// Moved all here to deal with a string of only numbers.

			check = true;  //  Moved here for the next if to work properly.
			//  Uncommented lines will produce the correct output.
			temp.insert(0, "(");
			temp.insert(4, ")");
			temp.insert(8, "-");
		}

		//std::cout << temp << std::endl;  //  Used for tsting.

		if (!check)
		{
			std::cout << "Invalid Entry! Please try again." << std::endl;
			std::this_thread::sleep_for(std::chrono::seconds(3));  //  Used to pause the program.
			                                                       //  include <chrono> and <thread> headers.
		}
	}

	return temp;
}


I removed some code that would take several extra files to make the program work the way I use it.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.