Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,067 people online right now. Registration is fast and FREE... Join Now!




character conversion

 
Reply to this topicStart new topic

character conversion, i need help to resolve a small problem

ezymann
12 Apr, 2007 - 08:43 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 2


My Contributions
write YOUR PROGRAMS IN A PROPER AND CONSISTENT CODING STYLE.
Write a C++ program with a case structure. The program reads in a character from
the user, then translates it into a different character according to the following rules:
i. characters X, Y, Z will be translated to characters x, y, z respectively;
ii. the space character ' ' will be translated to underscore '_'
iii. digits 0, 1, 2, .., 9 will all be translated to the question mark '?'
iv. all other characters remain unchanged.
The program then displays the translated character.

here is my program:

#include <cstdlib>
#include <iostream>

using namespace std;

int main() {

char ch, output;
cout << "Enter A Character:";
ch = cin.get();

switch(ch)
{
case 'X':
output = 'x';
break;

case 'Y':
output = 'y';
break;

case 'Z':
output = 'z';
break;

case ' ':
output = '_';
break;

case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
output = '?';
break;

default:
output = ch;
}

cout << "The translated character is: " << output << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}

now i need to make sure when i enter a number apart from 0-9, eg 99, it will display that number and not a '?'.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Character Conversion
12 Apr, 2007 - 08:54 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You are only taking one character as input...do you want to be able to take more?
User is offlineProfile CardPM
+Quote Post

ezymann
RE: Character Conversion
12 Apr, 2007 - 08:59 AM
Post #3

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 2


My Contributions
QUOTE(Amadeus @ 12 Apr, 2007 - 09:54 AM) *

You are only taking one character as input...do you want to be able to take more?


yes, because i want to be able to enter for example 74, and get it to display 74. tnz for replying.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Character Conversion
12 Apr, 2007 - 09:49 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well I went and got all excited about finite automata... then I realized that your program had some problems being made into one.

The problem is that you output for each char as it is entered. THis does not allow you any chance to see what the next character is. Now if you didn't mind you program runing so that the first digit (say 7) output a '?' and then the next digit (say 4) outputing "74" -- so the total output given the input string "74" would be "?74" -- not ideal.

Another solution would be to pospone your output until you had the next input. This would look really strange with the prompts.

Another solultion would be to read in a line at a time, and then translate the line all at once. This way you could peek ahead and if the current char is a digit, and the next char is a digit (or the last char was a digit) then I know to output the digit rather than a '?'.

I am sure there are other methods.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:48PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month