#include <cstdlib> #include <iostream> using namespace std; #define li '\n' int total = 0; const int MAX = 10; int numArray[ MAX ]; int i = 0; int main () { int Oper; cout << "Please select an operation: " << li << li << "1. Addition" << li << "2. Subtraction" << li << "3. Multiplication" << li << "4. Division" << endl; cout << li << "Selection: "; cin >> Oper; cout << li << li; switch (Oper) { case 1: cout << "Please enter up to 10 numbers to add (Type Q to quit):" << endl; do{ cin >> numArray[ i ]; total += numArray[ i ]; }while( i == 10 ); break; } system("PAUSE"); return 0; } |
|
|
cin >> my_int;
) and user enters a string, istream failbit is set. istream object can be converted to a bool that equals the failbit state, so you can write
|
|
|
|
What I am actually going to do is make each operation a separate function. The switch() will call upon each function, just in case I want to use the functions later on in the code as I plan to get a little complex with it once I learn enough.
What I am actually going to do is make each operation a separate function. The switch() will call upon each function, just in case I want to use the functions later on in the code as I plan to get a little complex with it once I learn enough. see..now y like how you think :) .. that is the beauty of modular programing,you make functions so you can use them as often as you can,and save space.keep on the good job :P |
If you ask user to enter a number (cin >> my_int;) and user enters a string, istream failbit is set. istream object can be converted to a bool that equals the failbit state, so you can write 1 2 if(cin >> my_int) //user entered number else //user entered string You'll then want to see if user entered "Q". You must first clear failbit with istream::clear(), then ask for string input, then compare it to "Q". I'll write this code for you: 1 2 3 4 5 6 7 8 9 10 bool loop = 1; do{ if(! cin >> numArray[i]){ cin.clear(); string str; cin >> str; if(str == "Q") loop = 0;//you cant just break here. i still needs to be incremented once } i++;//you need this.. }while(i < 10 && loop);//note the '<' Now, about the code structure, you shouldn't ask for input inside the switch. You'll have to copy the same code four times then. You should to that before the switch. Then iterate and add/subtract/... the numbers. |
/* This program is a simple calculator allowing simple mathimatical operations such as multiplication, division, addition and subtraction. Currently, this is version 1.0, and operations are done by menu selection. Hopefully, in a later update, it will allow users to input raw equations to be solved directly without the need to choose from a menu. My plans for this program is to develop it until a full-use algebraic calculator. */ #include <cstdlib> #include <iostream> using namespace std; int total = 0; // The value to hold the answer to the equation. const int MAX = 10; // This constant is the max input allowed of the user. int numArray[ MAX ]; // This is the array that user input will be placed into, it will allow for 10 numbers only. int i = 0; // This is the variable that will be used to gather input from user, and place input into an numArray. double addition() // The addition function will add user input, allowing decimals. { do{ cin >> numArray[ i ]; total += numArray[ i ]; i++; }while(i < 10); cout << "\n" << total << endl << endl; // Returns the sum of the numbers entered in the array. return 0; } int addout() { cout << "You have chosen the addition operation. Please enter up to 10 numbers to add (type 'Q' to quit).\n"; cout << "\nNumbers: "; return 0; } int main () { int Oper; cout << "Please select an operations:\n"; cout << "1. Addition\n"; cout << "2. Subtraction\n"; cout << "3. Multiplication\n"; cout << "4. Division\n"; cout << "\nChoice: "; cin >> Oper; cout << endl; switch(Oper) { case 1: addout(); addition(); break; } system("PAUSE"); return 0; } |
|
|