Ok, how would I get my MS-DOS prompt calculator to allow the user to inter multiple integers, and then add/multiply/etc them together? I've done this before, but I do not remember how, because it was SO long ago, and I forgot most of my C++.
#include <iostream>
usingnamespace std;
int main()
{
int total = 0;
constint MAX = 10;
int numArray[ MAX ];
for( int i = 0; i < MAX; i++ )
{
cout << "Enter integer: ";
cin >> numArray[ i ];
total += numArray[ i ];
}
cout << "\nThe total is " << total;
return 0;
}
Here's a simple program that computes the sum of 10 integers, the integers entered by the user a stored in an array, every time the user enters an integer into the array, that number is added to total. You could replace "total += numArray[ i ]" with "total *= numArray[ i ]" to multiply the integers.
Ok, looks good except for one thing...copying your program directly results in the the program repeating the inquiry to enter an integer for the number of integers I enter. For example...if I did 2 integers it would return "Enter integer:" twice more but not give any results. If I enter 10 integers per MAX, it would return "Enter integer" 10 times, plus give the results.
#include <iostream>
usingnamespace std;
int main()
{
int total = 0;
int num;
cout << "Enter integer: ";
cin >> num;
while( num != -1 )
{
total += num;
cout << "Enter integer: ";
cin >> num;
}
cout << "\nThe total is " << total;
return 0;
}
This program will keep prompting you for input until you enter "-1" (the sentinel) which causes the program to break out of the loop and display the total of the integers you've entered. This way you can add any number of integers.