Home > Mobile >  Code involving counting prime numbers stops working once a composite number is input. C
Code involving counting prime numbers stops working once a composite number is input. C

Time:09-30

Code involving counting prime numbers stops working once a composite number is input. C .

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;
            }
            else
            {
                break;
            }

        }
        if (num > largest)
        {
            largest = num;
        }
        if (num < smallest)
        {
            smallest = num;
        }
        if (num == 0 || num == 1)
        {
            j == 1;
        }
        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;
}

Example of program's output:

Please enter a series of numbers, press (Q or q) to process: 2
The corresponding element for the cumulative total sequence is: 2
Please enter a series of numbers, press (Q or q) to process: 7
The corresponding element for the cumulative total sequence is: 9
Please enter a series of numbers, press (Q or q) to process: 4
The corresponding element for the cumulative total sequence is: 13
Please enter a series of numbers, press (Q or q) to process: 13
The corresponding element for the cumulative total sequence is: 26
Please enter a series of numbers, press (Q or q) to process: 17
The corresponding element for the cumulative total sequence is: 43
Please enter a series of numbers, press (Q or q) to process: q

Largest number: 17
Smallest number: 2
How many prime numbers? 2
Have a great day!

The first two inputs are prime numbers and work properly, but once I input a composite number (4) the rest of the prime numbers do not get counted as a prime number.

CodePudding user response:

You set j = 0 outside of your d-while loop. A prime number does not change the value of j, but a composite number does. Once you put in a composite number and j is changed to be something other than 0, that value stays as it is for the next iteration of your do-while loop.

Solution: move int j = 0; to be inside the do-while loop so that it is reset for each number.

In general you should declare variables in as small/local of a scope as possible.
So while var, smallest, largest, and prime are all used outside the do-while loop, your variables num, sum, i, and j are only used within the do-while loop and so should only be declared within the loop.
Additionally, i is only used in the for-loop and so should be declared only there: for (int i = 2; i <= num / 2; i )


Also, not relevant to your question, but I noticed that you have this:

if (num == 0 || num == 1)
{
    j == 1;
}

and I'd like to point out that the statement j == 1; is useless. It checks if j is 1 and returns true or false, which then is not used.
I assume you meant this:

if (num == 0 || num == 1)
{
    j = 1; //assignment, not check
}
  •  Tags:  
  • c
  • Related