#include <iostream>
using namespace std;
int main ()
{
char letters;
char ch = '#';
do
{
cout<< "Program to convert letters to their corresponding telephone digits." <<endl;
cout<< "To stop the program enter #." <<endl;
cout<< "Enter letters: ";
cin>>letters;
cout<<endl;
if( letters>='A' && letters<= 'Z' || letters>='a' && letters<='z')
{
cout<< "The letter you entered is: " ;
cout<<letters <<endl;
cout<< "The corresponding telephone digit is: ";
}
else if (!letters)
{
cout<<"Invalid" <<endl;
}
switch (letters)
{
case 'A':
case 'B':
case 'C':
case 'a':
case 'b':
case 'c':
cout<< '2' <<"\n\n";
break;
case 'D':
case 'E':
case 'F':
case 'd':
case 'e':
case 'f':
cout<< '3' <<"\n\n";
break;
case 'G':
case 'H':
case 'I':
case 'g':
case 'h':
case 'i':
cout<< '4' <<"\n\n";
break;
case 'J':
case 'K':
case 'L':
case 'j':
case 'k':
case 'l':
cout<< '5' <<"\n\n";
break;
case 'M':
case 'N':
case 'O':
case 'm':
case 'n':
case 'o':
cout<< '6' <<"\n\n";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
case 'p':
case 'q':
case 'r':
case 's':
cout<< '7' <<"\n\n";
break;
case 'T':
case 'U':
case 'V':
case 't':
case 'u':
case 'v':
cout<< '8' <<"\n\n";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
case 'w':
case 'x':
case 'y':
case 'z':
cout<< '9' <<"\n\n";
}
}
while(letters!='#');
return 0;
}
I am not able to get characters in one line
Page 1 of 18 Replies - 181 Views - Last Post: 05 October 2012 - 03:33 PM
#1
I am not able to get characters in one line
Posted 05 October 2012 - 01:50 PM
I have tried to use string, getchar, get, getline. Nothing seems to work if i want all the characters entered by the user to appear in one line. Here is my code, and Thanks Much in advance for the help!
Replies To: I am not able to get characters in one line
#2
Re: I am not able to get characters in one line
Posted 05 October 2012 - 02:03 PM
If you want to output on the same line then you have to remove the \n escape character and the endl manipulator from your code...
#3
Re: I am not able to get characters in one line
Posted 05 October 2012 - 02:05 PM
You should make the logic of this line clearer by using more parentheses:
or better, change the letter to upper or lower case so that only one check and fewer case statements are necessary.
If you want to read it all in one line, then you should use getline() to read it all into a character array and then iterate through that to pull out the letters.
if( letters>='A' && letters<= 'Z' || letters>='a' && letters<='z')
or better, change the letter to upper or lower case so that only one check and fewer case statements are necessary.
If you want to read it all in one line, then you should use getline() to read it all into a character array and then iterate through that to pull out the letters.
#4
Re: I am not able to get characters in one line
Posted 05 October 2012 - 02:23 PM
I am not sure if I made myself clear earlier what I wanted as an output. When I execute the program, it gives me the prompt:
Enter letters: ABC
After the input, it gives me output as follows:
The letter you entered is: a
The corresponding telephone digit is: 2
It repeats these statement 3 times because I entered 3 characters.
I want the program output to be as follows:
Enter letters: ABC
The letter you entered is: ABC
The corresponding telephone digit is: 222
cout<< "Program to convert letters to their corresponding telephone digits." <<endl; cout<< "To stop the program enter #." <<endl; cout<< "Enter letters: ";
Enter letters: ABC
After the input, it gives me output as follows:
The letter you entered is: a
The corresponding telephone digit is: 2
It repeats these statement 3 times because I entered 3 characters.
I want the program output to be as follows:
Enter letters: ABC
The letter you entered is: ABC
The corresponding telephone digit is: 222
#5
Re: I am not able to get characters in one line
Posted 05 October 2012 - 02:33 PM
If that is your desired output, then take your prompts and initial reporting of the user's input out of the do-while loop. As mentioned above, you'll need to get the entire input as a string (probably using getline()).
#6
Re: I am not able to get characters in one line
Posted 05 October 2012 - 02:36 PM
Man! You have a single char:
It cannot hold up 3 chars. You need to have a storage for 3 if you want to store ABC.
char letters;
It cannot hold up 3 chars. You need to have a storage for 3 if you want to store ABC.
#7
Re: I am not able to get characters in one line
Posted 05 October 2012 - 02:52 PM
AKMafia:
Is this correct,
now, i get the error that switch expression of type 'char [7]' is illegal
Skydiver:
I took outta loop, but no help, and is this correct for getline?
Is this correct,
char letters[7];
now, i get the error that switch expression of type 'char [7]' is illegal
Skydiver:
I took outta loop, but no help, and is this correct for getline?
cin.getline(letters, 7);
#8
Re: I am not able to get characters in one line
Posted 05 October 2012 - 03:18 PM
Yes but, not the best solution. Because you don't know in advance that how many letters the user will enter, if the user enters too many, you will end up losing some of them...
As of the error, you have to provide an expression that would evaluate to a single letter (as for your case) so the switch would match it to any case.
Assuming you need only 3 letters:
As far for other statements, like if and the switch, you have to specify the index of the letter.
e.g.
Assuming i is the loop counter which goes from 0 to 2.
Hope this Helps!
As of the error, you have to provide an expression that would evaluate to a single letter (as for your case) so the switch would match it to any case.
Assuming you need only 3 letters:
cout << "Enter letters:"; char letters[3]; cin >> letters;
As far for other statements, like if and the switch, you have to specify the index of the letter.
e.g.
if(letters[i] >= 'A' && letters[i] <= 'Z') & switch(letters[i])
Assuming i is the loop counter which goes from 0 to 2.
Hope this Helps!
This post has been edited by AKMafia001: 05 October 2012 - 03:20 PM
#9
Re: I am not able to get characters in one line
Posted 05 October 2012 - 03:33 PM
Reading the manual usually helps: http://en.cppreferen..._string/getline
Use the built in std::string class (unless you are using Turbo C++, in which case, you may be in for a bigger headache). That way you can do something like:
In your shoes, I would write my code following this pseudo code:
Use the built in std::string class (unless you are using Turbo C++, in which case, you may be in for a bigger headache). That way you can do something like:
string letters; getline(cin, letters);
In your shoes, I would write my code following this pseudo code:
bool WannaQuit()
{
char ch;
do
{
cout >> "Do you want to quit? ";
string input;
cin >> input;
ch = toupper(input[0]);
} while (!(ch == 'Y' || ch == 'N'));
}
string GetLetters()
{
cout << "Enter the letters: ";
string letters;
cin >> letters;
return letters;
}
string ConvertToDigits(const string & letters)
{
string digits;
for(int i = 0; i < letters.size(); i++)
{
char mappedNumber = '?';
// map letters[i] to a phone digit and append to digits
digits += mappedNumber;
}
return digits;
}
int main()
{
do
{
string letters = GetLetters();
string digits = ConvertToDigits(letters);
cout << "The letters you entered were: " << letters << endl;
cout << " These map to phone digits: " << digits << endl;
} while (!WannaQuit());
return 0;
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|