strtoul() function use

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

strtoul() function use

 
0
  #1
Nov 22nd, 2007
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:
  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
  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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: strtoul() function use

 
0
  #2
Nov 22nd, 2007
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!
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: strtoul() function use

 
0
  #3
Nov 22nd, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: strtoul() function use

 
0
  #4
Nov 22nd, 2007
I don't know about the stroul function.

I also have no idea what you are planning to do with this.
  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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: strtoul() function use

 
0
  #5
Nov 22nd, 2007
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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: strtoul() function use

 
0
  #6
Nov 22nd, 2007
Oops! Ancient Dragon & I replied just at about the same time!!
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

Re: strtoul() function use

 
0
  #7
Nov 22nd, 2007
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.


  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: strtoul() function use

 
0
  #8
Nov 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC