| | |
strtoul() function use
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 32
Reputation:
Solved Threads: 0
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:
heres the part I need help with
what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?
heres my code:
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
// 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?
•
•
•
•
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?
"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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
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.
I don't know about the stroul function.
I also have no idea what you are planning to do with this.
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.
I also have no idea what you are planning to do with this.
C++ Syntax (Toggle Plain Text)
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.
"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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
•
•
what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt?
"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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Oct 2007
Posts: 32
Reputation:
Solved Threads: 0
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.
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)
#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
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
Line 90: you could just swap the numbers so that num1 < num2...
A few other notes. Choose C or C++, not both:
1. Use
2. When dealing with boolean values, use the keywords
3. Likewise, when testing boolean values, say
or
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
The better way is just to declare your array as
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.
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 12000The 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.
![]() |
Similar Threads
- Need help on a program (function bug) (C)
- Assign content to a function pointer (C)
- PHP Feezing with a function (PHP)
- ASP Replace Function.... (ASP)
- missing function header (C++)
- Function that returns void (C++)
Other Threads in the C++ Forum
- Previous Thread: counting a few numbers and storing the values
- Next Thread: pointers and chars
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






