[Solved]Crashing with This signal : Arithmetic exception



  • 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



  • 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.



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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

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



  • Or install a validator



  • 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]



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.