Hello.
im a student working in C++ and im fairly new to both C++ and i guess this community.(i was reccomended to come here if i had problems) im just doing some simple exercises or s it seemed. here is the question.
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.

simple? well i guess, i was able to do most of it but im stuck on Q2 and Q4. i cant find a way to enter spacebar through the case(ive tested other characters to give out the underscore and i have tried some 'getline' stuff) and i am lost upon the last bit. I'm sure it ustilises the 'default' part of the code. bleh maybe i explained incorrectly.
btw im not a slacker asking for easy answers heres what ive done

#include<iostream>
using namespace std;
int main(){
    string type2;
    char type;
    char c;
    cout<<"please enter one of the valid characters  ";
    cin>>type;
    switch(type)
    {case 'X':
          type2="x";
          break;
    case 'Y':
         type2="y";
         break;
    case 'Z':
         type2="z";
         break;
    case '0': case '1': case '2': case '3': case '4': case'5': case'6': case'7': case'8': case '9':
         type2="?";
         break;

default :
        type2=
 ;
        }
cout<<type2<<endl;
system("pause");
return 0;
}

anyways anyhelp is much appreciated. telling me what im supposed to do or simply pointing me in correct direction would be great.(my textbooks fail at this) and yes i have watched through several 'youtube' style tuts and googled lots but to no sucess.

thnx, Leone, novice c++ student.

Recommended Answers

All 7 Replies

When using a standard input stream, the extraction operator (operator>>) considers whitespace (which a space is) to be the end of the input. This is why you can't get it to read the space character. Do some research on the istream::get() function.

checking both out immediatly thanks.
any ideas on

iv. all other characters remain unchanged.

?

checking both out immediatly thanks.
any ideas on

iv. all other characters remain unchanged.

?

That sounds like a default case to me...

Both of my posts are essentially the same. Something strange happened with the first version.

nvm i figured it out :D

basically i set
default :
type2=type;

woot 3/4 done.

i'm still reading through your links though, was playign around with default and stumbled upon the idea of it really.
looks like i should be done tonight :)

hmmm well done, thread solved thnx

0.o after thinking in it all and solved it

found that's u solved the thread

well done....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.