need to have user end when desired and not enter 0s or letters in my calculator program

1. I need to add a function that will alert a user not to use numbers <= 0 or a character for input. They are to enter numbers only greater than 0 on the calculator. (besides the symbol of course that is used to do calculations.)

2. Lastly, how can I make this so I can exit when I want as a user?

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
#include<iostream>
#include<string>
#include<cmath>
#include <iomanip>
using namespace std;


int main ()
{
// declaring variables
    cout << fixed << showpoint << setprecision(2);double num1, num2,answer ;
    char symbol;
    char reply;     // hold reply to continue loop

    do
	{

        // Prompts user for choice of arimetic expression to use.
	    cout << "Please input calculation operation eg. (1 + 2): ";
        // allows user to enter input.
	    cin >> num1 >> symbol >> num2 ;

		// result if the number zero is chosen
		if(num1, num2 ==0)
		{
		break;
		}

		// result if a char is entered instead of a number
		else if (num1, num2=='a')
			{
			break;
		    }

        // result if addition is chosen
	    if(symbol=='+')
        {
		    answer = num1 + num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if subtraction is chosen

	    else if(symbol == '-')
		{
		    answer= num1 - num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if multiplication is chosen

	    else if(symbol == '*')
		{
		    answer = num1 * num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if division is chosen

	    else if (symbol == '/')
		{
		    answer = num1 / num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if modulus is chosen

	    else if (symbol == '%')
		{
		    answer = static_cast<double>(static_cast<int>(num1) % static_cast<int>(num2));
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if exponent is chosen

	    else if(symbol == '^')
        {
		    answer = pow ( num1, num2);
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
        }

		// continue loop yes or no
        cout  << "do you want to continue? y/n ";
        cin   >> reply;

    }
	// loop result if y or n is chosen
	while (reply=='y');

	// end of program
return 0;

}
Last edited on
Maybe you could use,
#include <typeinfo>
and check if num1 and num2 produces a d (I assume for double type)
1
2
3
    cout << typeid(num1).name() << endl;    // print d if a double
    cout << typeid(symbol).name() << endl;  // print c if a char
    cout << typeid(num2).name() << endl;

to test for correct type.

For the second question, instead of an infinite loop,

while(true)

You could use a conditional loop which depends on the user input,

1
2
3
4
5
6
7
do
{
    ....
    cout << "do you want to continue? y/n";
    cin   >> reply;

}while (reply=='y');

As typeid tests the type of the variable, which will always be double for num1 and num2 irrespective of their values, I don't see how it will of any use here. You already know the variables are doubles. (And while GCC will report 'd', MSVC gives 'double')

You could get input from the user into a string using getline and then check it to see if it's (e.g.) "exit". If not, then parse the string using istringstream.

Instead of parsing the string with istringstream could parse it char by char if your keen enough. This could be extended to a parser which can handle more complicated expression than those of the form "2.0 * 4.0" more readily than the ostream approach.

Andy

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
#include <iostream>
#include <sstream>
#include <string>

bool isValidValue(double val) {
    return (val > 0.0);
}

bool isValidOp(char op) {
    static const std::string allowed_ops = "+-/*%^";
    return allowed_ops.npos != allowed_ops.find(op);
}

bool processLine(const std::string& line);
void test_processLine();

int main() {
    bool running = true;

    do {
        std::cout << "Enter simple arithmetic expression or 'exit' to end program\n";
        std::cout << ">";
        std::string line;
        std::getline(std::cin, line);
        // really need to trim leading and trailing spaces...
        if(line == "exit") {
            running = false;
        } else if(line == "test") {
            test_processLine();
        } else {
            processLine(line);
        }
    } while(running);

    return 0;
}

bool processLine(const std::string& line) {
    bool result = false;
    std::istringstream iss(line);
    double lhs   = 0.0;
    double rhs   = 0.0;
    char   op    = '\0';
    char   dummy = '\0';
    if (    !(iss >> lhs >> op >> rhs)          // get what we want
         ||  (iss >> std::ws && iss.get(dummy)) // try to get more if got it ok
         || !isValidOp(op)                      // check results
         || !isValidValue(lhs)
         || !isValidValue(rhs) ) {
        std::cout << "parse failed!\n";
    } else {
        std::cout << "parse succeeded:\n"
                  << "lhs = " << lhs << "\n"
                  << "rhs = " << rhs << "\n"
                  << "op  = " << op  << "\n";
        result = true ;
    }
    return result;
}

void test_processLine() {
    struct TestCase {
        const char* line;
        bool        expectedResult;
    };

    static const TestCase testCases[] = {
        // pass cases
        {"1.0+2.0", true},
        {"1.0-2.0", true},
        {"1.0*2.0", true},
        {"1.0/2.0", true},
        {"1.0%2.0", true},
        {"1.0^2.0", true},
        {"1.0 + 2.0", true},
        {"1.0 - 2.0", true},
        {"1.0 * 2.0", true},
        {"1.0 / 2.0", true},
        {"1.0 % 2.0", true},
        {"1.0 ^ 2.0", true},
        // fail cases
        {"0.0 + 2.0", false},
        {"1.0 + 0.0", false},
        {"-1.0 + 2.0", false},
        {"1.0 + -2.0", false},
        {"1.0 + penguin", false},
        {"penguin + 2.0", false},
        {"1.0 @ 2.0", false},
        {"1.0 plus 2.0", false},
        {"1.0 + 2.0 penguins", false},
        {"1.0 2.0", false},
        {"1.0 +", false},
        {"+ 2.0", false},
        {"1.0", false},
        {"+", false},
        {"@", false},
        {"penguin", false},
        // etc
    };

    static const int count = sizeof(testCases)/sizeof(testCases[0]);

    int passed = 0;

    for(int i = 0 ; i < count; ++i) {
        const TestCase& tc = testCases[i];
        std::cout << "test case #" << (i + 1) << " : \"" << tc.line << "\"\n";
        bool result = processLine(tc.line);
        if(result == tc.expectedResult) {
            ++passed;
            std::cout << "pass\n";
        } else {
            std::cout << "fail\n";
        }
    }

    std::cout << "\n";
    std::cout << "Passed " << passed << " out of " << count << "\n";
}


(validating code inspired by cire's post here:
http://www.cplusplus.com/forum/beginner/108849/#msg592118 )
Last edited on
ok I have to use if and else if statements in the program.

so Codewriter you are on the right track. I just don't know where to put your statements in my code. Can you specify by putting it and do a copy/paste of complete code in a reply?

I tried and ended up with errors.
Last edited on
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
#include<iostream>
#include<string>
#include<cmath>
#include <iomanip>
using namespace std;

int main ()
{
// declaring variables
    cout << fixed << showpoint << setprecision(2);double num1, num2,answer ;
    char symbol;
    char reply;     // hold reply to continue loop


    do
	{

        // Prompts user for choice of arimetic expression to use.
	    cout << "Please input calculation operation eg. (1 + 2): ";
        //allows user to enter input.
	    cin >> num1 >> symbol >> num2 ;

        // result if addition is chosen

	    if(symbol=='+')
        {
		    answer = num1 + num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if subtraction is chosen

	    else if(symbol == '-')
		{
		    answer= num1 - num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if multiplication is chosen

	    else if(symbol == '*')
		{
		    answer = num1 * num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if division is chosen

	    else if (symbol == '/')
		{
		    answer = num1 / num2;
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if modulus is chosen

	    else if (symbol == '%')
		{
		    answer = static_cast<double>(static_cast<int>(num1) % static_cast<int>(num2));
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
		}

        // result if exponent is chosen

	    else if(symbol == '^')
        {
		    answer = pow ( num1, num2);
		    cout << "The answer is:" << endl;
		    cout << num1 << " " << symbol << " " << num2 << " = " << answer << endl;
        }

        cout  << "do you want to continue? y/n ";
        cin   >> reply;

    }while (reply=='y');


//system ("pause");
return 0;

}

Last edited on
Thank you all this resolved my issues.
Topic archived. No new replies allowed.