![]() |
| ||
| 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> heres the part I need help with // if user enters one number what do I put in the num1 = strtoul(argv[]) to take the number entered in the command line prompt? |
| ||
| Re: strtoul() function use Quote:
Quote:
|
| ||
| 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. |
| ||
| 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. 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. |
| ||
| Re: strtoul() function use Quote:
|
| ||
| Re: strtoul() function use Oops! Ancient Dragon & I replied just at about the same time!! |
| ||
| 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. #include <cstdlib> |
| ||
| 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 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. |
| All times are GMT -4. The time now is 4:56 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC