macaddy
15th November 2015, 23:41
I'm new to QT and C++ so this might be simple enough but it's got me totally stuck!
I'm just wanting to load a text file, split it in to arrays, add up the arrays and put the totals into a textedit.
the file is structured like this num1/num2/num3 e.g :
1/4/7
2/5/8
3/6/9
So I want to do 1+2+3 = 6 (put 6 in to text edit)
{
QString fileName = "/home/sss.txt";
QFile mFile(fileName);
mFile.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&mFile);
QString line;
do {
line = stream.readLine();
QStringList parts = line.split("/", QString::KeepEmptyParts);
if (parts.length() == 3) {
QString aa = parts[0];
QString ab = parts[1];
QString ac = parts[2];
int a, sum;
sum = 0;
int arraya[] = {aa.toInt()};
for (a=0; a<1; a++)
{
int totala = sum+=arraya[a];
QString suna = QString::number(totala);
ui->textEdit->append(suna);
}
}
} while (!line.isNull());
}
What I can't figure out is how to add the numbers together! I thought it would be simple enough but I've been googling for more than a week and experimenting with different things but am just getting more frustrated with it!
Any help would be much appreciated. Thanks.
I'm just wanting to load a text file, split it in to arrays, add up the arrays and put the totals into a textedit.
the file is structured like this num1/num2/num3 e.g :
1/4/7
2/5/8
3/6/9
So I want to do 1+2+3 = 6 (put 6 in to text edit)
{
QString fileName = "/home/sss.txt";
QFile mFile(fileName);
mFile.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&mFile);
QString line;
do {
line = stream.readLine();
QStringList parts = line.split("/", QString::KeepEmptyParts);
if (parts.length() == 3) {
QString aa = parts[0];
QString ab = parts[1];
QString ac = parts[2];
int a, sum;
sum = 0;
int arraya[] = {aa.toInt()};
for (a=0; a<1; a++)
{
int totala = sum+=arraya[a];
QString suna = QString::number(totala);
ui->textEdit->append(suna);
}
}
} while (!line.isNull());
}
What I can't figure out is how to add the numbers together! I thought it would be simple enough but I've been googling for more than a week and experimenting with different things but am just getting more frustrated with it!
Any help would be much appreciated. Thanks.