RE: Implimenting prompts in program for reinput from user?

  • From: "Delaunay Christophe" <christophe.delaunay@xxxxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 20 Jul 2010 13:34:02 +0200

Hi Jes,

Here is a sample piece of code which could perform what you requested.

------ BEGINNING OF CODE ------
// --- The needed headers ---
#include <iostream>

// You may add "using namespace std" in order to avoid typing "std::"
// in front of each call to cout, cin or endl. I did not do it.
int main(void) {
        char c = '\0'; // The character to read
        std::cout << "This is a sample." << std::endl;
        do { // The loop itself
                std::cout << "Please enter a command : ";
                std::cin >> c; // reads the command
                switch (c) { // to parse the command
                case '1' :
                        std::cout << "I perform job for command 1." <<
std::endl;
                        break; // I don't want to execute following
cases

                case '2' :
                        std::cout << "I perform job for command 2." <<
std::endl;
                        break; // I don't want to execute following
cases

                case '3' :
                        std::cout << "I perform job for command 3." <<
std::endl;
                        break; // I don't want to execute following
cases

                case 'e' :
                case 'E' : // please understand both cases as 'e' or 'E'
                        std::cout << "I am requested to end." <<
std::endl;
                        break; // I don't want to execute the "default"
statement

                default :
                        std::cout << "Sorry. I did not recognize your
command." << std::endl;
                } // end of switch on character c
        } while ( (c != 'e') && (c != 'E') );

        // at end of loop, I only need to return
        std::cout << "I am finished!" << std::endl;
        return 0;
} // end of main
------ END OF CODE ------

In this piece of code, I made the important assumption that the commands
are always one character long.

And now, here is a sample of what happens when I launch this small piece
of code on my linux box:

------ BEGINNING OF OUTPUT ------
[delaunayc@rennxlxrda013 Test]$ ./LittleTest
This is a sample.
Please enter a command : 1
I perform job for command 1.
Please enter a command : a
Sorry. I did not recognize your command.
Please enter a command : e
I am requested to end.
I am finished!
[delaunayc@rennxlxrda013 Test]$
------ END OF OUTPUT ------

HTH, Have a nice day. Chris D

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
Sent: mardi 20 juillet 2010 12:36
To: programmingblind@xxxxxxxxxxxxx
Subject: Implimenting prompts in program for reinput from user?

Say I want a user to enter a number from 1 to 3, but they enter
something else, like 4, or the letter v, or whatever. How would I tell
the program to display an error message to the user using cout, then
return them to the initial prompt?
Thanks.
Jes
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: