Every time I run this program, I get the number the user inputted printed back on the screen infinite amount of times. According to my CS teacher, he wants us to use basics, no arrays or strings. The user is suppose to type in several numbers followed by -999. Say I type in 7 10 14 13 -999 into the line, and press enter, I get 7 repeating on the screen until I press Ctrl + C to exit. It's suppose to go through the loop, print the number out if it's an even and then check the system input stream and grab the next number on it. But it does this...any help would be appreciated. Ohh sorry for the poor code structure.

#include <iostream>
using namespace std;
const int SENTINEL = -999;

// This program is suppose to have the user input a list of numbers followed by
// -999 as the sentinel. It will take the numbers the user inputted and check 
// whether or not they are even. If they are, it will count how many are even, 
// and output that number. It will also output the total data set size

int main () {
    
    cout << "List a set of numbers followed by the number -999" << endl;
    
    int x;
    int ct = 0;
    int en = 0;
    int mod = 0; //
    
    cin >> x;
    // I put the cin here so it can run it through the while condition before
    // the loop body. That way, when the user enters -999, ct won't increment 
    // by 1
    
    while (x != SENTINEL) {
          ct++;
          mod = x % 2;
          if (mod == 0) {
             en++; 
             cout << x;
          }
          
          }
          
    cout << "The total amount of even numbers were: " << en << endl;
    cout << "The total amount of numbers in the data set were: " << ct << endl;
    
    return 0;
    
}

You need to watch your brace style. (The while end-brace should be at the same indent as the while keyword, based on your other braced blocks.)

The loop never ends because you forgot to cin >> x; at the end of your loop also.

Hope this helps.

Problem solved. Thanks for the help!

Be a part of the DaniWeb community

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