Search code examples
c++loopsc++11vectorencapsulation

My code seems fine, but certain inputs result in undesired outputs


The goal of the program is to take in a user-input list of numbers, separated by commas. These numbers can include ranges. Before entering numbers, the user must enter a title for the problem set, but this portion of the code seems to work entirely fine. For example, if the user inputs:

"Lesson 1"1-10,12,13,15,18,19-23,25

The code will output:

"For Lesson 1, you will have to complete the following problems: 1, 2, 
3, 4,..." 

until the list is complete.

If the user inputs numbers included within a range, ranges that overlap one another, or out-of-order input (excluding backwards ranges [you cannot enter 16-12 as a range]), the code will automatically sort (in increasing order) and output each number without repeating any elements. You also cannot enter any chars besides commas and hyphens (though I believe I managed to have cin keep reading until it finds the first int value after a comma or hyphen), and must hit enter to submit the input.

For complete information on the assignment, if needed, look here: http://craie-programming.org/122/labs/seqlist.html

The problem is hard to pinpoint precisely, as it happens only when the user enters an extremely complex, though valid, input. Including overlapping ranges, repeating numbers, and out of order input seems to cause the list to repeat a few numbers here and there, and I simply cannot find why that is the case!

Any input as to what may be causing the inconsistent issues would be greatly appreciated. Thanks!

It works in some cases, but it must work in all. If the user plays nice with the code, it works fine... but this code must be able to take abuse and work through it regardless!

I have tried fiddling with the binarySearch function, altering the calculations of ub (upper bound) and lb (lower bound) within the if-else statement, but the issue seems to persist. Perhaps there is something I missed in trying, however, I was unable to find it.

I have also tried fiddling with the switch statement in the do-while loop, and everything seems fine there, although perhaps the default case may be causing the errors... if so, I certainly cannot find out why.

I am currently working on a Chromebook using the C4Droid IDE-- though, as much as Chromebooks suck, I don't think that is the problem.

Here's a link to all of my code thus far, if needed: https://docs.google.com/document/d/1umbIlfxRniBb9ANcbwcsv2K4hjAHEjH3itGFVB6p83U/edit?usp=sharing

I believe the following functions may be the cause of the problem (all encapsulated into problemList class):

bool binarySearch(int num, int lb, int ub){
        int mid = (lb + ub) / 2;
        if(ub - lb > 1){
            if(num == numbers[mid]){
                return true;
            }
            else if(num > numbers[mid]){ 
                lb = mid + 1;
                binarySearch(num, lb, ub);
            }
            else{
                ub = mid;
                binarySearch(num, lb, ub);
            }
    }
        else return false;
}

AND/OR

void addNumber(int n){ /* adds a number to the numbers vector while sorting it at the same time via a backwards bubble sort.*/  
    if(!binarySearch(n, 0, numbers.size() - 1)){
        vector<int>::size_type currentIndex = 
                numbers.size();
        while(currentIndex > 0 && numbers[currentIndex - 1] > n){
            currentIndex--;
        }
            numbers.insert(currentIndex + numbers.begin(), 
                n);
    }
}

AND/OR

void setVector(){
    bool going = true;
    unsigned int firstNum, secondNum;
    cin >> firstNum;
    numbers.push_back(firstNum);
    do{
        switch(cin.peek()){
            case ',':
                cin.ignore();
                cin >> firstNum;
                addNumber(firstNum);
                break;

            case '-':
                cin.ignore();
                cin >> secondNum;
                for(int x = firstNum + 1; x <= 
                                secondNum; x++)
                    addNumber(x);       
                         break;

            case '\n':
                going = false;
                break;

            default:
                cin.ignore();
                break;
        }
    }while(going);  
}   

If I enter a random set of numbers (as stated earlier), including overlapping ranges and pre-existing & out of order numbers, I frequently receive invalid output. Often, the output repeats numbers, skips some numbers, or even places numbers in the wrong order.

For example, if a user inputs:

"Lesson 1"1-6,3,6,2-7,4,5,1-7,9-12,10-13

The program should output:

For Lesson 1 you need to do the following problems:
1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13

Notice how it skips the 8, as 8 was not included as any numbers or within any ranges. The output I would get, however, would be (when tested):

For Lesson 1 you need to do the following problems:
1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 9, 10, 11, 11, 12, 12, 13

Solution

  • It's possible that your issue lies in the binarySearch condition:

        if(ub - lb > 1)
    

    I did not have permission to access your full code, so I cannot test this at the moment, but I think this would result in your binarySearch failing to check the first and last elements in your vector. Maybe you could try switching this to:

        if(ub - lb >= 0)
    

    Hope this helps.