tip calculator

I was wondering if someone could point me in the right direction on how to make a tip calculator. I am supposed to make one using the choice of "fair service=15% tip, good service=20% tip, and great service= 25% tip. This is my first time writing a program and I do understand too much of c++ yet. I would love any help I could get.
Work out exactly what you want to do in the program first.

I'd say you need to:
get the user input
  price (int cents or pence)
  quality of service (string or int)

calculate percentage
  if service is fair, tip = (15 / 100) * price
  if service is good...
  ...

output total
  tip
  total (price + tip)


If you have any questions on how to implement this, feel free to ask.
Here's one with a nice GUI and everything. Just installed the Qt SDK and you are good to go.

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
#include <QMainWindow>
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

class Ui_MainWindow
{
public:
    QWidget *centralWidget;
    QLabel *label;
    QLabel *label_2;
    QLabel *label_3;
    QLabel *label_4;
    QLineEdit *subtotal;
    QLineEdit *tippercent;
    QLineEdit *tipamount;
    QLineEdit *total;
    QPushButton *pushButton;
    QHBoxLayout *horizontalLayout;
    QHBoxLayout *horizontalLayout_2;
    QHBoxLayout *horizontalLayout_3;
    QHBoxLayout *horizontalLayout_4;
    QVBoxLayout *verticalLayout;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(273, 177);
        centralWidget = new QWidget(MainWindow);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));

        label   = new QLabel(centralWidget);
        label_2 = new QLabel(centralWidget);
        label_3 = new QLabel(centralWidget);
        label_4 = new QLabel(centralWidget);
        label->setObjectName(  QString::fromUtf8("label"  ));
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_3->setObjectName(QString::fromUtf8("label_3"));
        label_4->setObjectName(QString::fromUtf8("label_4"));

        subtotal   = new QLineEdit(centralWidget);
        tippercent = new QLineEdit(centralWidget);
        tipamount  = new QLineEdit(centralWidget);
        total      = new QLineEdit(centralWidget);
        subtotal->setObjectName(  QString::fromUtf8("subtotal"  ));
        tippercent->setObjectName(QString::fromUtf8("tippercent"));
        tipamount->setObjectName( QString::fromUtf8("tipamount" ));
        total->setObjectName(     QString::fromUtf8("total"     ));
        tipamount->setReadOnly(true);
        total->setReadOnly(true);

        pushButton = new QPushButton(centralWidget);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));

        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setSpacing(6);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        horizontalLayout->addWidget(label);
        horizontalLayout->addWidget(subtotal);

        horizontalLayout_2 = new QHBoxLayout();
        horizontalLayout_2->setSpacing(6);
        horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        horizontalLayout_2->addWidget(label_2);
        horizontalLayout_2->addWidget(tippercent);

        horizontalLayout_3 = new QHBoxLayout();
        horizontalLayout_3->setSpacing(6);
        horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
        horizontalLayout_3->addWidget(label_3);
        horizontalLayout_3->addWidget(tipamount);

        horizontalLayout_4 = new QHBoxLayout();
        horizontalLayout_4->setSpacing(6);
        horizontalLayout_4->setObjectName(QString::fromUtf8("horizontalLayout_4"));
        horizontalLayout_4->addWidget(label_4);
        horizontalLayout_4->addWidget(total);

        verticalLayout = new QVBoxLayout(centralWidget);
        verticalLayout->setSpacing(6);
        verticalLayout->setContentsMargins(11, 11, 11, 11);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->addLayout(horizontalLayout);
        verticalLayout->addLayout(horizontalLayout_2);
        verticalLayout->addWidget(pushButton);
        verticalLayout->addLayout(horizontalLayout_3);
        verticalLayout->addLayout(horizontalLayout_4);

        MainWindow->setCentralWidget(centralWidget);
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow" , 0, QApplication::UnicodeUTF8));
        label->setText(            QApplication::translate("MainWindow", "Subtotal $:", 0, QApplication::UnicodeUTF8));
        label_2->setText(          QApplication::translate("MainWindow", "Tip %:"     , 0, QApplication::UnicodeUTF8));
        label_3->setText(          QApplication::translate("MainWindow", "Tip Amount:", 0, QApplication::UnicodeUTF8));
        label_4->setText(          QApplication::translate("MainWindow", "Total:"     , 0, QApplication::UnicodeUTF8));
        subtotal->setText(         QApplication::translate("MainWindow", "10.00"      , 0, QApplication::UnicodeUTF8));
        tippercent->setText(       QApplication::translate("MainWindow", "15.0"       , 0, QApplication::UnicodeUTF8));
        pushButton->setText(       QApplication::translate("MainWindow", "Calculate"  , 0, QApplication::UnicodeUTF8));

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi
};

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui_MainWindow)  {    ui->setupUi(this);  }
    ~MainWindow()  {    delete ui;  }
private slots:
    void on_pushButton_released()
    {
        double subtotal   = ui->subtotal->text().toDouble();
        double tippercent = ui->tippercent->text().toDouble();
        double tipamount  = subtotal * (tippercent/100.);
        double total      = subtotal + tipamount;
        ui->tipamount->setText(QString("%1").arg(tipamount));
        ui->total->setText(QString("%1").arg(total));
    }
private:
    Ui_MainWindow *ui;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
Last edited on
I would like to thank everyone who replied to my question. I have another question if someone could help me with it. I have this much so far but my total is coming out a negative number.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main( )
{
int a;
double percent = 0.15;
int b;
int c;
cout << "Welcome to the tip calculator" << endl;
cout << "Please enter the amount of the check. " << endl;
cin >> a;
cout << "The amount of tip you should leave. " << endl;
cin >> b;
cout << "Tip = "" $" << a * percent << endl;
cout << "The total amount of the check is " << endl;
cin >> c;
cout << "Total = " " $" << a + b << endl;

system ("pause");
return 0;
}
Excellent, we have something to work with.

When working on this sort of thing, you need to define what will be an input and what will be an output before we begin.

Right now I see:
Inputs:
- Check subtotal (a)
- Tip amount (b)
- Check total (c)

Outputs:
- Tip amount
- Total

You are asking for b and c, but then re-calculating them which is rather confusing to me as a user. Try to trim down your inputs. You really don't need all of that information. Also, use more descriptive names (a,b,c doesn't tell me anything). Finally, the amount of money could include cents and therefore require a decimal. That means we should make a,c,b doubles or floats. This may help you out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main( )
{
    double subtotal, tipamount, total;
    double tippercent = 0.15;

    cout << "Welcome to the tip calculator"          << endl
         << "Please enter the amount of the check. " << endl;
         
    cin >> subtotal;

    tipamount = subtotal * tippercent;
    total     = subtotal + tipamount ;

    cout << "Tip   = $" << tipamount << endl
         << "Total = $" << total     << endl;

    return 0;
}
Welcome to the tip calculator
Please enter the amount of the check.
10.00
Tip   = $1.5
Total = $11.5
Press any key to continue . . .
Last edited on
Thank you so much for your help. You guys are great. I will absolutely tell all my friends about you guys.
Thanks
Topic archived. No new replies allowed.