While getting user input, I set the smallest and largest numbers input into their own variables, but for whatever reason they start out = to 0

While getting user input, I set the smallest and largest numbers input into their own variables, but for whatever reason they start out = to 0.

Code:

#include <iostream>

using namespace std;

int main()
{
    int num;
    string var;
    int sum = 0;
    int i;
    int largest = INT_MIN;
    int smallest = INT_MAX;
    int j = 0;
    int prime = 0;
    do {
        cout << "Please enter a series of numbers, press (Q or q) to process: ";
        cin >> num;
        if (cin.fail())
        {
            cin.clear();
            cin >> var;
            if (var != "Q" && var != "q")
            {
                cout << "Invalid input, try again" << endl;
            }

        }
        if (num > largest)
        {
            largest = num;
        }
        if (num < smallest)
        {
            smallest = num;
        }
        if (num == 0 || num == 1)
        {
            prime = prime;
        }
        else
        {
            for (i = 2; i <= num / 2; i++)
            {
                if (num % i == 0)
                {
                    j = 1;
                    break;
                }
            }
            if (j == 0)
            {
                prime++;
            }
        }
        sum += num;
        cout << "The corresponding element for the cumulative total sequence is: " << sum << endl;
        cin.ignore(sum, 'n');
    } while (var != "Q" && var != "q");
    cout << endl;
    cout << "Largest number: " << largest << endl;
    cout << "Smallest number: " << smallest << endl;
    cout << "How many prime numbers? " << prime << endl;
    cout << "Have a great day!" << endl;
}

Here is an example of the program being run. Program example

The smallest number here should be 8, and the issue is that it begins at 0. The same thing with the largest number. Program example #2

Answer

Your loop is testing the “num” variable even if the user inputs q or Q, adding an else statement else break; after the if (var != "Q" && var != "q") will fix it. For the future, always keep in mind that when the “if” function fails, it will move on to the next line, if you need to to not execute, you either need to break out of the loop or change the structure of your code.