954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

strtoul() function use

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?

jrice528
Light Poster
32 posts since Oct 2007
Reputation Points: 30
Solved Threads: 0
 
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!

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 
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.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

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

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

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;
}
jrice528
Light Poster
32 posts since Oct 2007
Reputation Points: 30
Solved Threads: 0
 

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: [code=C++].)

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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You