I need to write a program in QT4 that converts the value entered by the user to Celsius or to Fahrenheit

The temperatue.cpp:

I need to write a program that converts celcius to fahrenheit and vice versa

the temperature.cpp is:

#include "temperature.h"
//constructor
Temperature::Temperature() {
//{initializes and places widgets using a layout
setWindowTitle("Celcius to Fahrenheit Converter");
QGridLayout* layout = new QGridLayout;
QLabel* inputRequest = new QLabel("Enter the temperature ");
tempEntry = new QLineEdit;
tempEntry->setMaxLength(20);
tempEntry->setAlignment(Qt::AlignRight);
QPushButton* convertC = new QPushButton("C_To_F");
QPushButton* convertF = new QPushButton("F_To_C");
QLabel* labelC = new QLabel("Celcius ");
resultC = new QLCDNumber;
resultC->setSegmentStyle(QLCDNumber::Flat);
resultC->setDigitCount(4);
QLabel* labelF = new QLabel("Fahrenheit ");
resultF = new QLCDNumber;
resultF->setSegmentStyle(QLCDNumber::Flat);
resultF->setDigitCount(4);
QPushButton* clear = new QPushButton("Clear All");
layout->addWidget(inputRequest, 0,0);
layout->addWidget(tempEntry, 0,1);
layout->addWidget(convertC, 0,2);
layout->addWidget(convertF, 1,2);
layout->addWidget(new QLabel, 1, 0); //for spacing
layout->addWidget(labelC,2,0);
layout->addWidget(resultC,2,1);
layout->addWidget(labelF,3,0);
layout->addWidget(resultF,3,1);
layout->addWidget(clear, 4,1);
setLayout(layout);
//}
//{ connect signals and slots
connect(convertC, SIGNAL(clicked()), this, SLOT(calculateC()));
connect(convertF, SIGNAL(clicked()), this, SLOT(calculateF()));
connect(clear, SIGNAL(clicked()), this, SLOT(clearFields()));
//}
}
// display the Celcius equivalent of the int
void Temperature::calculateC() {
int answer=0;
double ctemp;

QString iStr = tempEntry->text();
bool ok;
int i = iStr.toInt(&ok);
if (!ok) {
error = new QErrorMessage(this);
error->showMessage("Please enter a valid integer");
}
resultC->display(i);
}

// display the Fahrenheit equivalent of the int
void Temperature::calculateF() {
int answer=0;
double ftemp;
QString iStr = tempEntry->text();
bool ok;
int i = iStr.toInt(&ok);
if (!ok) {
error = new QErrorMessage(this);
error->showMessage("Please enter a valid integer");
}
resultF->display(i);
}

//clear widgets
void Temperature::clearFields(){
tempEntry->clear();
resultC->display(0);
resultF->display(0);
}

How do I add the conversion calculations?

Here are the formulae for converting between Fahrenheit and Celsius:

Celsius = (5.0 / 9.0) * (Fahrenheit - 32)
Fahrenheit = (9.0 / 5.0) * Celsius + 32

Once you have the values for Celsius or Fahrenheit, just plug in the formula at the appropriate place and display the result.

This article has been dead for over six months. Start a new discussion instead.