Read in a lot of character and space.. then convert alphabet to number and space to -

i just want to know how can i convert a lot of char input into int. I think the one i had done is not right because it does not return integer and my current code only read first char .
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
#include <iostream>
using namespace std;

int getInt(char);
bool isValidChar(char);
int main()
{
	char a;
	cout << "Enter a phone symbols: " ;
    cin.get(a);
	getInt(a);
//	  bool isValidChar(a);
}

int getInt(char a)
{
	
	switch(a)
	{
		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' : cout << "9";
				   break; 
	
	}
}

//bool isValidChar(char a)
//{
//	  if
//} 
int a = cin.get();

Edit: Perhaps not. You want to convert characters into phone pad equivalents? The case statement you have seems fine, except you may want to add capital characters and numerics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int getInt(char c)
{
  switch(c)
  {
    case '1': // 1 only resolves to itself
    {
      return 1;
    }
    case 'a': case 'A':
    case 'b': case 'B':
    case 'c': case 'C':
    case '2': // all these resolve to 2 on phone pad
    {
      return 2;
    }
    ...
 


You'll also need a loop to process the input, maybe:
1
2
3
4
5
6
7
char c;
cout << "Enter phone symbol(s):";
while(cin.get(c))
{
  if(c == ' ' || c == '\n') break; // stop processing on space or return
  int phoneDigit = getInt(c);
}
Last edited on
Thank you so much i've figured out that part because of you. now trying to add another question. it'll ask whether wanna continue or not if answer is Y or y it'll continue the loop again. i also want to run another bool isValidChar to check whether the put is only A-Z,a-z and -. If the rest of the char like * or & it'll terminate translating to integer and show error message.
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
#include <iostream>
using namespace std;

int getInt(char);
bool isValidChar(char);
int main()
{
	char a;
	char b;
	do
	{
	cout << "Enter a phone symbols: " ;
    while(cin.get(a))
	{
   		if(a == '\n') break; // stop processing on space or return
  		int phoneDigit = getInt(a);
	}
	getInt(a);
	cout << "" << endl;
//	  bool isValidChar(a);
	cout << "Continue (Y/y): ";
	cin.get(b);
	}while(b =='Y' || b=='y');
}

int getInt(char a)
{
	
	switch(a)
	{
		case '1' : cout << 1;
		case 'a' : 
		case 'A' :
		case 'b' :
		case 'B' : 
		case 'c' :
		case 'C' : 
		case '2' : cout << 2;
				   break;
		case 'd' :
		case 'D' : 
		case 'e' :
		case 'E' :
		case 'f' :
		case 'F' : 
		case '3' : cout << 3;
				   break;
		case 'g' :
		case 'G' : 
		case 'h' :
		case 'H' :
		case 'i' :
		case 'I' : 
		case '4' : cout << 4;
				   break;
		case 'j' :
		case 'J' : 
		case 'k' :
		case 'K' :
		case 'l' :
		case 'L' : 
		case '5' : cout << 5;
				   break;
		case 'm' :
		case 'M' : 
		case 'n' :
		case 'N' :
		case 'o' :
		case 'O' : 
		case '6' : cout << 6;
				   break;
		case 'p' :
		case 'P' : 
		case 'q' :
		case 'Q' :
		case 'r' :
		case 'R' : 
		case 's' :
		case 'S' : 
		case '7' : cout << 7;
				   break;
		case 't' :
		case 'T' : 
		case 'u' :
		case 'U' :
		case 'v' :
		case 'V' : 
		case '8' : cout << 8; 
				   break;
		case 'w' :
		case 'W' : 
		case 'x' :
		case 'X' :
		case 'y' :
		case 'Y' :
		case 'z' :
		case 'Z' :
		case '9' : cout << 9;
				   break; 
		case ' ' : cout << '-';
				   break;
		case '0' : cout << 0;
				   break;
	
	}
}

bool isValidChar(char a)
{
	if(a<'A' || a>'z' )
}
Last edited on
so valid characters are a-z, A-Z, 0-9, and space (becomes a dash?) if that's it you'll maybe want something like:

1
2
3
4
5
#include <cctype>
bool isValidChar(char a)
{
  return (isAlphaNum(a) || a == ' '); // will return true for alphanumeric or space
}
i've changed the code into this and i can convert all the alphabet but i cannot stop reading input and cannot configure the continue part.
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
int main()
{
	char a;
	char b;
	do
	{
	cout << "Enter a phone symbols: " ;
	cin.get(a);
    while(cin.get(a))
	{
   		if(isValidChar(a) && a!='\n') // stop processing on space or return
  		int phoneDigit = getInt(a);
		else
		cout << "Invalid char " << a << " found - rejected" << endl;
	}
	
	getInt(a);
	cout << "" << endl;
	cout << "Continue (Y/y): ";
	cin.get(b);
	}while(b =='Y' || b=='y');
}

bool isValidChar(char a)
{
  return ((a>='A' && a<='Z') || (a>='a' && a<='z') || (a>='0' && a<='9') || a == ' '); // will return true for alphanumeric or space
}

Last edited on
when the user hits enter isValidChar() will return false. All you are doing is either accepting the character or printing out an error message. You need to break; if you want to kick out of the while loop on the user pressing return.
Topic archived. No new replies allowed.