6 Replies - 1648 Views - Last Post: 25 July 2010 - 07:57 AM Rate Topic: -----

#1 Guest_Mike*


Reputation:

Convert letters or words to numbers

Posted 24 July 2010 - 09:10 PM

I have a requirement to create a program that converts letters or words to numbers (like on a telephone) and I have not been able to make this work for two days. I have changed this program and have gone from being able to print out nothing at all; not being able to enter anything; not performing its loop, and not outputting the number for each letter. I am required to use a do...while loop or a while loop. I have chosen the while loop. The words that I must use to test the program are: Toast, Eggs, and Monday. I am getting nowhere. Any assistance would be appreciated. Thanks, Mike

Here is the code:

#include <iostream>
using namespace std;

void main ()     //initially had as int main ()
{
	char cLetter = 'a';  //set character to lowercase value
	cLetter = toupper ( cLetter ); //change lowercase to uppercase

	cout << " Press the EOF character to terminate this program \n" 
	     << " OR \n"
		 << " Enter a letter: " << endl;				

	while ( ( cLetter = cin.get() ) != EOF );
	{
		switch (cLetter) //search for matiching letter
		{
			case 'a':
			case 'b':
			case 'c':
				cout << "2";
				break;

			case 'd':
			case 'e':
			case 'f':
				cout << "3";
				break;

			case 'g':
			case 'h':
			case 'i':
				cout << "4";
				break;

			case 'j':
			case 'k':
			case 'l':
				cout << "5";
				break;

			case 'm':
			case 'n':
			case 'o':
				cout << "6";
				break;

			case 'p':
			case 'q':
			case 'r':
			case 's':
				cout << "7";
				break;

			case 't':
			case 'u':
			case 'v':
				cout << "8";
				break;

			case 'w':
			case 'x':
			case 'y':
			case 'z':
				cout << "9";
				break;

			default:
				cout << "1" << endl; //this was added since there is no letter for 1
		}
	}
}


This post has been edited by no2pencil: 24 July 2010 - 09:11 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0

Replies To: Convert letters or words to numbers

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Convert letters or words to numbers

Posted 24 July 2010 - 09:12 PM

Fix your post so that you use CODE tags. Instructions to that effect are posted all over the place, including at the time of posting. Please follow them.

So you've had problems with your code. What about the code right now? So us show sample input, expected output, and current observed output.

EDIT: It is int main, by the way.

This post has been edited by Oler1s: 24 July 2010 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5410
  • View blog
  • Posts: 27,411
  • Joined: 10-May 07

Re: Convert letters or words to numbers

Posted 24 July 2010 - 09:16 PM

For starters you have a semi-colon on your while command line. This will end the command & not continue the code block.

To follow up, why are you setting the value to upper & then casing for the lowercase value?

The final error that I see is you put your open bracket for the switch on the same line as the command switch. This would be fine except that you have it after your comment about the switch, causing the compiler to fail.
Was This Post Helpful? 0
  • +
  • -

#4 beryllium  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 01-April 10

Re: Convert letters or words to numbers

Posted 24 July 2010 - 09:17 PM

you could make an enumerated type and assign letters to values.

try putting this in your code:

enum letter {a=2,b=2,c=2,
d=3,e=3,f=3,
g=4,h=4,i=4,
j=5,k=5,l=5,
m=6,n=6,o=6,
p=7,q=7,r=7,s=7,
t=8,u=8,v=8,
w=9,x=9,y=9,z=9};

that might clear things up.

hope it helps!
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5410
  • View blog
  • Posts: 27,411
  • Joined: 10-May 07

Re: Convert letters or words to numbers

Posted 24 July 2010 - 09:23 PM

The only thing I added was a case for enter, otherwise each entry ended with one.

#include <iostream>
using namespace std;

int main (void) {
  char cLetter = 'a';  //set character to lowercase value

  cout << " Press the EOF character to terminate this program \n"
  << " OR \n"
  << " Enter a letter: " << endl;

  while ( ( cLetter = cin.get() ) != EOF )  {
    switch (tolower(cLetter)) {
      case 'a':
      case 'b':
      case 'c':
      cout << "2";
      break;

      case 'd':
      case 'e':
      case 'f':
      cout << "3";
      break;

      case 'g':
      case 'h':
      case 'i':
      cout << "4";
      break;

      case 'j':
      case 'k':
      case 'l':
      cout << "5";
      break;

      case 'm':
      case 'n':
      case 'o':
      cout << "6";
      break;

      case 'p':
      case 'q':
      case 'r':
      case 's':
      cout << "7";
      break;

      case 't':
      case 'u':
      case 'v':
      cout << "8";
      break;

      case 'w':
      case 'x':
      case 'y':
      case 'z':
      cout << "9";
      break;

      case '\n':
      break;

      default:
      cout << "1" << endl; //this was added since there is no letter for 1
      break;
    }
  }
}


Gives the following output :

Quote

g++ convert.cpp -o convert
/code/c >$./convert
Press the EOF character to terminate this program
OR
Enter a letter:
abcdefghi
222333444>$


Hopefully this helps you fix your program.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_honumike*


Reputation:

Re: Convert letters or words to numbers

Posted 24 July 2010 - 09:25 PM

#include <iostream>
using namespace std;

void main ()     //initially had as int main ()
{
	char cLetter = 'a';  //set character to lowercase value
	cLetter = toupper ( cLetter ); //change lowercase to uppercase

	cout << " Press the EOF character to terminate this program \n" 
	     << " OR \n"
		 << " Enter a letter: " << endl;				

	while ( ( cLetter = cin.get() ) != EOF );
	{
		switch (cLetter) //search for matiching letter
		{
			case 'a':
			case 'b':
			case 'c':
				cout << "2";
				break;

			case 'd':
			case 'e':
			case 'f':
				cout << "3";
				break;

			case 'g':
			case 'h':
			case 'i':
				cout << "4";
				break;

			case 'j':
			case 'k':
			case 'l':
				cout << "5";
				break;

			case 'm':
			case 'n':
			case 'o':
				cout << "6";
				break;

			case 'p':
			case 'q':
			case 'r':
			case 's':
				cout << "7";
				break;

			case 't':
			case 'u':
			case 'v':
				cout << "8";
				break;

			case 'w':
			case 'x':
			case 'y':
			case 'z':
				cout << "9";
				break;

			default:
				cout << "1" << endl; //this was added since there is no letter for 1
		}
	}
}
/* Here is a little of the current output. I didn't get any results from entering these words and used Ctrl-Z to terminate.
 Press the EOF character to terminate this program
 OR
 Enter a letter:
toast
Toast
TOAST
Run
Monday
^Z
1
Press any key to continue . . .*/



Thanks for the assistance. I am going to make changes right now and see if that works. I really appreciate the assistance and I do not ask anyone to do my homework for me, and I now know how to use
 
for my entries.

Thanks,

Mike
Was This Post Helpful? 0

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Convert letters or words to numbers

Posted 25 July 2010 - 07:57 AM

It NEEDS to be int main(void)...void main is NOT LEGAL C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1