How can I fix this with a nested loop?

I wrote a program that asks a user to input letters to be converted into telephone digits for a phone number. I got it to work with a switch case in a loop but i want to ask the user if they want to go again. if they do, reset counter to 0 and go back into the loop after they are prompted to enter more letters for a phone number.

I know i need to use a nested loop but I'm just stuck and can't figure out how to rearrange this so i can get what i want...i feel stupid :(

I'm sorry if it seems like I'm just asking for an answer here, but i need some more helpful hints, something just isn't clicking in my head...

I am aware that some of it isn't doing anything and that's just because I'm still in the process of writing it

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
93
94
  #include <iostream>
#include <string>

using namespace std;

int main()
{
	char letter, goAgain = ' ';
	int counter = 0;

	cout << "Enter letters to be converted to their corresponding telephone digits: ";

	if (goAgain == 'Y' || goAgain == 'y')
	{
		counter = 0;
	}
	
	
	while (counter < 7)
	{		
		cin.get(letter);

		if (letter >= 'A' && letter <= 'z' && letter != ' ')
		{
			counter++;
		}
		if (letter > 'Z')
		{
			letter = letter - 32;
		}
		if (counter == 4)
		{
			cout << '-';
		}
		switch (letter)
		{
		case 'A':
		case 'B':
		case 'C':
			cout << "2";
			break;
		case 'D':
		case 'E':
		case 'F':
			cout << "3";
			break;
		case 'G':
		case 'H':
		case 'I':
			cout << "4";
			break;
		case 'J':
		case 'K':
		case 'L':
			cout << "5";
			break;
		case 'M':
		case 'N':
		case 'O':
			cout << "6";
			break;
		case 'P':
		case 'Q':
		case 'R':
		case 'S':
			cout << "7";
			break;
		case 'T':
		case 'U':
		case 'V':
			cout << "8";
			break;
		case 'W':
		case 'X':
		case 'Y':
		case 'Z':
			cout << "9";
			break;
		default:
			break;
		}
		if (counter >= 7)
		{
			cout << "Would you like to go again? (Y/N): ";
			cin >> goAgain;
			cout << endl;
		}
	} 

	

	system("pause");
	return 0;
}
Let say you have:
1
2
3
4
5
6
7
int main()
{

  // the code that does a thing once

  return 0;
}

And you want something like:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  char repeat = 'y';
  while ( repeat == 'y' ) {

    // the code that does a thing once

    std::cin >> repeat;
  }

  return 0;
}

Use a separate function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

void convertTelephone()           // Put your conversion routine in this function
{
   cout << "This will convert a telephone number once it's written.\n";
}


int main()
{
   char goAgain = 'y';
   while ( goAgain == 'y' || goAgain == 'Y' )
   {
      convertTelephone();        // Call function here	

      cout << "Would you like to go again? (Y/N): ";
      cin >> goAgain;
   } 
}
Last edited on
Topic archived. No new replies allowed.