8 Replies - 307 Views - Last Post: 05 October 2012 - 03:33 PM Rate Topic: -----

#1 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

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!

#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;
	}







Is This A Good Question/Topic? 0
  • +

Replies To: I am not able to get characters in one line

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

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...
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

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:

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.
Was This Post Helpful? 0
  • +
  • -

#4 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

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:
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
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

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()).
Was This Post Helpful? 0
  • +
  • -

#6 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: I am not able to get characters in one line

Posted 05 October 2012 - 02:36 PM

Man! You have a single char:
char letters;


It cannot hold up 3 chars. You need to have a storage for 3 if you want to store ABC.
Was This Post Helpful? 0
  • +
  • -

#7 jaynajay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-September 12

Re: I am not able to get characters in one line

Posted 05 October 2012 - 02:52 PM

AKMafia:

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);

Was This Post Helpful? 0
  • +
  • -

#8 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

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:
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

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

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:
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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1