Midterm help.

Create a the world’s simplest calculator. The program will add together all integers entered in by the user. It will stop adding when the user enters in a zero and print out the total.



Example
Welcome to the calculator that only performs addition
Enter in all of the numbers that you want to add together
Enter in 0 for your last number
3
4
5
0
The total is: 12

It's my first year and most of the time I don't know how to begin. I would appreciate all the help I can get.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>
#include <conio.h>
// conio.h is used for clrscr() and getch()

void main()
{
clrscr();
int a[100],i,sum=0;
a[0]=1;//initialize it to any non-zero integer value

cout<<"Welcome to the calculator that only performs addition\n";
cout<<"Enter in all of the numbers that you want to add together\n";
cout<<"Enter in 0 for your last number\n";
//you can cascade these too

for(i=1;a[i-1]!=0;i++)
{ cin>>a[i];
  sum=sum+a[i]; // also sum+=a[i], known as shorthands
}

cout<<"The total is: "<<sum;
getch();
}


cheers!
A better way you could do this would be to use a do while loop and only two variables instead of an array. For instance
1
2
3
4
5
6
7
8
9
10
11
12
 int main() {
    int input, sum = 0;

    cout << "Enter in all of the numbers that you want to add together" << endl;
    cout << "Enter in 0 for your last number" << endl;

    do {
           cin >> input;
           sum += input;
    } while (input != 0);
    cout << "And the sum of these numbers is: " << sum << endl;
}


of course this has no error checking but if it did it would not be the simplest calculator!
Last edited on
@ForTheReallys Corrrrrrrrrrrrrect! :D
Topic archived. No new replies allowed.