Home > Software engineering >  Need help correcting Homework code, C input/output
Need help correcting Homework code, C input/output

Time:11-17

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.

CodePudding user response:

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.

  • Related