my loop isn't working properly

my program is to convert input char to phone no and will ask whether want to continue or not after conversion. I am managed to do conversion but my loop for continue or not is not working properly. For first time it worked but after first time it'll straight away go to continue and it won't let me input the data again.
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
#include <iostream>
using namespace std;

char getNumber(char);
bool isValidChar(char);
int main()
{	

		char a; 
		char b;
	do
	{
		cout << "Enter a phone symbols: ";
		cin.get(a);
			while(a != '\n' && isValidChar(a))
			{
				cout << getNumber(a);
				cin.get(a);	   	   

			}
			if(a!='\n' && !isValidChar(a))
			{
				cout << "wrong input";	  
			}
	cout << "" << endl;
	cout << "Continue (Y/y): ";
	cin.get(b);
	}while(b == 'y' || b == 'Y');
}

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

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
}

Use cin>> instead of cin.get()
Last edited on
ok now i can solve it for correct input part but if i key in wrong input it'll stop the program straight away instead of asking me whether i want to continue or not.
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
int main()
{	

		char a; 
		char b;
	do
	{
		cout << "Enter a phone symbols: ";
		cin >> a;
			while(a != '\n')
			{
				if(isValidChar(a))
				{
					cout << getNumber(a);
					cin.get(a);
				}
				else
				{
					cout << "wrong input";
					a = '\n';
				}	 
			}
	cout << "" << endl;
	cout << "Continue (Y/y): ";
	cin.get(b);
	}while(b == 'y' || b == 'Y');
}
Last edited on
You need to add cin.ignore(80, '\n') after your last cin.get statements. This will discard anything left in the buffer before checking if you want to continue.
cin.get() get input from input stream and not from user.So use cin.ignore() before it or flush the stream.
closed account (4izT0pDG)
Couldn't you just change all the cin.get() to cin and do cout<<getNumber(a)<<endl; ?
Last edited on
@ocasio101 cannot. if i change all to cin instead of cin.get() it won't capture white space which needs to convert to -. now i think it's something wrong with my coding in checking of wrong input part. because everything i key in anything other than a-z,A-Z and - it'll straight away terminate the program. I can still see the continue statement but it won't let me key in whether i want to continue or not.
Last edited on
ofcourse you can get that.
White space works like end of word but like you can compare a to \n,same way you can compare it to space.Space is (char)32 .
for now everything is working except i key in the wrong input it will straight away stop the do while loop too. can any1 give me suggestion?
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
int main()
{	

		char a; 
		char b;
	do
	{
		cout << "Enter a phone symbols: ";
		cin >> a;
			while(a != '\n')
			{
				if(isValidChar(a))
				{
					cout << getNumber(a);
					cin.get(a);
				}
				else
				{
					cout << "wrong input";
					a = '\n';
				}
			}	 
	cout << "" << endl;
	cout << "Continue (Y/y): ";
	cin >> b;
	}while(b == 'y' || b == 'Y');
}
nvm i managed to solve it. Anyway thank you guys
Topic archived. No new replies allowed.