Calculator on Qt C++. Problem with QString and delete.

Hi! I'm new in C++ programming. THis is my First GUI application. It's just a very simple Calculator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QMessageBox>
#include<iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


double *a=new double;
double *b=new double;//
double *c=new double;//

void MainWindow::on_pushButton_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text()+"1");
}

void MainWindow::on_pushButton_2_clicked()
{
ui->lineEdit->setText(ui->lineEdit->text()+"2");
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text()+"3");
}

void MainWindow::on_pushButton_4_clicked()
{
ui->lineEdit->setText(ui->lineEdit->text()+"4");
}

void MainWindow::on_pushButton_5_clicked()
{
   ui->lineEdit->setText(ui->lineEdit->text()+"5");
}

void MainWindow::on_pushButton_6_clicked()
{
ui->lineEdit->setText(ui->lineEdit->text()+"6");
}

void MainWindow::on_pushButton_8_clicked()
{
     ui->lineEdit->setText(ui->lineEdit->text()+"8");
}

void MainWindow::on_pushButton_7_clicked()
{
     ui->lineEdit->setText(ui->lineEdit->text()+"7");
}

void MainWindow::on_pushButton_9_clicked()
{
ui->lineEdit->setText(ui->lineEdit->text()+"9");
}

void MainWindow::on_pushButton_10_clicked()
{
     ui->lineEdit->setText(ui->lineEdit->text()+"0");
}

void MainWindow::on_pushButton_11_clicked()
{
     ui->lineEdit->setText(ui->lineEdit->text()+"+");
}

void MainWindow::on_pushButton_12_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text()+"-");
}

void MainWindow::on_pushButton_13_clicked()
{
ui->lineEdit->setText(ui->lineEdit->text()+"*");}

void MainWindow::on_pushButton_14_clicked()
{
  ui->lineEdit->setText(ui->lineEdit->text()+"/");
}

void MainWindow::on_pushButton_15_clicked()

{
    int pos;

string sf, s,s1,s0;

QString str=ui->lineEdit->text();//Getting text from a Lineedit.
pos=(str.indexOf("+",Qt::CaseInsensitive)||(str.indexOf("-",Qt::CaseInsensitive))||(str.indexOf("*",Qt::CaseInsensitive))||(str.indexOf("/",Qt::CaseInsensitive))); //Finding a position of operator.
s=str.toStdString();//Converting QString to a transitional string variable;
sf=s.substr(0,pos+1);
QString st;
st=st.fromStdString(sf);
*a=st.toDouble();
QString sd,st0;
s1=s.substr(pos+1,s.length());
sd=sd.fromStdString(s1);
*b=sd.toDouble();
if (str.contains('+',Qt::CaseInsensitive)){
     *c=*a+*b;
  QString st1=QString::number(*c);
    ui->lineEdit->setText(ui->lineEdit->text()+"= "+st1);

}
else if(str.contains('-',Qt::CaseInsensitive)){
    *c=*a-*b;
  QString st1=QString::number(*c);

            ui->lineEdit->setText(ui->lineEdit->text()+"= "+st1);
}

else if(str.contains('*',Qt::CaseInsensitive))  {
    *c= *a * *b;
    QString st1=QString::number(*c);
            ui->lineEdit->setText(ui->lineEdit->text()+"= "+st1);}
else if(str.contains('/',Qt::CaseInsensitive)) {
  *c=*a/ *b;
  QString st1=QString::number(*c);
       ui->lineEdit->setText(ui->lineEdit->text()+"= "+st1);}


    }

void MainWindow::on_pushButton_16_clicked()
{
    if (!(ui->lineEdit->text().isEmpty())){
ui->lineEdit->setText("");
    }
}

Above is a whole mainwindow.cpp file. Here I got 2 problemes
1) The data, which I get from lineEdit is a QString. But function like substr can not be used to a QString. Is there some similar operation with QString?
2) When I'm tryieng to delete a b and c in here like this
1
2
3
4
5
6
7
void MainWindow::on_pushButton_16_clicked()
{delete a;
delete b;
delete c;
    if (!(ui->lineEdit->text().isEmpty())){
ui->lineEdit->setText("");
    }

THen the first result is correct, but when I press the pushButton_16 and try to input the next equation, the compiler sys me, that there's an error :
The program has unexpectedly finished.
/home/alexis/Документы/Qt/Projects/build-MyCalculator-Desktop-Release/MyCalculator crashed
What did I wrong?
Once more, excuse me for grammar mistakes )))
Last edited on
1) The data, which I get from lineEdit is a QString. But function like substr can not be used to a QString. Is there some similar operation with QString?
QString::section: http://qt-project.org/doc/qt-5/qstring.html

2) When I'm tryieng to delete a b and c in here like this ...
You're making a rod for your own back.

Instead of doing,
1
2
3
double *a=new double;
double *b=new double;//
double *c=new double;// 

do:
1
2
3
double a;
double b;
double c;

Plus, they should be local variables, not globals.
Last edited on
Topic archived. No new replies allowed.