0

I'm in my first intro to Comp Sci course. The assignment I have is inputting a .txt file and then outputting a revised .txt file. The first problems I had was just with fixing the directories. I did and everything came out fine with the .txt files, except the professor says I have an error in the switch statement.

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    int s = -2, a = 0, b = 0, NumIn;

    ifstream inFile("NumbersIn.txt");
    if (inFile.fail()) {
        cout << "Unable to open file" << endl;
        return -1;
    }

    ofstream outFile("RevisedNumbers.txt");
    while (!inFile.eof()) {
        inFile >> NumIn;
        cout << NumIn << endl;
        if (NumIn < 0)
            NumIn = -1;
        else if (NumIn > 0)
            NumIn = 1;
        outFile << NumIn << endl;
        switch (NumIn) {
        case -1:
            a += s;
            break;
        case 0:
            s += 1;
        case 1:
            b += s;
        }
    }
    outFile.close();
    inFile.close();
    cout << "A = " << a << endl;
    cout << "B = " << b << endl;
    cout << "S = " << s << endl;

    return 0;
}

Output:

 0
 -2
 9
 14
 -22
 12
 6
 -9
 12
 13
 -33
 21
 9
 0
 1
 2
 3
 4
 1
 A = -4
 B = -9
 S = 0
 Program ended with exit code: 0
 

I'm including the assignment question here, so the reason why I used s, a, and b as integers:

You program will require the following files
Input file: NumbersIn.txt (attached to this assignment)
Output file: RevisedNumbers.txt
You program will use the following variables:
int s=-2, a = 0, b = 0. NumIn;
As long as there is data in the input file, your program will read a number into the variable NumIn, print it on the screen, and then determine the following:
If NumIn is negative change it to -1
If NumIn is positive change it to +1
If NumIn is zero, do nothing
Write the new value of NumIn to the output file RevisedNumbers
After updating NumIN, use a switch statement to based on the current value of NumIn
When NumIn is -1 increase the value of A by S
When NumIn is 0 increment S by 1
When NumIn is 1 increase the value of B by S
When you no longer have data to read in print out the current value of A, B and S.

Submit a copy of your program.
The screen output
The input file.
The output file.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    You use a `break` after the first `case` of your `switch` statement, but not for the rest of the cases. Why is that? – jjramsey Nov 16 '21 at 15:36
  • 1
    What is the program supposed to do? case 0: and case 1: don't have breaks, so case 0 will fall through to case 1, is this intentional? – StefanFFM Nov 16 '21 at 15:37
  • 5
    There is also [a serious problem](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) with your input loop. – molbdnilo Nov 16 '21 at 15:39
  • 2
    It would be easier to guess what your program is supposed to do - and thus tell what's wrong - if you didn't use nonsense names like "s", "a", and "b", and gave a more specific description of the process than "revised". – molbdnilo Nov 16 '21 at 15:41

1 Answers1

0

Your version:

    switch (NumIn) {
    case -1:
        a += s;
        break;
    case 0:
        s += 1;
    case 1:
        b += s;
    }

To fix it, add two break statements:

    switch (NumIn) {
    case -1:
        a += s;
        break;
    case 0:
        s += 1;
        break;
    case 1:
        b += s;
        break;
    }

Without them, case 0 falls through to case 1. It's actually legal to do that, but considered a common bug, so you should ALWAYS make a comment if your fall-through is intentional. I presume this time it isn't.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • "*It's actually legal to do that, but considered a common bug*" - which is why most compilers have warnings for it. "*you should ALWAYS make a comment if your fall-through is intentional*" - in fact, C++17 adds a [`[[fallthrough]]` attribute](https://en.cppreference.com/w/cpp/language/attributes/fallthrough) for that exact purpose. – Remy Lebeau Nov 16 '21 at 18:00