Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 28th, 2005, 04:27 PM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default cin being skipped. ignore() not working


Hello all,

I am just starting in C++ and I have the code below. When I run it and enter anything besides 1,2 or 3 (enter a 'd' for example), I am stuck in an infinite loop displaying the menu and "Invalid Choice, please try again." I tried cin.ignore() in DoMenu(), but it had no effect. I'm thinking my logic is messed up. Whatever the cause, I could *really* use a hint, please. :)

TIA,
UN

Code:
#include <iostream>
#include <limits>
using namespace std;

enum CHOICE {Encrypt = 1, Decrypt, Quit};
char DoMenu(void);


int main(void)
    {
        bool QUIT = false;
        int choice = Encrypt;

        while (!QUIT)
        {
            choice = DoMenu();
            if (choice < Encrypt || choice >  Quit)
                {
                cout << "\nInvalid Choice, please try again.\n\n";
                continue;
                }

            switch( choice )
            {
                case Encrypt:
                    cout << "Encrypt Selected." << endl;
                    system("pause");
                    break;

                case Decrypt:
                    cout << "Decrypt Selected." << endl;
                    system("pause");
                    break;

                case Quit:
                    QUIT = true;
                    cout << "Exiting...\n\n";
                    break; 

                default:
                    cout << "Error in choice!\n";
                    QUIT = true;
                    break;
            } //End Switch

        } //End While

        return 0;

    } //End Main


char DoMenu(void)
{
    int MenuChoice;        

    rewind( stdin );
    system("cls");
    cout << "\n*---------------*";
    cout << "\n*   1. Encrypt\t*";
    cout << "\n*   2. Decrypt\t*";
    cout << "\n*   3. Quit\t*";
    cout << "\n*---------------*\n\n";

    cin >> MenuChoice;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return MenuChoice;
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
GUID events skipped in trace replay - why? Liston SQL Server 2005 0 April 23rd, 2007 07:32 AM
Determining Data type with CIN Bourne C++ Programming 2 March 23rd, 2007 05:40 PM
Problems with cin eye_of_telescope C++ Programming 1 March 17th, 2005 10:41 AM
Rebuild All: 2 succeeded, 20 failed, 0 skipped kingw BOOK: ASP.NET Website Programming Problem-Design-Solution 2 December 30th, 2004 05:39 PM
cin.getline alfrepa Visual C++ 0 April 14th, 2004 08:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.