5 Replies - 471 Views - Last Post: 23 February 2012 - 08:29 PM Rate Topic: -----

#1 Shalzaon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • 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  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6627
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • 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')

Was This Post Helpful? 1
  • +
  • -

#4 Shalzaon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

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

Re: Problems displaying proper output

Posted 23 February 2012 - 06:20 PM

Glad you got it working!
Was This Post Helpful? 0
  • +
  • -

#6 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • 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.

link: http://www.cprogramm...al/lesson5.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1