I am as lost as human being can possibly be

So,

This is my first year ever learning about computers etc and I have been doing pretty well learning C++ and though I am nowhere close to being a mediocre programmer, seeing that I only know C++, I am pretty optimistic. Then my professor gave us this dementor of an assignment and I am lost. If anyone can help me out I would be grateful.

So here are the instructions,

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.

Yes it may be very amateur and I apologize but bear with me please I literally started learning this a couple weeks ago.

So far I am here...

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
92

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

string digitizer(string NPN, string PPN[]);

int main()
{
	const int ref = 15;
	int PN;
	int index = 0;

	string PPN[ref];

	cout << "Please enter a phone number : ";
	cin >> PN;
	
	string NPN = to_string(PN);

	string digitizer(string NPN, string PPN[]);

	cout << "Your phone number is : " << PPN[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] << endl;




	system("Pause");
	return 0;

}

string digitizer(string NPN, string PPN[])
{
	int indexthree = 0;
	int indextwo = 0;

	while (PPN[indexthree] != ".")
	{

		if (NPN[indextwo] >= '0'&&NPN[indextwo] <= '9')
		{
			if (indexthree = 0, 3, 8)
			{
				if (indexthree = 0)
				{
					PPN[indexthree] = "(";
					indexthree++;
					PPN[indexthree] = NPN[indextwo];
				}
				else
				{
					if (indexthree = 3)
					{
						PPN[indexthree] = ")";
						indexthree = indexthree + 3;
						PPN[indexthree] = NPN[indextwo];
					}
					else {
						if (indexthree = 8)
						{
							PPN[indexthree] = "-";
						}

						indexthree = indexthree + 2;
						PPN[indexthree] = NPN[indextwo];
					}
				}
			}
			else {
				indexthree++;
				PPN[indexthree] = NPN[indextwo];
			}

			indextwo++;

		}
		else {
			indextwo++;
		}


	}





	return  NPN, PPN[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
}
You have a nice start that shows you are thinking and getting a feel for the language and what to do.

this is actually a nice real world problem. Data entry people often mangle data and you have to have software that cleans up behind them. You never know what you might get.

what I would do here is iterate over your input string, and try to extract (copy to another string) 10 digits (there is an is digit function you can use for this...). If you find 10 digits, format just those (throw everything else away) into the output style and be done. If you don't find 10 digits, reject the string as unfixable. If the string is < 10 long, reject it immediately.

Your code is too complicated and won't work on every possible mangled input string. The above algorithm is simpler and will work no matter what you are provided.

Can you code it using this idea? What you did so far makes me think you can, try it and come back if you get lost again.

Last edited on
I will try. I just tried changing everything to C-string and even tried using itoa as the professor said but it not only doesnt work but its just as annoying
Last edited on
I tried to simplify it and here it is..
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
//Lab #09 C++ Written by Borel U.Samaga De Tembiwa	
#include <iostream>
#include <string>
#include <sstream>
#include <string.h>
#include <iomanip>
using namespace std;

string digitizer(string NPN, string PPN[]);

int main()
{
	const int ref = 15;
	string NPN;
	int index = 0;
	string PPN;


	cout << "Please enter a phone number : ";
	cin >> NPN;

	string digitizer(string NPN, string PPN[]);


	cout << "Your phone number is : " << PPN << endl;
	cout << index << endl;



	system("Pause");
	return 0;

}

string digitizer(string NPN, string PPN[])
{
	int indexthree = 0;
	int ind = 1;
	int ref = 15;

	PPN[ref];

	string a = "(";
	string b = ")";
	string c = " - ";

	PPN[1] = { a };
	PPN[5] = { b };
	PPN[9] = { c };

	do{

		if (NPN[1] || NPN[5] || NPN[9])
		{
			ind++;
			PPN[ind] = NPN[ind];
		}
		else
		{
			if (isdigit(NPN[ind]))
			{
				PPN[ind] = NPN[ind];
				ind++;
			}
			else {
				ind++;
			}
		}

		indexthree++;
	} while (indexthree < 14);


	return  PPN[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
}





I tried to even debug it and I have a feeling that it isn't even stepping into the function. I really don't know what to do at this point. The index is there just for the sake of assuring myself that it is not a problem with the output.
Last edited on
I am not sure if I understand the assignment correctly. Normally you would create a second string and copy all the digits into the second string. If the length of the second string is 10 the input is correct. Finally you use string::substring the extract the 3 groups you need and display them properly.
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>

using namespace std;

string copy_digits(const string& input)
{
  string result;

  for (char ch: input)
    if (isdigit(ch))
      result += ch;

  return result;
}

int main()
{
  cout << "Enter a phone number: ";
  string phone_number;
  getline(cin, phone_number);
  phone_number = copy_digits(phone_number);
  if (phone_number.size() != 10)
    cerr << "Error: invalid format - the number must contain 10 digits.\n\n";
  else
  {
    cout << "(" << phone_number.substr(0, 3) << ")"
         << " " << phone_number.substr(3, 3) << " "
         << phone_number.substr(5, 4);

  }
  cout << "";

  system("pause");
  return 0;
}


EDIT - Fixed a little bug with substr
Last edited on
I am not sure if I understand the assignment correctly. Normally you would create a second string and copy all the digits into the second string. If the length of the second string is 10 the input is correct. Finally, you use string:: substring the extract the 3 groups you need and display them properly.


You understood it much better than I did mate. You're are a hero, thank you so much dude. I don't truly understand much of it but I will try to.
Topic archived. No new replies allowed.