Results 1 to 5 of 5

Thread: adding up numbers from text file

  1. #1
    Join Date
    Nov 2015
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default adding up numbers from text file

    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)

    Qt Code:
    1. {
    2. QString fileName = "/home/sss.txt";
    3. QFile mFile(fileName);
    4. mFile.open(QFile::ReadOnly | QFile::Text);
    5. QTextStream stream(&mFile);
    6. QString line;
    7. do {
    8. line = stream.readLine();
    9. QStringList parts = line.split("/", QString::KeepEmptyParts);
    10. if (parts.length() == 3) {
    11. QString aa = parts[0];
    12. QString ab = parts[1];
    13. QString ac = parts[2];
    14. int a, sum;
    15. sum = 0;
    16. int arraya[] = {aa.toInt()};
    17.  
    18. for (a=0; a<1; a++)
    19. {
    20. int totala = sum+=arraya[a];
    21. QString suna = QString::number(totala);
    22. ui->textEdit->append(suna);
    23. }
    24.  
    25. }
    26. } while (!line.isNull());
    27. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: adding up numbers from text file

    convert qstring into std::string and then use std::stoi .

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: adding up numbers from text file

    Quote Originally Posted by ImaRebel View Post
    convert qstring into std::string and then use std::stoi .
    macaddy already got ints, converting to a different string won't help anything.
    But the posted code is indeed quite confusing.

    So the goal is to get the sum of all numbers in the first "column", right?
    In order to make a sum across the iterations of the loop, the sum variable has to be declared outside the loop.

    And no need for temporary single element arrays. A single int fits nicely into a single int variable

    Cheers,
    _

  4. #4
    Join Date
    Nov 2015
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: adding up numbers from text file

    Thanks for the reply imarebel and thanks for the advice anda. I can understand the code looking confusing, it kind of morphed in to that over time as i got more confused and desperate.

    Is there any site you can point me to or give me an example so I can work this out myself? Up until now I've learned as I went along but inspite of literally searching google for over a week I haven't really found anything that has helped. Thanks

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: adding up numbers from text file

    If all you want is the sum of the numbers in the *first* column, try this:

    Qt Code:
    1. int sumCol0 = 0;
    2. do {
    3. line = stream.readLine();
    4. QStringList parts = line.split("/", QString::KeepEmptyParts);
    5. if ( parts.length() >= 1 ) {
    6. sumCol0 += (parts[0]).toInt();
    7. }
    8. } while (!line.isNull());
    9.  
    10. ui->textEdit->setText( QString::number( sumCol0 ) );
    To copy to clipboard, switch view to plain text mode 

    You should be able to figure out how to extend this if you want sums for each column. Do you understand why the declaration and initialization of sumCol0 is *outside* the loop that reads each line?

Similar Threads

  1. Replies: 7
    Last Post: 12th September 2012, 06:42
  2. Replies: 3
    Last Post: 29th August 2011, 22:39
  3. how convert numbers into text
    By tommy in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2009, 10:01
  4. a Text Editor with line numbers...
    By fullmetalcoder in forum Qt Programming
    Replies: 47
    Last Post: 5th April 2006, 11:10
  5. Adding numbers to circles in QPaint
    By therealjag in forum Qt Programming
    Replies: 1
    Last Post: 12th February 2006, 10:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.