OK, I finally figured out command line arguments... my program is called prime, in the console i need to type "prime 7" and the program will tell me if its a prime or not.

heres my code:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;


int main(int argc, char *argv[])
{
   const unsigned long arraySize = 1000000;
   bool a[arraySize];
   unsigned long num1;
   unsigned long num2;
   unsigned long num3;
   unsigned long counter = 0;
   unsigned long primes = 0;
   char z;
   unsigned long answer = 0;
  
//Initializing elements to 1 
        
for (int i = 0; i < arraySize; i++)             
{
   a[i] = 1;
}       
// For array subscript 2, all elements beyond 2 in the array that
// are multiples of 2 will be set to zero ; for array subscript 3, 
// all elements beyond 3 in the array that 
// are multiples of 3 will be set to zero


for (int i = 2; i * i < arraySize; i++)
{
    if (a[i])
       for (int j = i + i; j < arraySize; j += i)
          a[j] = 0;
} 
// if user enters more than 3 numbers or 0 numbers
// program should output useful information
// and title and author

if ((argc > 4) || (argc == 1))
{
 cout<<"This is Programming Assignment #5"<<endl;
 cout<<"THE SIEVE OF ERATOSTHENES"<<endl;
 cout<<"by Jeremy Rice"<<endl;
 cout<<endl;
 cout<<"Usage:"<<endl;
 cout<<"\tprime num1 - determines if num1 is a prime"<<endl;
 cout<<"\tprime num1 num2 - displays primes between numbers."<<endl;
}

// if user enters one number
// program should determine if that number is a prime

if (argc == 2)
{  
   if (a[argc] == 1)
   {   
       num1 = strtoul(argv[]);
       counter++;
       cout<<"This is a prime number."<<endl;  
   }
   else
   cout<<"This is not a prime number."<<endl;
}



// if user enters 2 numbers
// program, should calculate the prime numbers 
// between the two numbers

if (argc == 3)
   {
         
   }



return EXIT_SUCCESS;
}

heres the part I need help with

// if user enters one number
// program should determine if that number is a prime

if (argc == 2)
{  
   if (a[argc] == 1)
   {   
       num1 = strtoul(argv[]);
       counter++;
       cout<<"This is a prime number."<<endl;  
   }
   else
   cout<<"This is not a prime number."<<endl;
}

what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?

Recommended Answers

All 7 Replies

I finally figured out command line arguments

what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?

A contradiction!

If you enter prime 7 as the command-line argument then the word prime will be in argv[1] and '7' is in argv[2], making argc == 3, not 2. argv[0] is almost always the name of the executable program.

I don't know about the stroul function.

I also have no idea what you are planning to do with this.

if (argc == 3)
   {
         
   }

Let me try. arg[0] refers to the name of exe, which is tprime over here. Then onwards, arg[r] refers to the rth command line argument.

what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?

You will have to convert these strings containing numbers to integers by an appropriate function.

Oops! Ancient Dragon & I replied just at about the same time!!

Ok, so this is what i have... sorry for the ignorance, but i am slowly getting it..

if i typed "prime 45 70"
the program rightly displays primes between 45 and 70..
if I type "prime" the program rightly displays who it was created by and how to use it...except with a "memory fault"at end.

If I type " prime 6" I just get "memory fault" it should display if that number is prime or not..

What am i doing wrong to get that memory fault.. That is the last thing I need to do with this program, again sorry, but thank you for patience and your help.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;
const unsigned W = 10;

int main(int argc, char *argv[])
{
   const unsigned long arraySize = 1000000;
   bool a[arraySize];
   unsigned long num1;
   unsigned long num2;
   unsigned long num3;
   unsigned long counter = 0;
   unsigned long primes = 0;
   char z;
   unsigned long answer = 0;
  
//Initializing elements to 1 
        
for (int i = 0; i < arraySize; i++)             
{
   a[i] = 1;
}       
// For array subscript 2, all elements beyond 2 in the array that
// are multiples of 2 will be set to zero; for array subscript 3, 
// all elements beyond 3 in the array that 
// are multiples of 3 will be set to zero


for (int i = 2; i * i < arraySize; i++)
{
    if (a[i])
       for (int j = i + i; j < arraySize; j += i)
          a[j] = 0;
} 
// if user enters more than 3 numbers or 0 numbers
// program should output useful information
// and title and author

if ((argc > 4) || (argc == 1))
{
cout<<"This is Programming Assignment #5"<<endl;
cout<<"THE SIEVE OF ERATOSTHENES"<<endl;
cout<<"by Jeremy Rice"<<endl;
cout<<endl;
cout<<"Usage:"<<endl;
cout<<"\tprime num1 - determines if num1 is a prime"<<endl;
cout<<"\tprime num1 num2 - displays primes between numbers."<<endl;
}



num1 = atoi(argv[1]);
num2 = atoi(argv[2]);
num3 = num1;
// if user enters one number
// program should determine if that number is a prime

if ((argc > 1) && (argc < 3))
{
   if (a[num1] == 1)
   cout<<num1<<" is a prime number."<<endl;
   else
   cout<<num1<<" is not a prime number."<<endl;
}

// if user enters 2 numbers
// program, should calculate the prime numbers 
// between the two numbers

if (argc == 3) 
   for (int i = num1; num1 < num2; num1++)

    if (num1 < num2)
    if (a[num1] == 1)
     {
      counter++; 
      cout<<setw(W)<<num1;
     }
       else if
        (a[num1] == 0)
     {
         cout<<"";
     }   
        else 
          cout<<"num1 must be lower than num2."<<endl;          
if (num1 > num2)
{
cout<<"Error: Invalid user specified number."<<endl;
return 0;
}
cout<<endl;
cout<<"There are "<<counter<<" primes between "<<num3<<" and "<<num1<<endl;
return EXIT_SUCCESS;
}

Between lines 51 and 52 you need to add return EXIT_SUCCESS; . (If you start your code blocks with the name of the language you are using, you get line numbers: [[I][/I]code=C++[I][/I]].)

Line 56: are you sure there is an argv[ 1 ]?
Line 57: are you sure there is an argv[ 2 ]?
Line 62: isn't that the same as if (argc == 2) ?
Line 90: you could just swap the numbers so that num1 < num2...

A few other notes. Choose C or C++, not both:
1. Use #include <cstdlib> 2. When dealing with boolean values, use the keywords true and false , so it is obvious that you are dealing with a boolean value and not an integer.
3. Likewise, when testing boolean values, say if (a[num1]) or if (a[num1] == true) 4. Stick lines 43..52 as the first thing in your main. (There are good reason's why, but I don't want to take the time to explain them all now...)
5. Use functions.
6. Old C people and those who don't know better like to have their students use const int foosize = 12000; and junk like that. That's really about the same thing as saying: #define foosize 12000 The better way is just to declare your array as bool a[1000000]; and use the sizeof operator to get its size later: for (int i = 0; i < sizeof( a )/sizeof( a[ 0 ] ); i++) Well, it is another way, at least. (It prevents you from being tempted to do dumb things.)
7. You forgot to use strtoul()...

You seem to have a pretty good grasp of what your code should be doing. Good job.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.