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 ();
}
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;
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.
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.
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
}