Very strange c++ behaviour!

Discussion in 'Software' started by Stoo_Pot, Nov 2, 2005.

  1. Stoo_Pot

    Stoo_Pot Private E-2

    Hi everyone,
    I'm trying to learn c++ from a book and was trying one of the 'simple' exercises to find the first 100 prime numbers using a function which tests whether one number is a multiple of another. As you'll see there's a cout << "anything" << endl; line that's been commented out. If I remove the comment marks and allow this line to become part of the code, the program works (except for the lots of lines saying "anything" between the prime numbers). However if I remove or comment out this line, I get the error message from the multiple function "bad parameters".
    Why on earth does this cout line make any difference?!


    #include <iostream.h>
    #include <math.h>

    // Declare function multiple
    int Multiple(const int&, const int&);

    int main(void)
    {
    // Prime number = number divisible only by itself and 1.
    int i=0, Check, Prime=1, Integer1=1, Integer2;

    while(i <= 5)
    {
    for(Integer2 = 1; (Integer2 <= Integer1); Integer2++)
    {
    Check = Multiple(Integer1, Integer2);
    // cout << "anything" << endl;
    if(Check == 1)
    {
    if((Integer1 != Integer2) && (Integer2 !=1))
    {
    Prime = 0;
    }
    }
    }
    if(Prime ==1)
    {
    cout << Integer1 << " is a prime number!" << endl;
    i++;
    }
    if(Prime != 1)
    Prime = 1;
    Integer1++;
    }
    }

    /*******************************************************
    * Function Multiple: Determines whether a the first *
    * parameter is a multiple of the second parameter. *
    *******************************************************/

    int Multiple(const int& A_REF, const int& B_REF)
    {
    if((A_REF>1000) || (B_REF>1000))
    {
    cerr << "Multiple: Error: Bad parameters" << endl;
    return 2;
    }
    else
    {

    int a = A_REF;
    int b = B_REF;
    int trial;

    for(int i=1;((trial != a) && (i<=a)); i++)
    {
    trial = i*b;
    }

    if(trial == a)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }
    }


    Thank for any help in advance
     
  2. Stoo_Pot

    Stoo_Pot Private E-2

    Sorry this should be easier to read:

    Code:
    #include <iostream.h>
    #include <math.h>
    
    // Declare function multiple
    int Multiple(const int&, const int&);
    
    int main(void)
    	{
    // Prime number = number divisible only by itself and 1.
    	int i=0, Check, Prime=1, Integer1=1, Integer2;
    	
    	while(i <= 5)
    		{
    		for(Integer2 = 1; (Integer2 <= Integer1); Integer2++)
    			{
    			Check = Multiple(Integer1, Integer2);
     //  	 cout << "anything" << endl;
    			if(Check == 1)
    				{
    				if((Integer1 != Integer2) && (Integer2 !=1))
    					{
    					Prime = 0;
    				   }
    				}
    			}
    		if(Prime ==1)
    			{
    			cout << Integer1 << " is a prime number!" << endl;
    			i++;
    			}    
    		if(Prime != 1)
    			Prime = 1;  		
    		Integer1++;
    		}
    	}
    
    /*******************************************************
    *   Function Multiple: Determines whether a the first  *
    *	parameter is a multiple of the second parameter.   *
    *******************************************************/
    
    int Multiple(const int& A_REF, const int& B_REF)
    	{
    	if((A_REF>1000) || (B_REF>1000))
    		{
    		cerr << "Multiple: Error: Bad parameters" << endl;
    		return 2;
    		}
    	else
    		{
    	
    		int a = A_REF;
    		int b = B_REF;
    		int trial;
    	
    		for(int i=1;((trial != a) && (i<=a)); i++)
    			{
    			trial = i*b;
    			}
    		
       	if(trial == a)
    			{
    			return 1;
    			}
    		else
    			{
    			return 0;
    			} 
    		}
    	}
    
    
     

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