Broken for loop

I only started learning C++ recently and am a complete beginner. This is a simple problem I am doing for practice to solidify my understanding. The problem is that my for loop is not ending after the appropriate number of iterations like it should. No matter the number I input for N, the for loop continues to run and ask for new input long after it should. I have never had this problem before, so there is probably some small mistake that I am not catching. Can any of you guys see what the problem might be?

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
  /*Given a list of N integers, find its mean (as a double), maximum value, minimum value,
and range. Your program will first ask for N, the number of integers in the list, which the
user will input. Then the user will input N more numbers.*/

#include <iostream>
using namespace std;

int main()
{
	int N;
	double min,max=0,sum=0,mean,range,a;
	cout<<"How many numbers do you wish to input?\n";
	cin>>N;		//Number of numbers
	
	cout<<"Input "<<N<<" numbers: \n";
	
	for(int i=0;i<N;++i){
		cin>>a;		//input a number
		sum+=a;		//sum of all numbers (divide by N for mean)
		if(a>max){
			max = a;}	//if a is larger than prev. max then new max will be value of a.
		if(i=0){
			min=a;}		//sets first number that is input equal to min.
		if(a<min){
			min=a;}		//if subsequent numbers are less than the first, they will be new min.
	}
	
	range = max-min;
	mean=sum/N;
	
	cout<<"The mean is "<<mean<<".\n";
	cout<<"The min is "<<min<<".\n";
	cout<<"The max is "<<max<<".\n";
	cout<<"The range is "<<range<<".\n";
		
	return 0;
}
on line 22 if(i=0){ // = is assignment operator you are using an asssignment operator insted of logical operator to check if the situation is true or not.So every time the compiler reads the line i becomes 0 .So loop rus again and again.So use ==// logical operator if(i==0){ // LINE 22
Last edited on
Line 22: if(i=0){
You've used assingment rather than comparison.
Wow, I knew it would be something small like that. Thanks guys!
Topic archived. No new replies allowed.