Need Help ASAP

I need to make a calculator in C++ that will accept up to 5 values, and can add, subtract, multiply and divide. I can't seem but want to make an infinite amount of different if, or switch statements to do this. There has to be an easier way. Could someone possibly make a program for me that I could see how to do it?

An example would be: Please enter your problem: 5+6/7*10-4

Then it would output the answer. Please help soon. Thank you!
closed account (48T7M4Gy)
Could someone possibly make a program for me that I could see how to do it?
Almost certainly 'could' but whether anyone 'will' is another question.
Could you possibly give it a shot? I gave it a good shot and i'm just stuck.
closed account (48T7M4Gy)
Show us what you have done please.
@iPros

If you really
gave it a good shot
, then you must have some code to show for it, whether it works, or not. Show us what you've tried and failed with, and someone could possibly show you why you're
just stuck.
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
#include <iostream>

using std::endl;
using std::cout;
using std::cin;

int main()
{
	double value1{};
	double value2{};
	double value3{};
	double value4{};
	double value5{};
	double value1_2;
	char start1;
	char operation1;
	char operation2;
	char operation3;
	char operation4;


	

	cout << " =================================== " << endl
		<< " |      Simple Calculator           |" << endl
		<< "====================================" << endl;

	cout << "Please enter your first value: ";
	cin >> value1;
	cout << "Please enter your second value: ";
	cin >> value2;
	cout << "Please enter your third value: ";
	cin >> value3;
	cout << "Please enter your forth value: ";
	cin >> value4;
	cout << "Please enter your fifth value: ";
	cin >> value5;

	cout << "Your five values are: " << value1 << " " << value2 << " " << value3 << " " << value4 << " " << value5 << endl;

	cout << "What would you like to do to the first two values?" << endl
		<< "(( +, -, *, /): " << endl;
	cin >> operation1;
	cout << "What would you like to do to the next value?"<<endl
		<< "(( +, -, *, /): " << endl;
	cin >> operation2;
	cout << "What would you like to do to the next value?" << endl
		<< "(( +, -, *, /): " << endl;
	cin >> operation3;
	cout << "What would you like to do to the last value?" << endl
		<< "(( +, -, *, /): " << endl;
	cin >> operation4;

	cout << value1 << operation1 << value2 << operation2 << value3 << operation3 << value4 << operation4 << value5 << endl;



this is what I have.
Last edited on
@iPros

The code you're showing doesn't seem to be in accordance to what you first outlined.
An example would be: Please enter your problem: 5+6/7*10-4

It looks like you threw this together to appease us and make it look like you're working on it.

Anyway, here is a little help, with the way you first asked for it..

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

#include <iostream>
#include <string>

using std::endl;
using std::cout;
using std::cin;
using std::string;

void Multiplication( double &answer, double Numbers[5] );
void Division( double &answer, double Numbers[5] );
void Addition( double &answer, double Numbers[5] );
void Subtraction( double &answer, double Numbers[5] );

int main()
{
	string problem;
	double Numbers[6]={0}; // Initialize to zero
	int order[4];
	int len, temp = 0, next = 0;
	char op[4] = {'*','/','+','-'};
	cout << "Please enter the math problem, using up to 5 numbers. [ex: 3+4*5/6+8 ] : ";
	cin >> problem;
	len = problem.length()+1;

	for(int x=0;x<len;x++)
	{
		if(problem[x] >='0' && problem[x] <= '9' )
		{
			temp *= 10;
			temp += problem[x]-48;
		}
		else
		{
			Numbers[next] = temp;
			for(int y=0;y<4;y++)
			{
				if(problem[x] == op[y])
				{
					order[next] = y; // The operation requested
				}
			}
			temp=0;
			next++;
		}
		if(next > 5)
		{
			cout << "You have too many numbers in your math problem.." << endl;
			cout << "Program ending.." << endl;
			return 1;
		}
	}
	double answer=0.0;
	
	for(int x=0;x<4;x++)
	{
		if(order[x] == 0)
			Multiplication( answer, Numbers );
		if(order[x] == 1)
			Division( answer, Numbers );
		if(order[x] == 2)
			Addition( answer, Numbers );
		if(order[x] == 3)
			Subtraction( answer, Numbers );


	}
	cout << endl << "Numbers entered, were : ";
	for(int x=0;x<next;x++)
		cout << Numbers[x] << " ";

	cout << endl << "Answer is " << answer << endl << endl;
	return 0;
}

void Multiplication( double &answer, double Numbers[5] )
{
	answer+=Numbers[0]*Numbers[1];
}

void Division( double &answer, double Numbers[5] )
{
	answer+=Numbers[0]/Numbers[1];
}

void Addition( double &answer, double Numbers[5] )
{
	answer+=Numbers[0]+Numbers[1];
}

void Subtraction( double &answer, double Numbers[5] )
{
	answer+=Numbers[0]-Numbers[1];
}


This only does the first two numbers. It's now up to you to either finish you version, or get this one working for ALL the numbers inputted with the operations inputted.

Remember the operation precedence's also.
I didn't throw it together to
appease us
. I am taking a class in school and this is as much as she has taught us. The farest we have gotten is switch statements. I have no idea what
void
means.
Can anyone help?
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
#include <iostream>

int main()
{
    std::cout << "enter an expression like 5+6/7*10-4 to be evaluated from left to right,\n"
              << "    enter q to finish input : " ;

    double lhs ;
    std::cin >> lhs ; // read the first number

    double rhs ;
    char op ;
    while( std::cin >> op && op != 'q' && std::cin >> rhs ) // read in operator and next number
    {
        switch(op)
        {
            case '+' : lhs += rhs ; break ;
            case '-' : lhs -= rhs ; break ;
            case '*' : lhs *= rhs ; break ;
            case '/' : lhs /= rhs ; break ; // may want to check for division by zero
            // etc.

            default:
                std::cerr << "\n***error: unsupported operator '" << op << "'\n" ;
                return 1 ;
        }
    }

    std::cout << "\nresult: " << lhs << '\n' ;
}

http://coliru.stacked-crooked.com/a/712a05927cfa1844
@JLBorges thank you so much.
Topic archived. No new replies allowed.