0

For some reason, it goes into the cin statement the first time but then skips it every other time in this code. After running it a few times, I realized it goes into the default case for some reason without even asking for the user input. Why?

Here's the code. It's a menu with a huge switch statement. The functions inside the switch statements aren't relevant here since it's just the overall loop.

int main()
{
    Document* myDocs = new Document[10];
    int docCount = 0;
    bool menu = true;
    int userInput = 0;

    while ( menu == true )
    {
        //int userInput = 0; 
        userInput = 0;
        cout << "----------------MENU----------------" << endl << endl;
        cout << " --------(1) LOAD DOCUMENT (1)--------- " << endl << endl;
        cout << "--------(2) OUTPUT DOCUMENT (2)--------" << endl << endl;
        cout << " --------(3) ANALYZE DOCUMENT (3)-------- " << endl << endl;
        cout << " ------(4) COMPARE TWO DOCUMENTS (4)------ " << endl << endl; 
        cout << " ----------(5)  ENCRYPT  (5)---------- " << endl << endl; 
        cout << "-----------(6)  DECRYPT  (6)----------" << endl << endl;
        cout << "-------------(7) EXIT (7)--------------" << endl << endl; 

        cin >> userInput;
        string docName;
        string outputLoc;

        switch (userInput)
        {
        case 1:
            // Function
            break;
        case 2:
            // Function
            break;
        case 3-8:
            // Function
            break;
        default:
            break;
        }

Basically, I first enter the first userinput. Let's say I enter 1. Then it goes into case 1. But then after it gets out of case 1, it goes into an infinite loop that keeps displaying the menu. Shouldn't it stop at the cin statement? That's what I dont understand.

EDIT::

Case 1 is the primary one i'm worried about since I'm trying them 1 by and 1 and case 1 doesn't work. This is the full code for case 1:

    case 1: // Load Document
        {
            string inputLoc;
            cout << "Please input the document name:" << endl;
            cin >> docName;
            myDocs[docCount].setName(docName);
            myDocs[docCount].id = docCount;
            cout << "Input Location: " << endl;
            cin >> inputLoc; 
            myDocs[docCount].loadDocument(inputLoc);
            docCount++;
            break;
        }

I'm starting to speculate there is something wrong with my loadDocument function. Here is the code for that:

    void Document::loadDocument(string name)
    {
ifstream myFile(name);
int numOflines = 0; 
string theLine;
char words; 

while (myFile.get(words))
{
    switch (words)
    {
    case '.':
        numOflines++;
        break;
    case '?':
        numOflines++;
        break;
    case '!':
        numOflines++;
        break;
    }
}

lineCount = numOflines; 
setLineCt(numOflines);
arr = new Line[lineCount];
myFile.close();
char theChar;
ifstream myFile2(name);
int key = 0;

if (myFile2.is_open())
{
    for ( id = 0; id < lineCount; id++ )
    {           
        while (myFile2>> noskipws>>theChar && theChar != '.' && theChar != '!' && theChar != '?') // Changed from || to &&
        {
            //myFile2>> noskipws >> theChar;
            theLine[key] = theChar;
            key++;
        }           

        myFile2>>theChar;

        arr[id].setStr(theLine); 
    }
}
}

Basically I'm trying to load the document and store the word count and line count from it. Is there something wrong in it?

  • possible duplicate of [Cin in a while loop](http://stackoverflow.com/questions/19307979/cin-in-a-while-loop) – Daniel May 03 '14 at 22:15
  • 1
    Do you really have `case 3-8:` in your program? – R Sahu May 03 '14 at 22:24
  • Can you post rest of `main()`? – R Sahu May 03 '14 at 22:25
  • question is not clear I tried your code and every thing is OK. try to put some break points and know where exactly the problem, or put your hole code and what you inserted in. – Mohammad Alshaar May 03 '14 at 23:06
  • Sorry. No I don't really have a case 3-8 in my code. I just didn't want to list them all out. The rest of my main is 800 lines of code, so I thought that would be too long if I added each function and everything. It may be because of one of the functions in case 1 since that's the one i've been trying. I edited my original post – user3421510 May 03 '14 at 23:15
  • 1
    At 800 lines, you might want to consider some refactoring. – JBentley May 03 '14 at 23:22
  • I know. I just don't have the time at this moment. Sorry.But I have a question. When I type in file location, do I type in the file address? Like for ex: C:\Users\Me\Visual Studio 2012...etc.? – user3421510 May 03 '14 at 23:24
  • If your program starts looping over and over and over, you may want to check that all of your types associated and interacting with the input are sound. I had a friend type a less-than-friendly string into a terminal that was expecting an integer once, and the whole thing went nuts. – jaredad7 May 04 '14 at 06:26
  • Before cin ..probably u can try flushall() – sameer karjatkar May 05 '14 at 11:45

0 Answers0