sum, mean, largest, second largest number for an unknown number of integers (what is wrong??)

I'll write a program that asks the user for an (unknown) number of integers(positive and negative) and print mean and the sum of them. series should end with '0 '.
The program should also print the largest and second largest of the input, and will consist of a number of functions. (my doesn't, help pleease)

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
	{

  int inputnumbers, second_max=0, max=0, sum=0;
	int n=0;
	double mean;
	cout << "enter some integers: ";
    cin >> inputnumbers;
     while(inputnumbers != 0) 
    {
		sum += inputnumbers;
        cin >> inputnumbers;
        n++;
        if(inputnumbers >= max )
		{
            second_max = max;
            max = inputnumbers;
        }
		else if(inputnumbers >= second_max)
		{
            second_max = inputnumbers;
        }
    }
	mean = (double) sum / n;
     cout << "Sum of the numbers you entered is: "<< sum << endl;
	 cout << "mean of the input is: " << mean << endl;
	 cout << "the largest number you entered is: " << max << endl;
	 cout << "the second largest number you entered is: " << second_max << endl;

	}
_getch();
return 0;
}


expected output:

 enter some integers: 44 15 6 -4 3 11 -7 9 0
 Sum of the numbers you entered is: 77
 mean of the input is: 9.625
 the largest number you entered is: 44
 the second largest number you entered is: 15


my output:

 enter some integers: 44 15 6 -4 3 11 -7 9 0
 Sum of the numbers you entered is: 77
 mean of the input is: 9.625
 the largest number you entered is: 15
 the second largest number you entered is: 11


what am i doing wrong?? can somebody please help me fix it? :)

/S
Last edited on
You are loosing the first input
1
2
3
4
5
 cin >> inputnumbers;
     while(inputnumbers != 0) 
    {
		sum += inputnumbers;
        cin >> inputnumbers; //<--- 
Use while( cin>>input and input not_eq 0 ) to get the numbers

¿why the weird indentation?
Last edited on
Ok thanks, i'll try.

(not done yet, therefore the weird layout. shall rewrite it when it works...)

S
i don't get it, how do you mean? :S
where should i write what and how?
he simply says that just delete the codes corresponding to lines 24,25,26 &27 nd rerun.....ull get wat u wish................ :)
1
2
3
4
5
6
7
8
/*    cin >> inputnumbers;
     while(inputnumbers != 0) 
    {
		sum += inputnumbers;
        cin >> inputnumbers;*/
     while( cin>>inputnumbers and inputnumbers not_eq 0 ){
         sum += inputnumbers;
         //rest of the code 


(not done yet, therefore the weird layout. shall rewrite it when it works...)
You should let your editor to manage indentation.
do you mean? dsnt wrk. gah
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main ()
{
	{
		int inputnumbers, second_max=0, max=0, sum=0;
		int n=0;
		double mean;
		cout << "enter some integers: ";
		while( cin>>inputnumbers, inputnumbers != 0 )
		{
			sum += inputnumbers;
			cin >> inputnumbers;
			n++;	
			if(inputnumbers >= max )
			{
            second_max = max;
            max = inputnumbers;
			}
			else if(inputnumbers >= second_max)
			{
				second_max = inputnumbers;
			}
		}
		mean = (double) sum / n;
		cout << "Sum of the numbers you entered is: "<< sum << endl;
		cout << "mean of the input is: " << mean << endl;
		cout << "the largest number you entered is: " << max << endl;
		cout << "the second largest number you entered is: " << second_max << endl;
	}
	_getch();
	return 0;
}


(WARNING: conversion from looser to doublelooser; possible loss of IQ)
Right now?
thnk it works 0.o
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
	{
  int inputnumbers, second_max=0, max=0, sum=0;
	int n=0;
	double mean;
	cout << "enter some integers: ";
    while( cin>>inputnumbers, inputnumbers != 0 )
	{
         sum += inputnumbers;

        n++;
        if(inputnumbers >= max )
		{
            second_max = max;
            max = inputnumbers;
        }
		else if(inputnumbers >= second_max)
		{
            second_max = inputnumbers;
        }
    }
	mean = (double) sum / n;
     cout << "Sum of the numbers you entered is: "<< sum << endl;
	 cout << "mean of the input is: " << mean << endl;
	 cout << "the largest number you entered is: " << max << endl;
	 cout << "the second largest number you entered is: " << second_max << endl;

	}
_getch();
return 0;
}
Topic archived. No new replies allowed.