jammah
jammah

Reputation: 1

Why im getting an infinite loop and how could i solve it?

Im pretty new to programming so i probably made lots of mistakes helllpppp

Im writing a program where user should enter number of integers , then the program should print out how many even numbers are there in those integers and there sum

#include <iostream>
using namespace std;
int main()
{

    int n=3;
    int counteven;
    int count=1;
    int sum=0;
    cout<<"Enter the number of integers: ";
    cin>>n;
    while(count<=n)

        cin>>n;
        count++;
        if (n%2==0);
        { 
        counteven;
        sum=sum+n;

        cout<<"The even numbers are:"<<counteven<<endl;
        cout<<"The sum of eve numbers is:"<<sum<<endl;}
        system("pause");
        return 0;
        }

thankss

Upvotes: 0

Views: 24

Answers (1)

Umibozu
Umibozu

Reputation: 177

It looks like you don't have any braces ({}) for your while loop which is probably causing it to loop over the line:

while(count<=n)

An example of a correct while loop (taken from here):

// custom countdown using while
#include <iostream>
using namespace std;

int main ()
{
  int n = 10;
  while (n>0) {
    cout << n << ", ";
    --n;
  }
  cout << "liftoff!\n";
}

Upvotes: 1

Related Questions