-2

I just recently created a code in C++ where I have to display the occurrence of numbers from a text file that I made using this code:

using namespace std;

int main()
{

    bool isCovered[99] = {};
    int number;


    // Read each number and mark its corresponding element covered
    while (cin.good())
{
    cin >> number;
    if (number == 0)
    {
        break;
    }
    if ((number >= 1) && (number <= 99))
    {
        isCovered[number - 1] = true;
    }
}


    // Check if all covered
    bool allCovered = true; // Assumes all covered initially
    for (int i = 0; i < 99; i++)
        if (!isCovered[i])
        {
            allCovered = false; //Finds one number not covered
            break;
        }
    // Display result
    if (allCovered)
     cout << "The tickets cover all numbers" << endl;
    else
     cout << "The tickets don't cover all numbers" << endl;

    return 0;
}

It's not displaying a result, is the program too complex, or is it something that I'm missing?

EDIT: Thanks @selbie for the edit to my code, i was able to figure out that it was a user input but when i put in a zero for the final input. It displays the messages "The tickets don't cover all numbers", why is that?

Big Pete
  • 1
  • 1
  • 1
    You don't need a loop to initialize all items in `isCovered` to false. You can simply declare `bool isCovered[99] = {};` – selbie Jan 16 '20 at 01:30
  • 2
    If you need to count the number of occurrences it would make more sense to hold an array of integers so that `count[number - 1]` will give you the frequency of `number`. And just do `count[number-1]++` to count. – David G Jan 16 '20 at 01:32
  • 1
    I get perfectly sensible results when I run that program. Exactly what input did you give it? – abelenky Jan 16 '20 at 01:34
  • Adding onto abelenky's comment, example output that you are seeing alongside the output you expect to see would be helpful in determining the root cause of the problem. – Skyler Ferris Jan 16 '20 at 01:36
  • Alright @selbie, i went and removed the ```// Initialize the array``` part and added the ```{}``` but repl.it is still showing a blank result. – Big Pete Jan 16 '20 at 01:36
  • Ok @0x499602D2, @abelenky, i was able to input the numbers into the ```{}``` but it came up with this error message: ```main.cpp:7:41: error: constant expression evaluates to 45 which cannot be narrowed to type 'bool' [-Wc++11-narrowing] ...= {5, 78, 8, 9, 70, 45, 8};``` what could it mean? I'm still learning C++ as a student. – Big Pete Jan 16 '20 at 01:43
  • If you type any number into your input stream greater than `99`, you'll get undefined behavior. – selbie Jan 16 '20 at 01:52
  • @BigPete `{5, 78, 8, 9, 70, 45, 8}` -- Where did this new code come from? Also, as suggested, you need to check if anything greater than 99 is entered – PaulMcKenzie Jan 16 '20 at 01:54
  • 1
    What do you mean by not displaying anything? Is console blank? What is the input? – TruthSeeker Jan 16 '20 at 01:54
  • @PaulMcKenzie i have a series of numbers in a text file that i'm trying to get the program to run. But they are not puncuated (basically without commas). – Big Pete Jan 16 '20 at 01:59
  • Pete, are you manually typing your numbers into the console to test, or are you using file redirection (ala: `program.exe < input.txt` ) ? – selbie Jan 16 '20 at 02:00
  • @BigPete In your comment, you highlighted the error as if that text is within your code. All you were told to do is `bool isCovered[99] = {};` – PaulMcKenzie Jan 16 '20 at 02:00
  • Then, problem could be the reading file or somewhere else. Please provide [minimum reproducible exams](https://stackoverflow.com/q/5963269/4139593) – TruthSeeker Jan 16 '20 at 02:07
  • @selbie i'm not too sure. I'll see if i can keep messing around with it. – Big Pete Jan 16 '20 at 02:29
  • This sounds like a task for `std::map` or even just `std::set`, rather than an array – Remy Lebeau Jan 16 '20 at 05:43

1 Answers1

1

The bug, if any, is here:

cin >> number;
while (number != 0)
{
    isCovered[number - 1] = true;
    cin >> number;
}

Two possible issues:

  1. If reading from a redirected file, there's no detection of an end-of-file condition. Such would be the case if the program was invoked as program.exe < input.txt and the input.txt did not contain a 0. Without checking for eof, the program will hang when reading from an input file redirection.

  2. Further, there's nothing to guard against bad input. i.e. numbers outside the range of [1..99]. Without guarding against out of range numbers, undefined behavior will get introduced. Or more likely, the stack will get trashed as a result of inserting into a memory address out of range.

The easy fix is this:

while (cin.good())
{
    cin >> number;
    if (number == 0)
    {
        break;
    }
    if ((number >= 1) && (number <= 99))
    {
        isCovered[number - 1] = true;
    }
}
selbie
  • 100,020
  • 15
  • 103
  • 173
  • ` // Read each number and mark its corresponding element covered while (cin.good()) { cin >> number; if (number == 0) { break; } if ((number >= 1) && (number <= 99)) { isCovered[number - 1] = true; } }` So, i was able to change it but didn't realize that i had to manually input my numbers in the system, lol, i also noticed that when i put `0` in for my final input it gave me `cout << "The tickets don't cover all numbers" << endl;` – Big Pete Jan 16 '20 at 17:32