getting a strange output in the console

I am also not able to get my letters to convert to numbers

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//prototypes
int readials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8);
int todigit (char &d);
void acknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);


int main()
{
char d1, d2, d3, d4, d5, d6, d7, d8;
int return_value = 0;



	do
	{
		 return_value = readials(d1, d2, d3, d4, d5, d6, d7, d8);
			if(return_value == -5) break;
		switch(return_value)
		{
		case -1:  cout << "ERROR - An invalid character was entered"; break;
		case -2:  cout << "ERROR - Phone number cannot begin with 0"; break;
		case -3:  cout << "ERROR - Phone number cannont begin with 555"; break;
		case -4:  cout << "Error - Hyphen is not int the correct postion"; break;
		default:  acknowledgeCall(d1,d2,d3,d4,d5,d6,d7,d8);

		}

	}
	while (true);

	

return 0;

}

int readials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
{

int return_value;

cout << "Input the first digit";
cin >> d1;
if (d1 == 'Q' || d1 == 'q')
return -5;

cout << "Please continue";

cin >> d2, d3, d4, d5, d6, d7, d8;


	return_value = todigit(d1);
	if (return_value != -1)
		return return_value;

	return_value = todigit(d2);
	if (return_value != -1)
		return return_value;

	return_value = todigit(d3);
	if (return_value != -1)
		return return_value;

	return_value = todigit(d4);
	if (return_value != '-')
		return -4;

	return_value = todigit(d5);
		if (return_value != -1)
			return return_value;

	return_value = todigit(d7);
		if (return_value != -1)
			return return_value;


	return_value = todigit(d8);
		if (return_value != -1)
			return return_value;

	if (d1 == '0')
	return -2;

	if (d1 == '5' && d2 == '5' && d3 == '5')
	return -3;



return 0;
}

int todigit(char &d)
{
	d = toupper(d);

		switch(d)
		{
		case '0': break;
		case '1': break;
		case '2': break;
		case '3': break;
		case '4': break;
		case '5': break;
		case '6': break;
		case '7': break;
		case '8': break;
		case '9': break;

		case 'A': case 'B': case 'C':
			d = '2'; break;

		case 'D': case 'F': case 'E':
			d = '3'; break;

		case 'H': case 'I': case 'G':
			d = '4'; break;

		case 'K': case 'L': case 'J':
			d = '5'; break;

		case 'N': case 'O': case 'M':
			d = '6'; break;

		case 'Q': case 'R': case 'P': case 'S':
			d = '7'; break;

		case 'T': case 'U': case 'V':
			d = '8'; break;

		case 'W': case 'X': case 'Y': case 'Z':
			d = '9'; break;

		default:
			return -1;
		}

return 0;
}

void acknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8)
{
cout << " The number that you dialed was" << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << endl;

}
The problem is line 54.
It should be cin >> d1 >> d2 >> d3 >> //and so on.

Try to use array instead of d1-8.
You could really use an array here.
Another problem:
1
2
3
	return_value = todigit(d1);
	if (return_value != -1)
		return return_value; //exit the function, but do not analize d2-8 
I think it should be if( return_value == -1 )
Last edited on
unfortuantly I am unable to use an array, that is next weeks lesson.
Are you using g++? if so you can do this:
case '0' ... '9': break;
I'm not sure what this code is trying to do, please explain.
It is recieving a phone number and printing it to the screen using functions.
Well that's a feature i've never come across! Please enlighten me, how is this wonderful action happening? There must be an embedded goto somewhere!
I am just calling functions. The problem that I am having is in line 29. When switched line 47 is printed to the screen, however it only prints one or two digits at a time followed by blocks of lines. It does not print the entire statement at once.
That's not what rocketboy9000 was asking what this:

1
2
3
4
5
6
7
8
9
10
 case '0': break;
 case '1': break;
 case '2': break;
 case '3': break;
 case '4': break;
 case '5': break;
 case '6': break;
 case '7': break;
 case '8': break;
 case '9': break;


did. When you are saying you are calling functions with it, you are incorrect. You are just breaking the loop so you don't return an error if it's already a number.

edit: adding a suggestion

So, i'm wondering, is there a particular reason you aren't just using
http://www.cplusplus.com/reference/clibrary/cctype/
to check if it's a number (I just think that hard coding it in the switch is a little unnecessary.
Last edited on
I see, would placing them all in case statment be better like I did with the letters in line 114-135 or just omit them all together?
See edited post above for a useful function =)
thanks.
Topic archived. No new replies allowed.