In c++ I am working on a code that creates a table in a .txt file for organization of money and I am having an issue where the lines separating the columns don't match up if you don't put exactly exactly 10 numbers/letters into the box. Any ideas on how to fix this?
Here is a screenshot of what the issue
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream outputFile;
double monday, tuesday, wednesday, thursday, friday;
int columns;
string categories;
outputFile.open("organize.txt");
cout << "How many columns do you need in your table? ";
cin >> columns;
outputFile << "| Categories | Monday | Tuesday | Wednesday | Thursday | Friday |\n";
outputFile << "|------------|------------|-----------|-------------|------------|------------|\n";
for (int count = 1; count <= columns; count++)
{
cout << "Enter category (no more than "
<< count << ": ";
cin >> categories;
outputFile <<"| " << categories;
}
outputFile.close();
return 0;
}
Basically I need to be able to stop the user from entering more than 10 letters/numbers while providing empty space to fill the area if they used less than 10 characters.
Though it is not entirely clear how you intend to fill the Monday - Friday
columns, with your "Categories"
columns being the first, you can use std::setw()
to ensure that column is filled properly. Looking at the "Categories"
column with a maximum of 10-characters, with 2-spaces (one on each side) and 2-| (one on each side) you have a total width for those of 14-characters.
Since categories
is std::string
, after taking input from the user, you find the number of characters input with categories.size()
. So to read a category from the user, output the category (after "| "
) and then compute the number of characters needed to fill the remainder of the field (saving room for the last '|'
) you could use:
std::setw(14-categories.size()-1)
Putting your example together to fill the first Categories
column with the user input you could do:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
int main (void) {
std::ofstream outputFile;
// double monday, tuesday, wednesday, thursday, friday;
int columns = 0;
std::string categories;
outputFile.open("organize.txt");
std::cout << "How many columns do you need in your table? ";
if (!(std::cin >> columns)) {
std::cerr << "stream error on input.\n";
return 1;
}
outputFile << "| Categories | Monday | Tuesday | Wednesday "
"| Thursday | Friday |\n";
outputFile << "|------------|------------|-----------|-------------"
"|------------|------------|\n";
for (int count = 0; count < columns; count++)
{
std::cout << "Enter category (" << count+1 << "/" << columns << "): ";
std::cin >> categories;
outputFile << "| " << categories << std::setw(14-categories.size()-1)
<< "|\n";
}
outputFile.close();
return 0;
}
Example Use/Output
$ ./bin/cout_columns
How many columns do you need in your table? 7
Enter category (1/7): nuts
Enter category (2/7): bolts
Enter category (3/7): screws
Enter category (4/7): nails
Enter category (5/7): staples
Enter category (6/7): pliars
Enter category (7/7): hammer
Output File
$ cat organize.txt
| Categories | Monday | Tuesday | Wednesday | Thursday | Friday |
|------------|------------|-----------|-------------|------------|------------|
| nuts |
| bolts |
| screws |
| nails |
| staples |
| pliars |
| hammer |
That in-a-nut-shell is the way you will work down the rest of the columns to size and fill each. The challenge being you will have to have a full-weeks data for each of the categories before you start writing to the file because there isn't a way to back up and fill them in later.
Look things over and let me know if this is what you were after or if you have any questions.