You are here:

C++/C++ question

Advertisement


Question
If you have a moment to look at the code I would appreciate it. The program will run, but there is definitely a problem.

It is a related to a homework lab, but I have written a program that will run.  The problem is that there is definitely room for improvement.

For example...I can see three problems, but I have been unable to resolve them without creating errors (that will not allow the program to compile).

1-If you start by entering a number>0 there is an infinite loop.

2-The program contains an "if" condition that states
(if (number <=0)) then cout a message to tell the user to enter a non-negative number.  If you enter any number (neg, pos, or zero) on the second attempt then the program works fine.

3-If you enter zero or negative on first attempt, then I wanted the conditional loop to repeat until the user enters a positive number...how do I repeat the loop ANY time that number<=0...but proceed to the prime number calculation for number >0 on first attempt?

I tried using google to gain some insight, but there are so many variations to the prime number program (and none are close enough to my program to provide anything meaningful).  I am a novice (as you will readily discern from my program), but I have definitely given it my best effort, yet I am unable to make any progress over the past couple of day.  I have included the code below.  If you are inclined to take a look, I would definitely appreciate your input.  

Sincerely,
Jacqueline

Here is the code:

/*Description: Program allows the user to enter an integer >1 and tests to see if this integer is a prime number.*/

#include <iostream>
#include <cmath>
using namespace std;

int number, test_int, is_prime;

int main()
{
is_prime = true;
cout << "Enter an integer to test if it is a prime number: ";
cin >> number;

if (number <=0)

cout <<endl <<"You must enter a number greater than or equal to 1... " <<endl
<<"Please enter a positive integer to test if it is a prime number: ";
cin >> number;

test_int = 2;
while (test_int <= sqrt(static_cast<double>(number)))
{
if (number % test_int == 0)
is_prime = false;
test_int++;
}

if (is_prime)
cout << "The integer is a prime number." <<endl <<endl;
else
cout << "The integer is not a prime number." <<endl <<endl;

return 0;
}

Answer
One way to wait for input is to set the number to make the loop pass the first time.  Some people double up the code outside the loop and then at the bottom of the loop but I think less code is better.

number = 0;
while(number<=0)
 {
 cout << "Enter an integer to test if it is a prime number: ";
 cin >> number;

 if (number <=0)
   cout <<endl <<"You must enter a number greater than or equal to 1... " <<endl <<"Please enter a positive integer to test if it is a prime number: ";
 }

This will loop once and repeat while 0 or < 0 is entered.  Some people use a while(1) and use

if(number>0)
 break;

It doesn't matter as long as you loop while input is bad.

You want to add is_prime to this:

while (is_prime && test_int <= sqrt(static_cast<double>(number)))

A for statement might be an improvement as is assigning to is_prime without an if statement:

for(is_prime=true, test_int=2;is_prime && test_int <= sqrt(static_cast<double>(number));test_int++)
 is_prime=number % test_int != 0;

Good luck with it.  It looks good otherwise,

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.