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

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




help with SWITCH and IF statements

 
Reply to this topicStart new topic

help with SWITCH and IF statements, problems...im a beginner

noob2007
10 Apr, 2007 - 03:17 PM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
the quesiton is
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.

BUT my problems are:
1. is that when you enter upper case Y or Z it stil converts to lower case 'x'
2. when you enter another characters besides capital X or Y or Z it still converts to a lower case x, y, z(i tried putting a not else if statement but doesnt work) AND this problems occurs also for option 2 below in the code
3. for the space character, it automatically converts itself without the user input but i want the user to enter it in
4. in the only number conversion, when you enter not numbers it will still convert :S

help please i have no idea wat to do now sad.gif im a beginner programmer

CODE
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{

    char ch;
    int opselect;
    
     cout << "Please select a number: ";
     cout << "\n1 for characters X, Y, Z will be translated to characters x, y, z respectively\n";
     cout << "\n2 for the space character ' ' will be translated to underscore '_'\n";
     cout << "\n3 for digits 0, 1, 2, .., 9 will all be translated to the question mark '?'\n";
     cout << "\n4 for all other characters\n";  
     cin >> opselect;
     cout << "\nYou have selected option: " << opselect << endl;
            
    switch (opselect)
    {
           case 1:
                cout << "Please enter the character you want: ";
                cin >> ch;
           if (ch = 'X')
                cout << "\nThe character you have entered is translated into x" << endl << "\n";
                
            else if (ch = 'Y')
                cout << "\nThe character you have entered is translated into y" << endl << "\n";
                
            else if (ch = 'Z')
                cout << "\nThe character you have entered is translated into z" << endl << "\n";    

            break;
                
            case 2:  //character 'space' condition
                 cout << "Please enter the character you want: ";
                 ch = cin.get();      
                
            if (ch = ' ')
                cout << "\nThe character you have entered is translated into a '_' (underscore)"<< endl << "\n";
                break;
            
            case 3:  //numbers  
                  cout << "Please enter the character you want: ";
                  cin >> ch;            
            if (ch = '1')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";  
            else if (ch = '2')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";  
            else if (ch = '3')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '4')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '5')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '6')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '7')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '8')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '9')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
            else if (ch = '0')
            cout << "\nThe character you have entered is translated into a '?' (quesiton mark)" << endl << "\n";
                        else if (ch = 'X')
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;
            else if (ch = 'Y')
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;
            else if (ch = 'Z')
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;
                break;  
    
                 case 4:
                 cout << "Please enter the character you want: ";
                 cin >> ch;  
                 cout << "\nThe character you have entered is translated into a " << ch << endl << "\n";
                   break;                
            
     }
system("pause");
return 0;
}


This post has been edited by noob2007: 10 Apr, 2007 - 03:29 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Help With SWITCH And IF Statements
10 Apr, 2007 - 03:34 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
first off, in an if statement comparison is == a single equals is an assignment, ex:
n = 5; //n is the value 5
n == 5; //does n equal 5? returns a boolean value

all your values become 'x' because you have assigned the value, instead of comparing it.

CODE

else if (ch = 'X')
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;

this is wrong: needs {} as only 1 line is included in the if statement without them. Also i'm surprised it works at all with this code there, and another else if afterwards.

User is offlineProfile CardPM
+Quote Post

noob2007
RE: Help With SWITCH And IF Statements
10 Apr, 2007 - 03:57 PM
Post #3

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
QUOTE(William_Wilson @ 10 Apr, 2007 - 04:34 PM) *

first off, in an if statement comparison is == a single equals is an assignment, ex:
n = 5; //n is the value 5
n == 5; //does n equal 5? returns a boolean value

all your values become 'x' because you have assigned the value, instead of comparing it.

CODE

else if (ch = 'X')
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;

this is wrong: needs {} as only 1 line is included in the if statement without them. Also i'm surprised it works at all with this code there, and another else if afterwards.



im confused. i tried tat == but still doesnt work. now the program doesnt open givs error
CODE
            else if (ch == 'X')
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;


This post has been edited by noob2007: 10 Apr, 2007 - 03:59 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Help With SWITCH And IF Statements
10 Apr, 2007 - 04:01 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
all of your if statments and else if need to be ==, the code i posted, should be like:
CODE

else if (ch == 'X'){
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;
}

just because it didn't give errors does not make it correct.
User is offlineProfile CardPM
+Quote Post

noob2007
RE: Help With SWITCH And IF Statements
10 Apr, 2007 - 04:23 PM
Post #5

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
QUOTE(William_Wilson @ 10 Apr, 2007 - 05:01 PM) *

all of your if statments and else if need to be ==, the code i posted, should be like:
CODE

else if (ch == 'X'){
            cout << "Invalid character. Please re-enter: ";
            cin >> ch;
}

just because it didn't give errors does not make it correct.


yea i did tat now the program runs but doesnt giv the right output. it doesnt display its a invalid input

else if (ch == 'X'){
cout << "Invalid character. Please re-enter: ";
cin >> ch;
}
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Help With SWITCH And IF Statements
10 Apr, 2007 - 06:27 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Can you please post the updated code?
User is offlineProfile CardPM
+Quote Post

noob2007
RE: Help With SWITCH And IF Statements
11 Apr, 2007 - 01:49 AM
Post #7

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
my problem is tat :

wen i enter 2 to select the number 2 option, it displays :

Please enter the character you want:
The character you have entered is translated into a


Please enter the character you want:

BUT it is suppose to display:

'Please enter the character you want: '

ONLY because now this is where user input comes in to play

THEN the user enter the space character THEN it will display:

'The character you have entered is translated into a '_''

CODE
            case 2:    //character 'space' condition
          
                while (ch != '.')
                {  
                  
                   cout << "Please enter the character you want: ";
                   ch = cin.get();      
                   switch (ch)        
                   {
                          case ' ':
                               cout << "\nThe character you have entered is translated into a '_' (underscore)"<< endl << "\n";
                          break;
                  
                          default:
                                  cout << "\nThe character you have entered is translated into a " << ch << endl << "\n";
                   }
                }
           break;



This post has been edited by noob2007: 11 Apr, 2007 - 02:09 AM
User is offlineProfile CardPM
+Quote Post

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

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