I'm writing a program for my C++ class that goes off of a previous assignment. The previous assignment asked the user how many numbers they wanted to enter, asked them to then enter the numbers and then found the sum, average, largest and smallest number from the inputted numbers. The current assignment is asking us to take what we had for code in the previous assignment, but instead, ask the user for a number from 1 to 100, ask if they would like to enter another number (y or n), if yes, they enter another number, if no, the loop ends. Then, state how many numbers the user input, find the sum, average, largest, smallest of the inputted numbers. I'm having trouble figuring out the code. Heres what I had for the previous assignment:

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int UserInput, number, i;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
cout << "How many numbers do you wish to enter? ";
cin >> UserInput;

for (i = 0; i < UserInput; i++)
{
cout << "Enter a number: ";
cin >> number;
sum = sum + number;
average = float(sum)/UserInput;
if (number > largest)
largest = number;
if (!i || smallest > number)
smallest = number;
}

cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;

getch();
return 0;
}``````

and this is what I currently have for the newest assignment:

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int UserInput, number, i;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
cout << "Enter a number from 0 to 100: ";
cin >> UserInput;

for (i = 0; i < UserInput; i++)
{
cout << "Enter a number: ";
cin >> number;

sum = sum + number;
average = float(sum)/UserInput;
if (number > largest)
largest = number;
if (!i || smallest > number)
smallest = number;
}

cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;

getch();
return 0;
}``````

Note: We are not supposed to change much from the previous assignment.

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int number, i;
int count = 0;
char key;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
cout << "Enter a number from 0 to 100: ";
cin >> number;

for (i = 0; i < number; i++)
{
cout << "Would you like to enter another number (y or n)? ";
cin >> key;

cout << "Enter a number: ";
cin >> number;

sum = sum + number;
average = float(sum)/number;
if (number > largest)
largest = number;
if (!i || smallest > number)
smallest = number;
}

cout << "You've entered " << count << " numbers. ";
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;

getch();
return 0;
}``````

Am I heading in the right direction?

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int number, i;
int count = 0;
char key;
int sum = 0;
float average = 0;
int largest = 0;
int smallest = 0;
cout << "Enter a number from 0 to 100: ";
cin >> number;

for (i = 0; i < number; i++)
{

do
{
cout << "Would you like to enter another number (y or n)? ";
cin >> key;
key = tolower(key);
} while ( ! ( key == 'y' || key == 'n' ));

cout << "Enter a number: ";
cin >> number;

sum = sum + number;
average = float(sum)/number;
if (number > largest)
largest = number;
if (!i || smallest > number)
smallest = number;
}

cout << "You've entered " << count << " numbers. ";
cout << "The sum is: " << sum << endl;
cout << "The average is: " << average << endl;
cout << "The largest is: " << largest << endl;
cout << "The smallest is: " << smallest << endl;

getch();
return 0;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.