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.

Dani AI

Generated

A few concise, practical tips to make the program robust and portable.

was right: operator>> skips whitespace, so use std::cin.get() or read a whole line with std::getline and inspect its first character if you need to accept a space. hit the right idea for (iv) — make the default output equal to the input so "all other characters remain unchanged." Avoid using a std::string just to hold one character; a char for the output is simpler and more portable.

Example (compact and safe):

#include <iostream>
#include <cctype>
#include <limits>

int main() {
    char in;
    std::cout << "Enter a character: ";
    if (!std::cin.get(in)) return 0; // handle EOF/error

    char out = in; // default: unchanged
    if (in == 'X') out = 'x';
    else if (in == 'Y') out = 'y';
    else if (in == 'Z') out = 'z';
    else if (in == ' ') out = '_';
    else if (std::isdigit(static_cast<unsigned char>(in))) out = '?';

    std::cout << out << '\n';
}

Troubleshooting notes: if you mix >> and get() the leftover newline can be read by get() — use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); to flush it. Always cast to unsigned char before std::isdigit/std::tolower to avoid undefined behavior on platforms where char is signed. Avoid system("pause") for portability. Test with inputs like X, space, 5, and a to confirm each rule behaves as expected.

Recommended Answers

All 7 Replies

When you use the standard input streams, the extraction operator (operator>>) ignores whitespace, that's why you can't extract the space character.

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.

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.