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

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




User Input

 
Reply to this topicStart new topic

User Input

noob2007
12 Apr, 2007 - 12:02 AM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
the code below works fine but there is a few problems
it displays:
please enter the character you want:
The character you have entered is translated into a '' but you also have decided to exit the program

THis is not suppose 2 happen. it SUPPOSe to ask you to enter the space character then it displays teh converted character. then if teh user enter ' . 'then it will display the message and you will exit
this is my code:

CODE
case 2: //character 'space' condition
cout << "\n Note: Enter '.'(i.e. dot, full stop or decimal point) to exit\n\n";

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 << "'" << "but you also have decided";
cout << " to exit the program...\n\n";
}

}
break;


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

noob2007
RE: User Input
12 Apr, 2007 - 02:27 AM
Post #2

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 38


My Contributions
updated code: but now it display twice this msg:

Please enter a character:

how do i make it tat only display once

o yea i 4got to ask this too, is ther a way tat allow the user to enter as many spaces as possible?


CODE
case 2:    //character 'space' condition
                    cout << "\n    Note: Enter '.'(i.e. dot, full stop or decimal point) to exit\n\n";  
    
                    while (ch != '.')
    
                    {  
                         cout << "Please enter a character: ";
                         ch = cin.get();
                            
                       switch (ch)        
                       {                
                              case ' ':
                                   cout << "\nThe character you have entered is translated into a '_' (underscore)"<< endl << "\n";
                              break;
                              case '.':
                                   cout << "\nThe character you have entered is translated into a " << "'" << ch << "'" << "but you also have decided";
                                   cout << "to exit the program...\n\n";
                                  
                                  break;  
                       }
                      
                    }
               break;


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

Amadeus
RE: User Input
12 Apr, 2007 - 04:21 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
cin.get(), when no size parameters are provided, takes the first character from the input stream. that is not necessarily the character entered by the user. more often than not, there is a newline character left in the buffer from previous uses of cin.get(). you can either flush the buffer before asking for input, or ignore the first character in the buffer, like so:
CODE

cin.ignore();
ch=cin.get();

As for your second question, the user can enter whatever they wish...as long as you have the correct variable type to hold it. Can you describe what you wish to accomplish by having them enter spaces?
User is offlineProfile CardPM
+Quote Post

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

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