Making an array with a user specified amount

Is there a way of making an array with an amount depending on how many "words" in a string that a user types?
Say I put 1+2+3+4 it would make an array with 7 spots.
Right now I can only do an equation with 1 number + or - or * or / by another number and that's it but if I make a super huge array, a lot of memory is wasted.

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

using namespace std;

void calculator()
{ 
    string answer = "";
    cout << "Would you like to use the calculator? (yes or no)" << endl;
    cin >> answer;
    if (answer == "yes"||answer == "Yes"){
    do {
    double num1 = 0.0, num2=0.0, result=0.0;
    int SELECTOR = 0;
    char Operator=' ';
    cout << endl << "Input an equation (e.g. 1+2) using operators +, -, * or /" << endl;
    cin >> num1 >> Operator >> num2;
    if (Operator == '+'){
    SELECTOR = 1;
    }else {if(Operator == '-'){
    SELECTOR = 2;
    }else {if(Operator == '*'){
    SELECTOR = 3;
    }else{
    SELECTOR = 4;
    }}}
    switch (SELECTOR) {
case 1:
    result = num1 + num2;
    break;
case 2:
    result = num1 - num2;
    break;
case 3:
    result = num1 * num2;
    break;
case 4:
    result = num1 / num2;
    break;
    }
    cout << num1 << " " << Operator << " " << num2 << " = " << result << endl;
    cout << "Would you like to continue? (yes or no)\n" << endl;
    cin >> answer;
    } while (answer == "yes"||answer == "Yes");
    }
    cout << "\nUnderstandable, have a great day!" << endl;
}

int main()
{
    calculator();
    return 0;
}
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
#include <iostream>

int main()
{
    std::cout << "Input an equation (e.g. 1+2) using operators +, -, * or /\n"
              << "end input with an == (eg. 1 + 2.3 * 24.5 / 32 ==\n" ;

    double result ;
    std::cin >> result ; // the first number is the initial result

    // evaluate the equation with equal precedence for all operators, and
    //                            associativity from left to right
    //                            ie. a+b*c-d is parsed as ( (a+b) * c ) - d
    char operation ;
    double number ;
    while( std::cin >> operation >> number ) // for each subsequent term (pair of operation,number)
    {
        switch(operation)
        {
            case '+' : result += number ; break ;
            case '-' : result -= number ; break ;
            case '*' : result *= number ; break ;
            case '/' : result /= number ; break ;
            default  :
                if( operation != '=' ) std::cout << "invalid operation '" << operation << "'\n" ;
                std::cin.clear(std::cin.failbit) ; // put the stream into a failed state to exit from the loop
        }
    }

    std::cout << "result == " << result <<  '\n' ;
}
@JLBorges Is there a way to display the equation they entered and then the answer instead of "result == "?
Other than that, this is really good and I didn't think about doing it that way!
Thank you for the help, though do you have any constructive feedback?
Read the input into a string and then use a string stream to extract the numbers and operators from it.

Something along these lines:

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

int main()
{
    std::cout << "Input an equation (eg. 1 + 2.3 * 24.5 / 32 ) using operators +, -, * or /\n"
              << "end input with a new line\n" ;
    std::string input ;
    std::getline( std::cin, input ) ; // read in the complete line

    // create a string stream to read from the input entered by the user
    std::istringstream str_stm(input) ;
    double result ; // // the first number is the initial result
    if( !( str_stm >> result ) ) // if input of even the first number failed
    {
        std::cout << "badly formed input '" << input << "'\n" ;
        return 1 ;
    }

    // the equation parsed so far (initially contains just the first number)
    // note: tellg() returns the next input position in the stream
    // characters up to tellg() have been parsed and extracted
    std::string equation = input.substr( 0, str_stm.tellg() ) ;

    // evaluate the equation with equal precedence for all operators, and
    //                            associativity from left to right
    //                            ie. a+b*c-d is parsed as ( (a+b) * c ) - d 
    char operation ;
    double number ;
    while( str_stm >> operation >> number ) // for each subsequent term (pair of operation,number)
    {
        switch(operation)
        {
            case '+' : result += number ; break ;
            case '-' : result -= number ; break ;
            case '*' : result *= number ; break ;
            case '/' : result /= number ; break ;

            default: // invalid operation
                std::cout << "input after invalid operation '" << operation << "' discarded\n" ;
                goto print_result ; // just print what had been successfully parsed earlier
        }

        // update the equation to be what has been successfully parsed so far
        equation = input.substr( 0, str_stm.tellg() ) ;
    }

print_result:
        std::cout << equation << " == " << result << '\n' ;
}
Wow, thank you! I'll just rip off your code then change it a bit and make it my own... Just kidding (or am I? △)
Topic archived. No new replies allowed.