Assignment 7

Due Monday 22 October at 1am

This is a looping program to add or multiply a list of numbers typed by the user. The user can enter a string input for their choice and we will check that they select either ‘add’ or ‘multiply’.

  • In the first sample run, the user chooses to add numbers.

    Would you like to add or multiply? add
    Enter one number per line, 0 to stop:
    3
    5
    15
    1
    8
    0
    Thank you. The sum is 32.
  • In this example, the user multiplies. Note that the final zero cannot contribute to the product, or else the answer would always be zero.

    Would you like to add or multiply? multiply
    Enter one number per line, 0 to stop:
    2
    3
    1
    6
    0
    Thank you. The product is 36.
  • In this example, the user tries to enter something other than ‘add’ or ‘multiply’, and gets an error message.

    Would you like to add or multiply? subtract
    Sorry, I don't know how to subtract.
    Would you like to add or multiply? logarithm
    Sorry, I don't know how to logarithm.
    Would you like to add or multiply? shut up!
    Sorry, I don't know how to shut up!.
    Would you like to add or multiply? add
    Enter one number per line, 0 to stop:
    1
    2
    3
    0
    Thank you. The product is 6.

You may want to practice break to snap out of a loop from the middle. I think it’s most helpful in the loop that does error checking.

In order to read a string input from the user:

    cout << "What's for lunch? ";
    string choice;
    getline(cin, choice);

and then you can compare the string to literal strings using ==:

    if(choice == "pizza")
    {
        // etc...
    }