Home > Software engineering >  Program Fails to output greatest integer
Program Fails to output greatest integer

Time:09-21

I am writing a program that takes multiple user inputs, and counts how many prime numbers there are, and which number is the greatest and smallest of the inputs. However, I am stuck. when I run the code, it thinks every new input is the greatest number. for example, if I enter 1, 2, 3, 2... It says the greatest number is 2. It takes the last integer inputted and thinks that is the greatest number. I have no started on the lease common number yet, but if I am able to understand how to get the greatest number, I bet I can get the least. Thanks guys!

#include <iostream>
using namespace std;

int main() {

    int startstop = 1;

    cout << "start program?" << endl;
    int begin = 0;
    int primecounter = 0;
    
    cin >> begin;

    if (begin >= 0) {
        while (startstop != 0) {
            cout << "enter any number, or press Q to process." << endl;
            int oldnum = 0;
            int userinput=0;
            int newnum = userinput;
            int greatestnum = 0;
            int greatestcounter = 0;
            cin >> userinput;
           
            int x;          
            bool is_prime = true;
            if (userinput == 0 || userinput == 1) {
                is_prime = false;
            }

            // loop to check if n is prime

            for (x = 2; x <= userinput / 2;   x) {
                if (userinput % x == 0) {
                    is_prime = false;
                    break;
                }
            }

            if (is_prime) {
                primecounter  ;
            }

            //check if input is greater than previous input

            if (greatestnum > userinput) {
                greatestnum = userinput;
            }

            cout << "prime count: " << primecounter << endl;
            cout << "greatest num: " << greatestnum << endl;
            
       
            userinput = 0;

        }
    

        return 0;
    


    }
}

CodePudding user response:

greatestnum is only assined if it's greater than the user input. Since it's initialized to 0, the if statement,

if (greatestnum > userinput) {
    greatestnum = userinput;
}

will be false unless the user input is less than zero. If you want to make it the greatest from all user inputs, flip it to < and move the int greatestnum = 0; to right above the if (begin >= 0) {.

if (greatestnum < userinput) {
    greatestnum = userinput;
}

CodePudding user response:

#include <iostream>
using namespace std;

int main() {

  int startstop = 1;

  cout << "start program?" << endl;
  int begin = 0;
  int primecounter = 0,greatestnum = 0,oldnum = 0,userinput = 0;
  
  cin >> begin;

  if (begin >= 0) {
     while (startstop != 0) {
           cout << "enter any number, or press Q to process." << endl;
           cin >> userinput;
        
           int x;          
           bool is_prime = true;
           if (userinput == 0 || userinput == 1) {
              is_prime = false;
           }

           // loop to check if n is prime

           for (x = 2; x <= userinput / 2;   x) {
              if (userinput % x == 0) {
                 is_prime = false;
                 break;
              }
           }

           if (is_prime) {
              primecounter  ;
           }

           //check if input is greater than previous input

           if (userinput > oldnum) {
              greatestnum = userinput;
              oldnum = userinput;
           }

           cout << "prime count: " << primecounter << endl;
           cout << "greatest num: " << greatestnum << endl;
           
           userinput = 0;
     }
     return 0;

  }
}

Initialize 'oldnum=0' , 'userinput=0' and 'greatestnum=0' outside the loop. And there were some error in logic. If userinput is greater the old num the update greatest num=userinput and oldnum=userinput.

  •  Tags:  
  • c
  • Related