Chris U Chris U - 1 month ago 11
C++ Question

Formatting strings based on other variables

Working on an assignment for class that asks us for inputs such that we create a "corral" based on numbers from input. I'm having just an infuriatingly hard time trying to get the spacing right based on another variable. The corral should look something like:

|==|==|==|
: :
- -
: :
|==|==|==|


My issue being the empty space in the center (which should be based on the length of the top/bottom). Setw() works to some degree, but again, just an infuriating time. Here's the rest of what I have so far:`

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

main () {

int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
char choice;
theloop = 0;

while (theloop < 1) {

do {
cout << "How many North/South Fence posts? ";
cin >> nsfp;
if (nsfp < 2)
cout << "Value must be at least 2, please try again\n";
else if (nsfp > 10)
nsfp = 10;
} while (nsfp < 2);

do {
cout << "How many East/West Fence posts? ";
cin >> ewfp;
if (ewfp < 2)
cout << "Value must be at least 2, please try again\n";
else if (ewfp > 10)
nsfp = 10;
} while (ewfp < 2);

for (count = 1; count <= nsfp; count++) {
cout << "|";
nsfp2 = nsfp - 1;
for (count = 1; count <= nsfp2; count++) {
cout << "==|";
nsfpw = count;
}
}

for (count = 1; count < ewfp; count++) {
cout << "\n:" << " " << ":" << endl;
ewfp2 = ewfp;
for (count = 1; count < ewfp2; count++){
cout << "-" << setw(nsfpw) << "-" << "\n:" << ":" << endl;
}
}

cout << "\n" << nsfp << endl;
cout << ewfp;
theloop = 1;
}
}

Answer

Perhaps what you're looking for is this. The number of spaces comes out to be

3 * nsfp - 4;

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

main () {

    int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
    char choice;
    theloop = 0;

    while (theloop < 1) {

    do { 
    cout << "How many North/South Fence posts? ";
    cin >> nsfp;
    if (nsfp < 2)
        cout << "Value must be at least 2, please try again\n";
    else if (nsfp > 10)
        nsfp = 10;
    } while (nsfp < 2);

    do { 
    cout << "How many East/West Fence posts? ";
    cin >> ewfp;
    if (ewfp < 2)
        cout << "Value must be at least 2, please try again\n";
    else if (ewfp > 10)
        nsfp = 10;
    } while (ewfp < 2);

    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }
    cout << "\n";
    int spaces = 3 * nsfp - 4;
    for (count = 1; count < ewfp; count++) {
        cout << ":";
        for (int i = 0; i < spaces; i++) {
            cout << " ";
        }
        cout << ":\n-";
        ewfp2 = ewfp;
        for (int i = 0; i < spaces; i++){
            cout << " ";
        }
        cout << "-\n";   
    }
    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }

    cout << "\n" << nsfp << endl;
    cout << ewfp;
    theloop = 1;
    }
}

A cleaner way to do this:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

main () {

    int theloop,nsfp,ewfp,count,nsfp2,ewfp2,nsfpw,count2;
    char choice;
    theloop = 0;

    while (theloop < 1) {

    do { 
    cout << "How many North/South Fence posts? ";
    cin >> nsfp;
    if (nsfp < 2)
        cout << "Value must be at least 2, please try again\n";
    else if (nsfp > 10)
        nsfp = 10;
    } while (nsfp < 2);

    do { 
    cout << "How many East/West Fence posts? ";
    cin >> ewfp;
    if (ewfp < 2)
        cout << "Value must be at least 2, please try again\n";
    else if (ewfp > 10)
        nsfp = 10;
    } while (ewfp < 2);

    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }
    cout << "\n";
    string spaces(3 * nsfp - 4, ' ');
    for (count = 1; count < ewfp; count++) {
        cout << ":" << spaces << ":\n";
        cout << "-" << spaces << "-\n";
    }
    for (count = 1; count <= nsfp; count++) {
        cout << "|";
        nsfp2 = nsfp - 1;
        for (count = 1; count <= nsfp2; count++) {
            cout << "==|";
            nsfpw = count;
        }
    }

    cout << "\n" << nsfp << endl;
    cout << ewfp;
    theloop = 1;
    }
}
Comments