Since I'm bored…
This certainly has bugs in the edge cases (particularly relating to delimiters found at the end of the line; meh), but works for the example input, and shows (generally) one possible approach.
#include <iostream>
#include <sstream>
#include <cstring>
bool CharIsIn(const char c, const char* str, const size_t strLen)
{
for (size_t i = 0; i < strLen; i++)
if (str[i] == c)
return true;
return false;
}
// Detects the number of subsequences of "delimiters" in the line.
//
// By default a valid delimiter is either whitespace or a tab character,
// and "empty" columns are collapsed.
size_t DetectNumColumns(
const std::string& line,
const char* delimiters = " \t",
const bool collapseEmpty = true
)
{
if (line.empty())
return 0;
const size_t delimsLen = std::strlen(delimiters);
size_t result = 1;
bool lastWasDelim = true;
for (size_t pos = 0; pos < line.size(); ++pos)
{
if (CharIsIn(line[pos], delimiters, delimsLen))
{
if (!lastWasDelim || !collapseEmpty)
result++;
else if (pos == line.size()-1 && lastWasDelim && !collapseEmpty)
result++;
lastWasDelim = true;
}
else
{
lastWasDelim = false;
}
}
return result;
}
int main()
{
// Simulating your input file
std::stringstream ss;
ss << "1.5 7.6\n";
ss << "2.3 4.5\n";
ss << "9.9 7.5\n";
bool GotColumnCount = false;
int RowCount = 0, ColumnCount = 0;
std::string line;
while (std::getline(ss, line))
{
// On the first iteration (only!) count columns.
const int columns = DetectNumColumns(line);
if (!GotColumnCount)
{
// On the first iteration, store this.
ColumnCount = columns;
GotColumnCount = true;
}
else
{
// On subsequent iterations, just ensure the column
// count is consistent.
if (columns != ColumnCount)
throw std::out_of_range("Inconsistent column count in input");
}
// Always increment the row count (this bit's easy)
RowCount++;
}
std::cout << "The input consists of " << RowCount << " rows of " << ColumnCount << " columns\n";
}
The salient point is that you need to parse at least one row of text to find out how many times your delimiter(s) appears (or how many times a sequence of your delimiter(s) appears, depending on your exact requirements). You may want to parse every row of text to validate for a consistent number of columns throughout the file.
I'm deliberately not fixing the bugs, not only because I can't be arsed (though that is certainly true) but also to discourage you from simply copy/pasting this example as your solution! Please do use it for inspiration and come up with something better.
Hint: if your delimiter is always just a single character (e.g. a single whitespace), and you don't need to leniently handle things like added leading or trailing whitespace, DetectNumColumns
becomes substantially simpler than my attempt above; it's literally just counting (but be sure to count your fence's panels, rather than its posts!).