Extra space/char when using setfill() and setw(), Output changed after printing again

63 views Asked by At

Im a student and we are only allowed to use what we are taught. Our professor only taught us setfill(), setw(), left, right for . Ive been trying to print my code as a box with setfill() and setw() but there is an extra space or char in the output and I dont know why. When calling a function and returning, the 2nd setfill() and set() pair changed the output.

I tried moving things around but when I call a different function and go back it brings outputs an extra char/space. I expect a '|' char then 80 '-' char then ending with another '|' char. After calling a function and returning, the output changed to be 2 '|' char then 80 '-' char. I tried maybe including more endl, changing positions of setw() and setfill(), adding left, right. Nothing changed.

Snippet of the code function to print:

void charChoicesList(char& charChoice, householdList houseArray[], int countArray, householdList sortedArray[])
{

    do {
        cout << setw(50) << "[Please choose a category]" << endl;
        cout << "|" << setfill('-') << setw(80) << "|" << endl;
        cout << setfill(' ');
        
        cout << setw(80) << left << "|A. Print all Data" << "|\n"
             << setw(80) << left << "|B. List Identification code of households with income greater than the average" << "|\n"
             << setw(80) << left << "|C. Determine the percentage of households having an income below poverty level" << "|\n"
             << setw(80) << left << "|D. Print all Data sorted by household income" << "|\n"
             << setw(80) << left << "|E.Calculate and print the median household income" << "|\n"
             << setw(80) << left << "|F.Exit Program" << "|" << endl;

        cout << "|" << setfill('-') << setw(80) << "-" << "|" << endl;
        cout << setfill(' ') << endl;

        cin >> charChoice;

        switch (charChoice) {
        case 'a':
        case 'A':
            cout << "A. Print all Data\n" << endl;
            printData(houseArray, countArray);
            cout << endl;
            break;
        case 'b':
        case 'B':
            cout << "B. List Identification code of households with income greater than the average\n" << endl;
            calcAverageList(houseArray, countArray);
            cout << endl;
            break;
        case 'c':
        case 'C':
            cout << "C. Determine the percentage of households having an income below poverty level\n" << endl;
            povertyPercent(houseArray, countArray);
            cout << endl;
            break;
        case 'd':
        case 'D':
            cout << "D. Print all Data sorted by household income\n" << endl;
            printSorted(houseArray, countArray, sortedArray);
            cout << endl;
            break;
        case 'e':
        case 'E':
            cout << "E. Calculate and print the median household income\n" << endl;
            calculateMedian(houseArray, countArray, sortedArray);
            cout << endl;
            break;
        case 'f':
        case 'F':
            cout << "F. Exit Program\n" << endl;
            break;
        default:
            cout << "Input Error!\n" << endl;

        }
    } while (charChoice != 'f' && charChoice != 'F');

}
0

There are 0 answers