My command line calculator problem(s)

ok my code is:
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int startup()
{
    system("title choosing operation");
    cout << "Enter operation (add, subtract, multiply, divide,power,+,-,*,/,^)\n>";
}

double power(double a, double b)
{
    return pow(a,b);
}

double divide(double a, double b)
{
    return a / b;
}

double multiply(double a, double b)
{
    return a * b;
}

double subtract(double a, double b)
{
    return a - b;
}

double add(double a, double b)
{
    return a + b;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    double A;
    double B;
    string operation;
    cout << "NOTE: If the operation doesn't exist the program will exit." << endl;
    startup();
    cin >> operation;


    if (operation == "add"||operation=="+")
    {
        system("title adding");
        cout << "Enter first number: ";
        cin >> A;
        cout << "Enter second number: ";
        cin >> B;
        cout << add(A,B) << endl;
        startup();
        cin >> operation;
    }
    if (operation == "subtract"||operation=="-")
    {
        system("title subtracting");
        cout << "Enter first number: ";
        cin >> A;
        cout << "Enter second number: ";
        cin >> B;
        cout << subtract(A,B) << endl;
        startup();
        cin >> operation;
    }
    if (operation == "multiply"||operation=="*")
    {
        system("title multiplying");
        cout << "Enter first number: ";
        cin >> A;
        cout << "Enter second number: ";
        cin >> B;
        cout << multiply(A,B) << endl;
        startup();
        cin >> operation;
    }
    if (operation == "divide"||operation=="/")
    {
        system("title dividing");
        cout << "Enter first number: ";
        cin >> A;
        cout << "Enter second number: ";
        cin >> B;
        cout << divide(A,B) << endl;
        startup();
        cin >> operation;
    }
    if (operation == "power"||operation=="^")
    {
        system("title powering");
        cout << "Enter first number: ";
        cin >> A;
        cout << "Enter second number: ";
        cin >> B;
        cout << power(A,B) << endl;
        startup();
        cin >> operation;
    }
    system("title invalid operation");
    cout << "Not an operation";
    system("PAUSE >nul");
    return 0;
}


This is ran in a console application using code::blocks
Everything runs/builds fine, but there are a few stuff which i can't fix, for example:

If you input the operator once, do the calculation, then try to input the same calculation again, it will say its not a valid operation.

If you input a string or anything else apart from a double it will instantly change the second number to 0 and quit the program.

And a few others I haven't discovered.

Is there any way to fix these two? I'm not asking for code, if you can specify at least one little tip, it will help me.

Thanks, Bart
This happens because your program is set to execute only once.. It seems that you misunderstand how an "if" loop will work. Suppose I chose to add two numbers.

1
2
3
4
5
6
7
8
9
10
11
if (operation == "add"||operation=="+")   //This will check what input I gave.
    {
        system("title adding");                       //From here
        cout << "Enter first number: ";
        cin >> A;
        cout << "Enter second number: ";
        cin >> B;
        cout << add(A,B) << endl;                //Till here, is fine
        startup();                              //It calls startup function, prints some stuff.
        cin >> operation;                          //Asks for input again
    }                                            //End of "if" loop 

At this point your program ends. You understand what is going wrong?

So instead, use a while loop, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int nNumberofArgs, char* pszArgs[])
{
    double A;
    double B;
    string operation;
    cout << "NOTE: If the operation doesn't exist the program will exit. Otherwise, press "q" to exit." << endl;
    startup();
    cin >> operation;

    while (operation!="q")
    {
       //The rest of the code here. (That is, check what user wants to do, the usual)(Line 48-102 of your program here)
     }


Hope this helps... :)
THANK YOU!
YOU ARE A HERO!
I just need an anwser to my second question. Which I just think I can solve.
EDIT: I can't
Last edited on
bump
bump
bump
Not sure if it works, but try using stringstream.

Something like this might work:

Instead of this:

1
2
int number1;
cin >> number1;


Use this:

1
2
3
4
int string1;
int number1;
getline(cin, string1);
stringstream(string1) >> number1;
Topic archived. No new replies allowed.