While Loop Switch help

Fellows! In my while loop, I want the user to type in like 4 alphabets rather then one. How possibly can I do that?

for example, I want it to do this

Enter a Letter: aeda

I don't want the program to do this

Enter a letter: A

and then it exits the loop

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
int _tmain(int argc, _TCHAR* argv[])
{
	char letter;

//Tell the user about the program
	cout<<"\n\tThis program will translate the alphabets into numbers. Please press\n \"@\" if no alphabets are to be entered. ";

//Tell the user to enter in a letter
	cout<<"\n\t\tEnter a letter";
	cin>>letter;
//make a while loop so it keeps repeating
	
	while (letter!='#')
	{
		cout<<"\n\n\tThe letter you entered is: " << letter;
		cout<<"\n\n\tThe corresponding digit is: ";
			if(letter>='a' && letter<='z')
				switch(letter)
			{
					case 'a':
					case 'b':
					case 'c':
						cout<<1<<endl;
						break;
					case 'd':
					case 'e':
					case 'f':
						cout<<2;
						break;
					case 'g':
					case 'h':
					case 'i':
						cout<<3;
						break;
					case 'j':
					case 'k':
					case 'l':
						cout<<4;
						
					
									}
			else 
				{cout<<"\n\n\tINVALID INPUT!";
			cout<<"please type another entry";}
				cin>>letter;
	}				
			
	cout<<"AMERICA";
Nevermind, got that solved. Suppose I want the user to enter in only 4 alphabets. How can I so that? Only a beginner, so please give me a basic information that I can grasp on :)
A hint: the user has to enter a letter once, but the variable letter does not change during your while loop. So you have to overthink this construct again.
well I still can enter in abdwerad and get an outputted number, but lets say I only want four letters. I'm assuming I use if and else statement then?
closed account (o3hC5Di1)
Hi there,

I'm not sure what you're trying to achieve here?

Part of the problem is that you ask for input once and store the input into a char. A char only contains one character, so if you enter more than one character, it will only store the first character.

You could accept the user input as a string and iterate its characters, or you could include the question within your while loop:

1
2
3
4
5
6
7
8
9
10
//make a while loop so it keeps repeating
	
	while (letter!='#')
	{

//Tell the user to enter in a letter
	cout<<"\n\t\tEnter a letter";
	cin>>letter;

         //..... 


Perhaps if you clarify what your program is supposed to do we could be of more help.

All the best,
NwN
Apologize for my clarity. This is what I want my program to do.

I want it t enter in a total of 4 characters that will output the result in a number form

For instance I type in ABCD. It will output 1111.

I want the user to be only restricted to enter in 4 characters, instread of 5 or more. Not sure if switch was a good selection to use here though :/
Also ignore my first question on this post. That might have threw you off :)
Last edited on
Anyone?
Ok, so improvised on my code and fortunately got the user to only enter 4 or less characters. My problem now is that during the 4th counter, I want the "enter in letter" not to show on the console. Here are some notes on how my data is being displayed.
__________________________________________
Enter in a letter: A
Corresponding Digit: 1

Enter in a letter: E
Corresponding Digit: 2

Enter in a letter: A
Corresponding Digit: 1

Enter in a letter: A
Coressponding Digit: 1

Enter in a letter: A

AMERICA!

___________________________________

In the program, I have prompted the user to enter a character up to 4 times. When I have entered in my fourth letter, the console is displaying enter in a letter, which I do not want to happen. I only want this date below here.

_______________________________

Enter in a letter: A
Corresponding Digit: 1

Enter in a letter: E
Corresponding Digit: 2

Enter in a letter: A
Corresponding Digit: 1

Enter in a letter: A
Coressponding Digit: 1

AMERICA




#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char letter;
	int counter=1;

//Tell the user about the program
	cout<<"\n\tThis program will translate the alphabets into numbers. Please press\n \"@\" if no alphabets are to be entered. ";

//Tell the user to enter in a letter
	cout<<"\n\t\tEnter a letter: ";
	cin>>letter;
//make a while loop so it keeps repeating
	

	while (letter!='#' && counter<=4)
	{
		
			cout<<"\n\n\tThe corresponding digit is: ";
			if(letter>='a' && letter<='z')
				switch(letter)
			{
					case 'a':
					case 'b':
					case 'c':
						cout<<1<<endl;
						break;
					case 'd':
					case 'e':
					case 'f':
						cout<<2;
						break;
					case 'g':
					case 'h':
					case 'i':
						cout<<3;
						break;
					case 'j':
					case 'k':
					case 'l':
						cout<<4;
						
					
									}
			else 
				{cout<<"\n\n\tINVALID INPUT!";
			cout<<"please type another entry";}

			cout<<"\n\t\tEnter a letter: ";
				cin>>letter;
				counter ++;
	}				
			
	cout<<"AMERICA";
	_getch();
	return 0;
}


closed account (o3hC5Di1)
Hi there,

This happens because you first ask a letter before the while loop, then, within the while loop (which runs 4 times) you ask again. Again, this is easily solved by just putting the quesiton for a letter at the top of the while loop:

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
#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	char letter;
	int counter=1;

//Tell the user about the program
	cout<<"\n\tThis program will translate the alphabets into numbers. Please press\n \"@\" if no alphabets are to be entered. ";

//Tell the user to enter in a letter   //remove this here
	cout<<"\n\t\tEnter a letter: ";
	cin>>letter;
//make a while loop so it keeps repeating
	

	while (letter!='#' && counter<=4)
	{
			cout<<"\n\t\tEnter a letter: ";  //move this here
				cin>>letter;
		
			cout<<"\n\n\tThe corresponding digit is: ";
			if(letter>='a' && letter<='z')
				switch(letter)
			{
					case 'a':
					case 'b':
					case 'c':
						cout<<1<<endl;
						break;
					case 'd':
					case 'e':
					case 'f':
						cout<<2;
						break;
					case 'g':
					case 'h':
					case 'i':
						cout<<3;
						break;
					case 'j':
					case 'k':
					case 'l':
						cout<<4;
						
					
									}
			else 
				{cout<<"\n\n\tINVALID INPUT!";
			cout<<"please type another entry";}


		        counter ++;
	}				
			
	cout<<"AMERICA";
	_getch();
	return 0;
}



If you need for all characters to be entered at once you could simply use a std::string, or char-array and iterate over its individual characters:

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/operator%5B%5D/

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.