C++ problem with function

Discussion in 'Software' started by mrudella, Oct 25, 2004.

  1. mrudella

    mrudella Private First Class

    Hi all,
    Once again, I have come back in need of some assistance with a problem that I can't figure out.
    Here is a description of the program that we have to complete:
    Demonstration of user-defined functions with bool variable. This program contains a loop that asks the user for a positive integer and determines whether or not the number is a prime number.
    I have to keep everything in main the same. My only problem is down below at the function definitions at the bottom of the program. If someone can figure out what else I have to add in down there, it would be greatly appreciated.
    Here is the code so far for my program:

    //Include Files

    #include <iostream>
    #include <string>

    using namespace std;


    // Declarations for user-defined functions

    bool TestPrime (int);

    // Main function to test boolean user-defined functions

    int main () {

    // Declarations

    int number; // potential prime number to test
    bool result; // result of test
    string response;


    // Display programmer comment block

    cout << "COMP 160 Mark Rudella" << endl;
    cout << "Section 2 x" << endl;
    cout << "Lab Exercise 3 October 22, 2004"<< endl;
    cout << endl << endl;

    // Let's read in values to test until the user enters 0


    cout << "This program will test a user-supplied integer" << endl;
    cout << "to determine if it is a prime number." << endl << endl;

    cout << "Would you like to test a number? (Y or N)";
    cin >> response;
    cout << response;
    cout << endl << endl;

    while (response != "N")
    {
    cout << "Enter Number to test: ";
    cin >> number;
    cout << number << endl<<endl;

    result = TestPrime(number);

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

    cout << "Would you like to test another number? (Y or N)";
    cin >> response;
    cout << response;
    cout << endl << endl;

    }

    return 0;
    }

    // User-Defined Function Definitions

    bool TestPrime(int n)
    {
    bool result;
    int x;
    int ctr(2);
    while (ctr <= x)
    {
    if (n % ctr = '0')

    {
    // Not sure what goes in here.
    }


    }

    }
     
  2. QuickSilver

    QuickSilver Corporal

    sorry - no compiler installed so i cant test this but... what is this line trying to achieve:
    int ctr(2);

    it looks like you are trying to declare a variable called ctr but the brackets don't need to be there; you wanting an array???
     
  3. QuickSilver

    QuickSilver Corporal

    also with x you're doing a comparison in your while loop against the varialbe x that hasnt been initialised... not going to cause errors - just unexpected results...
     
  4. glennk721

    glennk721 MajorGeek

    Hello, have uploaded a C++ Programing Guide to my webserver, its a PDF ebook,,,you will need a utility such as Winzip to open the file,and then A PDF File Viewer hope you find this helpfull , Glenn
     
    Last edited by a moderator: Nov 13, 2004
  5. QuickSilver

    QuickSilver Corporal

    Ok apologies for before - I was on the phone at the same time and realied that my answers probably werent a great deal of help... you didn't post what error message you were getting so it was hard to know where to start and I just have never got round to getting my C/C++ compiler configured since I last did a clean install...
    So here goes...

    int ctr(2);
    This line looks suspect to me. As I said it looks like you are trying to declare a variable called ctr (counter?) and initialise its value to be 2. However your syntax is a tiny bit out.
    Assuming that is what you want to do try the following line instead...
    int ctr = 2;
    This declares the integer variable and sets its value to 2.

    int x;
    This line, whilst syntactically correct, from the way your code progresses leaves you vulnerable to unexpected program behaviour.
    ok so you've declared x but you haven't said what its initialised value should be. Also, as far as variable names go, x isn't too useful...
    Looking at your code I would hazard a guess that the following might be more useful....
    int x = n;
    This is taking the parameter you have passed in from main and assigning it to the local variable x.

    if (n % ctr = '0')
    Just looking at this line too....
    if the result of n modular (ctr assigned the value of characterr 0) is true.....
    Yea a few syntax errors here...
    First of all you need 2 equals signs - see explanation below...
    Second you are trying to compare an integer to a character value. I believe removing the single quotes around the 0 will be better!
    Then I would throw an extra bracket around the modular calculation there so that there is no confusion about the order of precedence.
    if((n % ctr) == 0)

    I would probably consider a for loop for this purpose as opposed to a while loop... whilst it makes little odds in the big scheme of things and would achieve the same thing it might make things a little more readable...
    eg

    int loop_counter;
    for (loop_counter=2 ; loop_counter<n ; loop_counter++)
    {
    blah blah...
    }

    But thats just me... ;)

    Comparison/Assignment
    A single = sign translates to an assignment.
    So:
    int x = 2;
    should read integer x assigned the value 2.
    int y = 2;
    int x = y;
    integer x now holds the value of integer y.

    Double = sign translates to comparison.
    if x == y
    if the value of x is equal to the value of y.


    Does this make sense? I'll happily go over any more of that with you if you're still having trouble.

    Ta
     
  6. rmStar-R

    rmStar-R Private E-2

    Aside from the syntax problem already pointed out, you also need to look over the algorithm of testing prime numbers... ;)

    I would finally like to comment on the initialization:

    Code:
    int ctr(2);
    
    this is perfectly legal in c++.

    You would also like to name your tester function as: isPrime( int nnum ) so that you would call something like:

    Code:
    if( isPrime( number ) ){
      //some code.
    }else{
      //other code.
    }
    
    or better

    Code:
    cout << number << ( ( isPrime(number) )? " is a ":"is not a " ) << "prime number." << endl;
    
    cute?

    You might also want to check the lower and upper case response of your 'n' or 'N' so use tolower( response ) function.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds