How do I do this?

This is a discussion on How do I do this? within the C++ Programming forums, part of the General Programming Boards category; Is it possible (or practical) to do this without using exit() or goto? And do I need the default case, ...

  1. #1
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40

    How do I do this?

    Is it possible (or practical) to do this without using exit() or goto? And do I need the default case, since there is only one case I am worried about? I have a version using if(), would it be more efficient to use that instead? Do I need the break after exit() since the program will have ended anyway?
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int n;
    	int i;
    	printf("Please enter an integer\n");
    	scanf("%d",&n);
    	for(i=2;i<n;i++)
    	{
    		switch(n%i)
    		{
    		case 0:
    			printf("%d is not a prime\n",n);
    			exit(0);
    			break;
    		default:
    			break;
    		}
    	}
    	printf("%d is a prime\n",n);
    	return 0;
    }
    Thanks.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Code:
    int main()
    {
    	int n;
    	int i;
    	printf("Please enter an integer\n");
    	scanf("%d",&n);
    	for( i=2; i < n; ++i ) {
                if( n % i == 0 ) {
    		printf("%d is not a prime\n",n);
                    break;
                }
            }
    	if( i == n )
                printf("%d is a prime\n",n);
    	return 0;
    }
    Note: no error checking is done
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Something like this ?

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int n;
    	int i;
    
    	bool isprime = true;
    
    	cout<< "Please enter an integer\n";
    	cin >> d;
    
    	for( i=2; i<n; i++ )	// I think it only needs be n/2 really. 
    	{	// If a number is being divided by something that's over half its value, it can't be whole!
    
    		if ( ( n%i ) == 0 )
    		{
    			isprime = false;
    		}
    	}
    
    	switch ( isprime )
    	{
    		case true;
    			cout<< d << " is a prime!";
    			break;
    
    		case false;
    			cout<< d << "is not a prime number :(";
    			break;
    	}
    
    	return 0;
    }

    I haven't compiled it, so I don't know if it works. Why did you use printf and scanf?

    EDIT - so much for going to sleep Mario F.
    Last edited by twomers; 07-24-2006 at 05:14 PM.

  4. #4
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    Thanks to both of you. Especially Mario F. - my if() version was exactly like that, except I didn't think to put an if() for the bottom printf. And I used printf and scanf because I thought of this while doing Thinking in C by Bruce Eckel and he uses a lot of printf and scanf in the lecture.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Quote Originally Posted by twoners
    EDIT - so much for going to sleep Mario F.
    I am, dad!
    Just finishing some stuff now that I finally had the whole thing working. It may not be much, but it really felt good to finally understand about memory management and have put it to work
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You could always put in something to check if it's an even number so that you wouldn't have to go through the whole checking all the variable malarky

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    There's a pretty big mix up in variables in twomers' code, change all occurances
    of d to n, also in the switch cases, it should be

    case true:

    not

    case true;

    Other than those it works fine (assuming a well behaved user )

    EDIT: If you are reading "Thinking In C", why did you post in the C++ forum?
    Make sure you know what language you are trying to learn!!!
    Last edited by Richie T; 07-24-2006 at 05:33 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    I'm used to C++, and it works in C and C++, so I didn't figure it would matter.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Putting your code into a function would be something else to consider.
    Code:
    #include <cstdio>
    using namespace std;
    
    bool foo(int n)
    {
       for ( int i = 2; i < n; ++i )
       {
          if ( n % i == 0 )
          {
             return 0;
          }
       }
       return 1;
    }
    
    int main()
    {
       for ( int n = 1; n < 20; ++n )
       {
          printf("%d is %sa prime\n", n, foo(n) ? "" : "not ");
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21