Okay this assignment is to prompt user for positive integer, compute and display the following: sum, average, and the largest input. It should terminate when a negative value is entered. Okay so I run the program, and it terminates when i plug in a negative number, but it still uses that negative as a value of 1 to add to the sum , and messes up my sum and average. Any help?
Code:
// Assignment11Smallest.cpp : Defines the entry point for the console application.
//
//terminate if integer is not positive
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main (void)
{
int numberofinputs,large,x;
double sum=0,average;
numberofinputs=0;
x=1;
large=x;
while (x>=0)
{
sum=sum+x;
numberofinputs=numberofinputs+1;
cout<<"\n Enter Data"<<endl;
cout<<"\n Terminate input by entering"<<"a negative number"<<endl;
cin>>x;
if (x>large)
large=x;
}
cout<<"number of inputs="<<numberofinputs<<endl;
cout<<"sum of integers="<<sum<<endl;
average=(sum)/(numberofinputs);
cout<<"average="<<average<<endl;
cout<<"Largest Value="<<large<<endl;
}