Then lets think about an algorithm.
First the variables we need: Since we use arrays, there should be an upper bound of numbers we can read (and one of operators also..). I tend to store those bounds in a const var.
Then we need an array of numbers and one of ops. Since there is (in the easy way) always one more num as op, it would be like this:
1 2 3
|
const int MaxOps = 40;
double num[MaxOps + 1];
char op[MaxOps];
|
After this we should ask for an easy math expression with +-/*. The standard input is line buffered, so we know that the use inputs a line and lets say, if we read a newline char '\n' the expression ends. What we now need is a for loop that reads up to MaxOps numbers and ops from user input. Like this
1 2 3
|
for (i = 0; i < MaxOps+1; i++) {
// lets do some crazy input here
}
|
how does it work? we assume that the user do always something like this:
(number) (op) (number) (op) (number) |
That makes it easy to read from input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// READ A NUMBER WITH CIN
if (!(cin >> num[i])) { // like this its more easy, but less powerfull
// do some Error Handling
cerr << "Error occured! - Exiting\n";
return 1;
}
// READ A CHAR (here operator) if NEWLINE QUIT!
char c = cin.get();
if (c == '\n') { // read new line so you
break; // reached end of expression
}
op[i] = c;
// remember: (num) (op) (num) (op) (num) (newline)
|
At last stage we need to print them out. Its up to you, to put everything together.
If you still need help, you're welcome.
Maikel