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.
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.
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;
}
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:
#include "stdafx.h"
#include <conio.h>
#include <iostream>
usingnamespace 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 herecout<<"\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: