0

I want to convert the Qstring in the QLineEdit to double using QList so that it could perform a calculation and display the results in the QMessagebox. If I can get some suggestions on how this can be done it would be great.

#include <QtGui>
#include <QList>

#include <iostream>

int main (int argc, char* argv[])
{
    QApplication app(argc, argv);
    QTextStream cout(stdout);

    bool ok;
    double answer;

    do
    {
        QString mark =  QInputDialog::getText(NULL ,"MarkCalc","Enter Mark:", QLineEdit::Normal,"", &ok);

        if (ok && !mark.isEmpty())    
             QList <QString> list;

        double am = (mark * 0.20)+(mark * 0.50)+(mark * 0.30);
        double ym = am * 0.20;
        double em = 75 * 0.40;
        double fm = em + ym;

        if (em <= 40 && fm >= 50)
            cout <<"pass";
        else
            cout << "fail";

        QString response = QString("Your Final Mark: %1 \n\n%5").arg(ym).arg(em);
        answer = QMessageBox::question(0, "Final Marks", response,QMessageBox::Yes | QMessageBox::No);
    } while (answer == QMessageBox::Yes);

    return 0;
}
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
user1291092
  • 105
  • 4
  • 9
  • Why are you using %5 in QString("Your Final Mark: %1 \n\n%5").arg(ym).arg(em) when you have only have 2 arguments? – drescherjm Feb 27 '14 at 23:26
  • What are they entering as a Mark? Is this a single value or multiple values separated by commas? or something else? – drescherjm Feb 27 '14 at 23:38
  • It will be 3 values seperated by commas.I need to fix the %5 in the QString. I am very new to QT, only using it a week. – user1291092 Feb 28 '14 at 10:40
  • Then use QString::split(",") to get a QStringList from your QString. QStringList list = mark.split(","); – drescherjm Feb 28 '14 at 12:19
  • You should use QStringList instead of QList – drescherjm Feb 28 '14 at 12:21
  • I need to convert the stringlist to double in order to do the calculation. I am not sure if there needs to be an array. Thanks – user1291092 Feb 28 '14 at 18:16
  • It does not need to be an array to be converted to double. Here is some help.. foreach(QString str,list) { double d = str.toDouble(); std::cout << d << std::end; } – drescherjm Feb 28 '14 at 18:39
  • I have added the foreach and I am getting errors. – user1291092 Feb 28 '14 at 19:11
  • expected primary-expressions before 'str' expected ')' before 'str' expected ';' before double 'am' was not declared in this scope QString mark = QInputDialog::getText(NULL ,"MarkCalc","Enter Mark:", QLineEdit::Normal,"", &ok); if (ok && !mark.isEmpty()) QStringList list = mark.split(","); foreach (QString str,list){ double d = str.toDouble(); std::cout << d << std::end; } – user1291092 Feb 28 '14 at 19:12
  • The am error is in your code I would comment out your calculations for now since it can not work the way you have it. – drescherjm Feb 28 '14 at 19:25
  • I have included the #include and commented out the calculations.Still the same errors. expected primary-expressions before 'str' expected ')' before 'str' – user1291092 Feb 28 '14 at 20:01
  • You need a { after if (ok && !mark.isEmpty()) and a second } before } while (answer == QMessageBox::Yes); – drescherjm Feb 28 '14 at 20:10
  • I have tried that but I am still getting errors. There are 4 open and 4 close braces. Still the same errors. – user1291092 Feb 28 '14 at 20:18
  • I am going to create a project in QtCreator to test this. – drescherjm Feb 28 '14 at 20:31
  • Thanks. I am just getting stuck with the errors. – user1291092 Feb 28 '14 at 20:56
  • I tried the code and it worked with no problem at all (besides getting the right includes for Qt5). I will post what I have as an answer since it does convert your QString to a list then to doubles but does not just solve just solve everything for you because I want you to think and learn. – drescherjm Feb 28 '14 at 21:06

1 Answers1

0

Here is an example taking the code from the OP and splitting the QString to a QStringList then using foreach to iterate over the list converting each QString in the list to a double then printing the double using cout:

#include <QtGui>
#include <QtCore>
#include <QtWidgets>


int main (int argc, char* argv[]) {
    QApplication app(argc, argv);
    QTextStream cout(stdout);

    bool ok;
    double answer;

    do{

        QString mark =  QInputDialog::getText(NULL ,"MarkCalc","Enter Mark:", QLineEdit::Normal,"", &ok);

        if (ok && !mark.isEmpty()) {

            QStringList list = mark.split(",");

            foreach(QString str, list) {
                double d = str.toDouble();
                cout << d << endl;
            }


//            double am = (mark * 0.20)+(mark * 0.50)+(mark * 0.30);
//            double ym = am * 0.20;
//            double em = 75 * 0.40;
//            double fm = em + ym;
//            if (em <= 40 && fm >= 50)
//                cout <<"pass";
//            else
//                cout << "fail";


//            QString response = QString("Your Final Mark: %1 \n\n%5").arg(ym).arg(em);
//            answer = QMessageBox::question(0, "Final Marks", response,QMessageBox::Yes | QMessageBox::No);
        }
    } while (answer == QMessageBox::Yes);


    return 0;
}

The following line splits the QString into a QStringList using a comma for the split.

QStringList list = mark.split(",");

After that I use a foreach to iterate over the QStringList one string at a time converting each string to a double and outputting the double one line for each double:

foreach(QString str, list) {
    double d = str.toDouble();
    cout << d << endl;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • I tested this code on Qt5.2 using the Qt 5.2.1 for Windows 32-bit (MinGW 4.8, OpenGL, 634 MB) download available on http://qt-project.org/downloads – drescherjm Feb 28 '14 at 21:16
  • I tested the code on my version Qt 4.7.0 (32 bit). I removed qt widgets and it worked. So in terms of the calculation part what would be a better way to do it then the way I have. am = assignment mark, ym = year mark, em = exam mark ,fm = final mark. – user1291092 Feb 28 '14 at 21:26
  • One thing you can do is make a declare a QList scores; before the foreach. Then instead of "cout << d << endl;" insert d into the scores list. And then after the foreach loop use score[0], score[1] ... as your scores. You will want to have some checking here to make sure that the user entered 3 numbers. Also look at the documentation for QString::toDouble(). There is an optional pointer to a bool that can be used to verify that what is passed is a number. – drescherjm Feb 28 '14 at 21:56
  • I dont have to check if the user entered 3 numbers. I just have to make sure the calculation is correct. I just have to convert the 3 numbers from a string to double and calculate the final result. – user1291092 Feb 28 '14 at 22:50
  • Would the Qlist implementation be something like this:QList list; list << 1.2 << 0.5 << 3.14; std::list stdlist = list.toStdList(); – user1291092 Feb 28 '14 at 22:53
  • Can you please explain how I would do this - Then instead of "cout << d << endl;" insert d into the scores list. Thanks – user1291092 Feb 28 '14 at 22:54
  • "I dont have to check if the entered 3 numbers" so would it be if your program crashed if the user only entered 1 number? – drescherjm Mar 01 '14 at 12:43
  • It would calculate the numbers entered. For this program I just have to get the calculation correct. I would not need to test for the 3 numbers entered. – user1291092 Mar 01 '14 at 14:16