> Need help with coding with C++?

Need help with coding with C++?

Posted at: 2014-12-18 
Here are some suggestions for you:

#include

#include

#include

using namespace std;

enum { sum = 1, difference, product, quotient };

int getInt(int minValid = LONG_MIN, int maxValid = LONG_MAX);

int main(int argc, char *argv[]) {

? ? int first_number, second_number, choice;

? ? cout << "Enter " << sum << " for sum, " << difference

? ? ? ? ? ? ? << " for difference, " << product << " for product or "

? ? ? ? ? ? ? << quotient << " for quotient: " ;

? ? choice = getInt(sum, quotient);

? ? cout << "Please enter your first number: ";

? ? first_number = getInt();

? ? while (true) {

? ? ? ? cout << "Please enter your second number: ";

? ? ? ? second_number = getInt();

? ? ? ? if ((choice == quotient) && (second_number == 0)) {

? ? ? ? ? ? cout << "Sorry, you can't divide by zero" << endl << endl;

? ? ? ? } else {

? ? ? ? ? ? break;

? ? ? ? }

? ? }

? ? if (choice == sum) {

? ? ? ? cout << "The sum is: " << first_number + second_number;

? ? }

? ? if (choice == difference) {

? ? ? ? cout << "The difference is: " << first_number - second_number;

? ? }

? ? if (choice == product) {

? ? ? ? cout << "The product is: " << first_number * second_number;

? ? }

? ? if (choice == quotient) {

? ? ? ? cout << "The quotient is: " << static_cast(first_number) / second_number;

? ? }

? ? cout << endl << endl;

? ? return 0;

}

int getInt(int minValid, int maxValid) {

? ? stringstream ss;

? ? string line;

? ? bool inputOk = false;

? ? int n;

? ? do {

? ? ? ? getline(cin,line);

? ? ? ? ss.clear(); ss.str(line);

? ? ? ? if ((!(ss >> n)) || (n < minValid) || (n > maxValid) || (ss.good())) {

? ? ? ? ? ? cout << endl << "invalid input, try again" << endl << "> ";

? ? ? ? } else {

? ? ? ? ? ? inputOk = true;

? ? ? ? }

? ? } while (inputOk == false);

? ? return n;

}

#if 0

Sample run:

Enter 1 for sum, 2 for difference, 3 for product or 4 for quotient: 4

Please enter your first number: 3

Please enter your second number: 0

Sorry, you can't divide by zero

Please enter your second number: 4

The quotient is: 0.75

#endif

Yeah you weren't really clear there..

Okay...what do you need help with?