CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 36

Threaded View

  1. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: why is it skipping lines?

    Because you need to clear the cin buffer of invalid characters:

    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    #include <cctype> // for tolower
    #include <limits>   //for numeric_limits
    
    using namespace std;
    
    void selection();
    void addChecksum();
    void runChecksum();
    
    int main()
    {
        selection();
        
        // A more portable version of system("pause") is:
        std::cout << "Press return to continue" << std::endl;
        std::cin.get();
        return 0;
    }
    
    void selection()
    {
        char choice;
    
        cout << "Please Select: " << endl;
        cout << "  A) Add checksum to specified file" << endl;
        cout << "  B) Verify integrity of specified file" << endl;
        cout << "  Q) Quit" << endl;
    
        cin >> choice;
        cout << endl;
    
        //Clear anything currently in the cin buffer
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
        //You can reduce the size of your switch statement by using tolower.
        [COLOR=Red]switch (tolower(choice))
        {
        case 'a':
            addChecksum();
            break;
        case 'b':
            runChecksum();
            break;
        case 'q':
            return;
        default:
            cout << "Your choice did not match any of the menu options." << endl;
        }
    }
    
    void addChecksum()
    {
        string inputFileName;
        ifstream inputFile;
        string line;
    
        cout << "Specify the file path: ";
        getline(cin, inputFileName);
    
        inputFile.open(inputFileName.c_str());
        cout << endl;
    
        while(!inputFile.is_open())
        {
            cout << "The file " << inputFileName << "could not be found or opened!" << endl;
            //You have a bug here. What if someone realises they don't
            //want to load a file anymore, but want to quit instead?
            //selection will keep being called even though the user is
            // pressing 'q' because they have not entered a valid file name!
            selection();
        }
    }
    
    void runChecksum()
    {
        string inputFileName;
        ifstream inputFile;
        string line;
        int checksum;
    
        checksum = 0;
    
        while(!inputFile.is_open())
        {
            cout << "Specify the file path: " << inputFileName;
            getline(cin, inputFileName);
    
            inputFile.open(inputFileName.c_str());
        }
    
        //I guess you are going to do your calulation here?
        // ....
        cout << "File checksum = " << checksum << endl;
    }
    Last edited by PredicateNormative; July 27th, 2010 at 03:37 AM. Reason: Changed colour on font.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured