Advanced calc project

Making an advanced calculator with the option to add several numbers to one operator, for example, 3 = x + y + z.
How can I prevent it from printing the answer after each input and only printing when the total amount is inputted?
Thank you in advance.

#include <iostream>
#include <conio.h>


using namespace std;





void add()
{
float sum = 0, n, i , a;

cout << "Enter the amount of numbers: \n";
cin >> a;
cout << "Enter the numbers individually below: \n";
for (i = 1; i <= a; i++){
cin >> n;
sum = sum + n;
cout << sum;
}

}
void sub ()
{
float sum = 0, n, n1, i, a;
cout <<"Enter the first number to subtract from: ";
cin >>n;
cout <<"How many numbers do you want to use? :";
cin >>a;
cout <<"Enter each number individually: \n";
for (i = 1; i <= a; i++)
{
cin >> n1;
sum = n - n1;
}
cout << sum;
}
void multi ()
{
float sum = 0, n , i ,a;
cout <<"Enter the amount of numbers: \n";
cin >> a;
cout << "Enter the numbers individually below: \n";
for (i = 1; i <= a; i++)
{
cin >> n;
sum = sum * n;
}
cout << sum;
}
void divi ()
{
float sum = 0, n , n1 , i, a;
cout <<"Enter the first number to divide: \n";
cin >>n;
cout << "How many numbers do you want to divide with: \n";
cin >>a;
cout << "Enter each number individually: \n";
for(i = 1; i <= a; i++)
{
cin >> n1;
sum = n / n1;
}
cout << sum;
}
void exit ()
{
getch ();
}



int main()

{
int opr;

cout << " 1. = + \n";
cout << " 2. = - \n";
cout << " 3. = * \n";
cout << " 4. = / \n";
cout << " 5. = Exit\n";

cout << "Choose one of the following operators : \n";
cin >> opr;
switch (opr)
{
case 1:
add();
break;
case 2:
sub();
break;
case 3:
multi();
break;
case 4:
divi();
break;
case 5:
exit ();
break;

}

}
Last edited on
Create a loop that alternates between entering numbers and operators, using an array to hold the sequence of numbers and operators entered. Exiting the input loop when the user enters '='. After the input is all entered and verified as correct "walk through" the array doing the calculations. When that is done display the final result.

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
it may be in your interest, depending on what else you need to make it do, to split the interface (cin and cout statements) from the work (add(), etc). having the interface in with the 'work' makes it hard to reconfigure the code to do new things: its locked into what it tells the user it will do.
Move the cout to after your input loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
void add()
{
	float sum = 0, n, i, a;

	cout << "Enter the amount of numbers: \n";
	cin >> a;
	cout << "Enter the numbers individually below: \n";
	for (i = 1; i <= a; i++) {
		cin >> n;
		sum = sum + n;		
	}
	cout << sum;           //  Move this to after your input lioop
}

Thanks for the help. Moving the cout outside the loop worked.

Sorry about the poor formatting. I'll keep it in mind until next time.
Topic archived. No new replies allowed.