Problems displaying proper output

Page 1 of 1

5 Replies - 471 Views - Last Post: 23 February 2012 - 08:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=267856&amp;s=bda8dcb7822e87ce6c675196d2bdecf5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Shalzaon

Reputation: 2
• Posts: 27
• Joined: 21-January 09

Problems displaying proper output

Posted 23 February 2012 - 01:14 AM

```#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
int output;
char input;

cout << "Hello, today we will translate a letter from a phone key pad into \n"
"into a corresponding digit " <<endl;
cout << "Please enter a letter you would like translated: ";
cin  >> input;

if (input == 'a', 'b', 'c')
{
input=toupper(input);
output = 2;
}

if (input == 'd', 'e', 'f')
{
input=toupper(input);
output = 3;
}

if (input == 'g', 'h', 'i')
{
input=toupper(input);
output = 4;
}

if (input == 'j', 'k', 'l')
{
input=toupper(input);
output = 5;
}

if (input == 'm', 'n', 'o')
{
input=toupper(input);
output = 6;
}

if (input == 'p', 'q', 'r', 's')
{
input=toupper(input);
output = 7;
}

if (input == 't', 'u', 'v')
{
input=toupper(input);
output = 8;
}

if (input == 'w', 'x', 'y', 'z')
{
input=toupper(input);
output = 9;
}

else
{
cout << "Sorry incorrect input \n";
}

cout << "The letter " << input<< " Corresponds to the digit: " <<output <<endl;

return 0;
}
```

For this assignment my objective was to take in a char data type and display its corresponding telephone key pad digit. When ever compiled my out always shows up as follows :

```/*Hello, today we will translate a letter from a phone key pad into
into a corresponding digit
Please enter a letter you would like translated: a
The letter A Corresponds to the digit: 9
Press any key to continue . . .
*/
```

I have tried reassigning my conditional statements to have the =, >=, and <= operators but with no luck on getting the proper output to display. Any suggestions as to what I could do to make my output not read as 9 regardless of what I input? Any help would be greatly appreciated!

Is This A Good Question/Topic? 0

Replies To: Problems displaying proper output

#2 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

Re: Problems displaying proper output

Posted 23 February 2012 - 01:17 AM

Since your values are 9, we can tell that you are returning true for each if conditional test, simply because 9 is the last value assigned.

Your conditional if statements are not valid syntax. I've never seen any one try to use commas in the statement. You want to research AND / OR in your if conditional statement.

#3 shurd

Reputation: 37
• Posts: 162
• Joined: 05-February 12

Re: Problems displaying proper output

Posted 23 February 2012 - 03:47 AM

```if (input == 'a', 'b', 'c')
```

You can't use commas, you need to specify every condition
```if (input == 'a'&& input== 'b' &&  input == 'c')
```

or
```if (input >= 'a' && input<='c')
```

#4 Shalzaon

Reputation: 2
• Posts: 27
• Joined: 21-January 09

Re: Problems displaying proper output

Posted 23 February 2012 - 01:00 PM

Thank you guys so much. Turns out i had tried to use to OR operators before but wrote it

```if (input == 'a' || 'b' || 'c')
```

And that didn't work, however changing all my conditional statements to read as follows gave me the correct output i was looking for

```if (input == 'a' || input == 'b' || input =='c')
```

Again thank you guys so much for the help =D

This post has been edited by Shalzaon: 23 February 2012 - 01:01 PM

#5 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

Re: Problems displaying proper output

Posted 23 February 2012 - 06:20 PM

#6 raspinudo

Reputation: 61
• Posts: 232
• Joined: 19-September 11

Re: Problems displaying proper output

Posted 23 February 2012 - 08:29 PM

Another thing you may want to look into would be a switch statement. They are very useful in these types of problems.