calculator

This is what I have so far...I'm not entirely sure how to use the do...while. I want the user to be able to enter up to 10 numbers after selecting an operation. I want it to goto the addition face even if the user enters less than 10 numbers, or quit and go back to the menu by type 'Q'.


#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;
}
Hy..well here is what y managed to do with you're code,but before y would like to give you some tips(you don't need to apply them if you don't want but,for me it helps a lot to read and understand the code better,instead of having long lines and using that #define \n,when you have other options that complicate you're code.).you can also try to arrange you're code so that it looks better,for you and for others.

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
int total = 0;
const int MAX = 10;
int numArray[ MAX ];

int main ()
{
	int Oper,i=0; // try to make the variables that you use for counting in the main

	cout << "Please select an operation:\n";
	cout<< "1. Addition\n";
	cout<< "2. Subtraction\n";
	cout<< "3. Multiplication\n";
	cout<< "4. Division\n";

	cout << "\nSelection: ";
	cin >> Oper;

	switch (Oper){
		case 1 : cout << "Please enter up to 10 numbers to add (Type Q to quit):\n";
			do{
				cin >> numArray[ i ];
				total += numArray[ i ];
				i++;  //you must increment i each time,so the loop does not go forever
			}while( i < 10 );
			/* This is the corect condition so that you enter 10 numbers.
			   Using == is wrong,that means repeat while i is 10,
			   but i will start with 0, 0==10 ??? no,so the loop will stop.
			   You can also use != ,that means repeat while i is not 10,
			   so the loop wil go from 0 until 10,then the condition i(10)!= 10 is false.
			*/
			cout << total <<endl;
			break;
	}

	system("PAUSE");
	return 0;
}


the other cases try to do them you're self..the part with the quitting and returning to the menu,y am not sure about it.y don't now how to make a quit after you entered in the switch.y will try to make something tomorrow.y'm a beginner so y don't have all the answers in my head :)..hope this helps.
Last edited on
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.
Thanks. 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. I thank you both again...despite the numerous different suggestions, I have learned a lot.
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
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


Thanks!

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.


Now, I'm kinda confused by that (as to where to put it)...but this is what I have thus far, and it is working so far:

/*

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;
}


Now, I guess maybe it is my compiler, but it seems to quit even without have anything declaring 'Q'...what I mean is if I were to enter Q instead of numbers for numArray, it breaks, or if I enter Q within the array even after entering the numbers, it will evaluate and add those numbers together, then break. Is that good or bad? I originally did and if/then statement, but then found that that might be a little too much, so I did while(i < 10 || i == 'Q'). But, after experimenting a little, by just wanting to see if I didn't even specify the 'Q' anywhere, I removed it. Blow in the hole, it acted as if I never took it out.

As for your code...I don't know where to put that.
A good structure of your program could look like this:
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
void get_input(){
    //pretty much the code I posted
    //note that you have to save the i variable to know how many numbers were entered
}
int add(){//and other math functions..
    int result = 0;//this function does not involve any input, so that you can  
                   // perform various calculations with the same input.
    for(int i = 0; i < /*how many numbers were entered*/; i++) result += numArray[i];
    return result;
}

int main(){
    int state = 1;//1 - input, 2 - add, 3 - subtract, 4 - multiply, 5 - divide, 6 - quit
    while(state != 6){//so that you can repeat everything
        switch(state){
            case 1: get_input(); break;//input is in this switch so that you can enter different values
            case 2: cout << add(); break;
            //other math functions..
            default: cout << "invalid input"; break;
        }
        cout<<"what do you want to do?";
        cin >> state;//it would be better to use strings here, but that is also
                     //slightly more complex.
    }
    return 0;
}
Topic archived. No new replies allowed.