944,214 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3098
  • C++ RSS
Nov 22nd, 2007
0

strtoul() function use

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. const unsigned long arraySize = 1000000;
  12. bool a[arraySize];
  13. unsigned long num1;
  14. unsigned long num2;
  15. unsigned long num3;
  16. unsigned long counter = 0;
  17. unsigned long primes = 0;
  18. char z;
  19. unsigned long answer = 0;
  20.  
  21. //Initializing elements to 1
  22.  
  23. for (int i = 0; i < arraySize; i++)
  24. {
  25. a[i] = 1;
  26. }
  27. // For array subscript 2, all elements beyond 2 in the array that
  28. // are multiples of 2 will be set to zero ; for array subscript 3,
  29. // all elements beyond 3 in the array that
  30. // are multiples of 3 will be set to zero
  31.  
  32.  
  33. for (int i = 2; i * i < arraySize; i++)
  34. {
  35. if (a[i])
  36. for (int j = i + i; j < arraySize; j += i)
  37. a[j] = 0;
  38. }
  39. // if user enters more than 3 numbers or 0 numbers
  40. // program should output useful information
  41. // and title and author
  42.  
  43. if ((argc > 4) || (argc == 1))
  44. {
  45. cout<<"This is Programming Assignment #5"<<endl;
  46. cout<<"THE SIEVE OF ERATOSTHENES"<<endl;
  47. cout<<"by Jeremy Rice"<<endl;
  48. cout<<endl;
  49. cout<<"Usage:"<<endl;
  50. cout<<"\tprime num1 - determines if num1 is a prime"<<endl;
  51. cout<<"\tprime num1 num2 - displays primes between numbers."<<endl;
  52. }
  53.  
  54. // if user enters one number
  55. // program should determine if that number is a prime
  56.  
  57. if (argc == 2)
  58. {
  59. if (a[argc] == 1)
  60. {
  61. num1 = strtoul(argv[]);
  62. counter++;
  63. cout<<"This is a prime number."<<endl;
  64. }
  65. else
  66. cout<<"This is not a prime number."<<endl;
  67. }
  68.  
  69.  
  70.  
  71. // if user enters 2 numbers
  72. // program, should calculate the prime numbers
  73. // between the two numbers
  74.  
  75. if (argc == 3)
  76. {
  77.  
  78. }
  79.  
  80.  
  81.  
  82. return EXIT_SUCCESS;
  83. }


heres the part I need help with
C++ Syntax (Toggle Plain Text)
  1. // if user enters one number
  2. // program should determine if that number is a prime
  3.  
  4. if (argc == 2)
  5. {
  6. if (a[argc] == 1)
  7. {
  8. num1 = strtoul(argv[]);
  9. counter++;
  10. cout<<"This is a prime number."<<endl;
  11. }
  12. else
  13. cout<<"This is not a prime number."<<endl;
  14. }

what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?
Similar Threads
Reputation Points: 30
Solved Threads: 0
Light Poster
jrice528 is offline Offline
32 posts
since Oct 2007
Nov 22nd, 2007
0

Re: strtoul() function use

Quote ...
I finally figured out command line arguments
Quote ...
what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?
A contradiction!
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 22nd, 2007
0

Re: strtoul() function use

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.
Last edited by Ancient Dragon; Nov 22nd, 2007 at 12:53 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,966 posts
since Aug 2005
Nov 22nd, 2007
0

Re: strtoul() function use

I don't know about the stroul function.

I also have no idea what you are planning to do with this.
C++ Syntax (Toggle Plain Text)
  1. if (argc == 3)
  2. {
  3.  
  4. }

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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 22nd, 2007
0

Re: strtoul() function use

Quote ...
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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 22nd, 2007
0

Re: strtoul() function use

Oops! Ancient Dragon & I replied just at about the same time!!
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 22nd, 2007
0

Re: strtoul() function use

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.


C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7. const unsigned W = 10;
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. const unsigned long arraySize = 1000000;
  12. bool a[arraySize];
  13. unsigned long num1;
  14. unsigned long num2;
  15. unsigned long num3;
  16. unsigned long counter = 0;
  17. unsigned long primes = 0;
  18. char z;
  19. unsigned long answer = 0;
  20.  
  21. //Initializing elements to 1
  22.  
  23. for (int i = 0; i < arraySize; i++)
  24. {
  25. a[i] = 1;
  26. }
  27. // For array subscript 2, all elements beyond 2 in the array that
  28. // are multiples of 2 will be set to zero; for array subscript 3,
  29. // all elements beyond 3 in the array that
  30. // are multiples of 3 will be set to zero
  31.  
  32.  
  33. for (int i = 2; i * i < arraySize; i++)
  34. {
  35. if (a[i])
  36. for (int j = i + i; j < arraySize; j += i)
  37. a[j] = 0;
  38. }
  39. // if user enters more than 3 numbers or 0 numbers
  40. // program should output useful information
  41. // and title and author
  42.  
  43. if ((argc > 4) || (argc == 1))
  44. {
  45. cout<<"This is Programming Assignment #5"<<endl;
  46. cout<<"THE SIEVE OF ERATOSTHENES"<<endl;
  47. cout<<"by Jeremy Rice"<<endl;
  48. cout<<endl;
  49. cout<<"Usage:"<<endl;
  50. cout<<"\tprime num1 - determines if num1 is a prime"<<endl;
  51. cout<<"\tprime num1 num2 - displays primes between numbers."<<endl;
  52. }
  53.  
  54.  
  55.  
  56. num1 = atoi(argv[1]);
  57. num2 = atoi(argv[2]);
  58. num3 = num1;
  59. // if user enters one number
  60. // program should determine if that number is a prime
  61.  
  62. if ((argc > 1) && (argc < 3))
  63. {
  64. if (a[num1] == 1)
  65. cout<<num1<<" is a prime number."<<endl;
  66. else
  67. cout<<num1<<" is not a prime number."<<endl;
  68. }
  69.  
  70. // if user enters 2 numbers
  71. // program, should calculate the prime numbers
  72. // between the two numbers
  73.  
  74. if (argc == 3)
  75. for (int i = num1; num1 < num2; num1++)
  76.  
  77. if (num1 < num2)
  78. if (a[num1] == 1)
  79. {
  80. counter++;
  81. cout<<setw(W)<<num1;
  82. }
  83. else if
  84. (a[num1] == 0)
  85. {
  86. cout<<"";
  87. }
  88. else
  89. cout<<"num1 must be lower than num2."<<endl;
  90. if (num1 > num2)
  91. {
  92. cout<<"Error: Invalid user specified number."<<endl;
  93. return 0;
  94. }
  95. cout<<endl;
  96. cout<<"There are "<<counter<<" primes between "<<num3<<" and "<<num1<<endl;
  97. return EXIT_SUCCESS;
  98. }
Reputation Points: 30
Solved Threads: 0
Light Poster
jrice528 is offline Offline
32 posts
since Oct 2007
Nov 22nd, 2007
0

Re: strtoul() function use

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: counting a few numbers and storing the values
Next Thread in C++ Forum Timeline: pointers and chars





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC