Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Crashing with This signal : Arithmetic exception
Forum Updated to NodeBB v4.3 + New Features

[Solved]Crashing with This signal : Arithmetic exception

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raminlich
    wrote on last edited by
    #1

    hi all this is my program
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <fstream>
    #include <QFile>
    #include <QTextStream>
    #include <QMessageBox>
    #include <QVariant>
    #include "global_objects.h"
    using namespace std;
    ofstream myf("e:/myftock.txt",ios::app);
    ofstream mys("e:/mystock.txt",ios::app);
    string names,namef;
    int ds,df,ms,mf,amounts,amountf,sdf,sds;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->groupBox_3->hide();
    ui->groupBox_4->hide();

    }

    MainWindow::~MainWindow()
    {
    delete ui;

    }
    void MainWindow::on_actionExit_triggered()
    {
    close();
    }

    void MainWindow::on_pushButton_f_clicked()
    {
    QString name;
    int d,m,sd,amount;
    name=ui->lineEdit_name->text();
    amount=ui->lineEdit_amount->text().toInt();
    d=ui->lineEdit_d->text().toInt();
    m=ui->lineEdit_m->text().toInt();
    sd=ui->lineEdit_y->text().toInt();
    namef=name.toStdString();
    amountf=amount;
    df=d;
    mf=m;
    sdf=sd;
    myf<<namef<<" "<<amountf<<" "<<df<<"/"<<mf<<"/"<<sdf<<"\n";
    myf.close();
    }

    void MainWindow::on_pushButton_s_clicked()
    {
    QString name_s;
    int d,m,sd,amount;
    name_s=ui->lineEdit_name->text();
    amount=ui->lineEdit_amount->text().toInt();
    d=ui->lineEdit_d->text().toInt();
    m=ui->lineEdit_m->text().toInt();
    sd=ui->lineEdit_y->text().toInt();
    names=name_s.toStdString();
    ds=d;
    ms=m;
    amounts=amount;
    sds=sd;
    mys<<names<<" "<<amounts<<" "<<ds<<"/"<<ms<<"/"<<sds<<"\n";
    mys.close();
    }

    void MainWindow::on_pushButton_srch_clicked()
    {
    QString srch;
    srch=ui->lineEdit_srch->text();
    if(srch=="s"){
    ifstream mys("e:/mystock.txt");
    mys.open("e:/mystock.txt");
    mys>>names;
    ui->groupBox_3->show();
    }
    if(srch=="f"){
    ifstream myf("e:/myftock.txt");
    myf.open("e:/myftock.txt");
    myf>>namef;
    ui->groupBox_4->show();
    }
    }

    void MainWindow::on_pushButton_3_clicked()
    {
    int yes,tod;
    float all;
    QString b;
    yes=ui->lineEdit_yes->text().toInt();
    tod=ui->lineEdit_tod->text().toInt();
    mys<<yes<<" "<<tod;
    ifstream mys("e:/mystock.txt");
    mys.open("e:/mystock.txt");
    mys>>amounts;
    yes=amountsyes;
    tod=amounts
    tod;
    yes*=100;
    all=(yes/tod);
    all=all-100;
    QMessageBox msg;
    msg.setText(b = QString::number(all));
    msg.exec();
    mys.close();
    }
    @
    thanks

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Well, this is fun. What seems to be the problem?? We are not here to just figure out what is wrong and test your code. We are here to help. If you say which line crashes, what stuff doesn't work, where does the compiler give an error we might be able to help and give some pointers where to look. Even give some code snippets to correct any errors.
      So first of all:

      • What do you want your program to do?
      • Where doesn't it work?

      My first opinion: Never use global variables!! It calls for bad programming and no Object Orientated coding.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gpopelka
        wrote on last edited by
        #3

        line 103 looks like a possible integer divide by 0. Not much else to go on...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You don't check that tod is non zero, so you might very well get a crash there.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Raminlich
            wrote on last edited by
            #5

            thanks all for reply
            as you say the problem is with line 103 and i tried many ways
            i don't how solve this
            any advice will be great
            thanks

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Don't allow invalid values in your line edit. Since it's a number use a QSpinBox

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Or install a validator

                Greetz, Jeroen

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Raminlich
                  wrote on last edited by
                  #8

                  hi and thanks for your quick reply
                  i Can't use QSpinbox because i want to set large digits
                  and about validator how i must use it in my program i hit little search in google but i just get confused(little nooob :) )
                  thanks
                  [quote author="SGaist" date="1407837742"]Don't allow invalid values in your line edit. Since it's a number use a QSpinBox[/quote]

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gpopelka
                    wrote on last edited by
                    #9

                    A validator is not going to be of any use if the user neglects to populate the line edit that supplies a value to tod.

                    Before performing the division at line 103, make sure tod != 0. If it is 0, put up a messageBox, tell the user what's wrong, and return without doing anything more.

                    Alternatively, use floating point instead of integer to hold tod. An integer divide by 0 will crash, a floating point divide by 0 will return nan. If the user sees a nan in his display, maybe he can figure it out himself.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved