Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,559 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,642 people online right now. Registration is fast and FREE... Join Now!




using ths strtoul function

 
Reply to this topicStart new topic

using ths strtoul function

jrice528
22 Nov, 2007 - 12:02 AM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 31


My Contributions
Ok, I figured out how to ues command line arguments. The program is called prime. So i need to say enter this into the control prompt..

> prime 7
then the program determines if it is a prime or not... Only thing is I dont how/where to put the strtoul function to turn the char 7, into the int 7...

heres the part I need help on, somewhere in there I need to turn the 7 into an int.
CODE

// if user enters one number
// program should determine if that number is a prime

if (argc == 2)
{  
   if (a[argc] == 1)
   {  
       counter++;
       cout<<"This is a prime number."<<endl;  
   }
   else
   cout<<"This is not a prime number."<<endl;
}



heres the whole code"

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;
  

cout<<"This program is programming assignemnt #5."<<endl;
cout<<"THE SIEVE OF ERATOSTHENES."<<endl;
cout<<"By Marley Walsh of CSCI 111."<<endl;


//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 one number
// program should determine if that number is a prime

if (argc == 2)
{  
   if (a[argc] == 1)
   {  
       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)
   {
        
   }


// 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;
}
return EXIT_SUCCESS;
}


i hope that all made sense
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Using Ths Strtoul Function
22 Nov, 2007 - 12:08 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,129



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
http://www.dreamincode.net/forums/showtopic30387.htm
CODE

int numb;
numb=atoi(argv[i]);
printf("%d\n",numb);


User is online!Profile CardPM
+Quote Post

jrice528
RE: Using Ths Strtoul Function
22 Nov, 2007 - 09:21 AM
Post #3

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 31


My Contributions
Ok! I figured out how to use the atoi functiom, ty for the reference. I figured it out when I put "prime 55 70" in console it finds all primes between those two numbers...

Except when I put "prime 8" i get a "memory fault" here is the code could you tell me what could be causing that?
CODE
#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;
}


its here, is where i am getting the memory fault
CODE
// 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;
}

User is offlineProfile CardPM
+Quote Post

colo001
RE: Using Ths Strtoul Function
23 Nov, 2007 - 02:34 AM
Post #4

New D.I.C Head
*

Joined: 6 Nov, 2007
Posts: 2


My Contributions
QUOTE
its here, is where i am getting the memory fault
CODE
// 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;
}



CODE
// 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;




the error is in actually the open and close brackets. try putting in the brackets in the code where you check the prime numbers within the range. it worked fine when i place brackets around each block. i think that putting in a quote at the end pf this line should do:
CODE
cout<<endl;
cout<<"There are "<<counter<<" primes between "<<num3<<" and "<<num1<<endl;



(*edit - please use code tags when posting)

This post has been edited by jjhaag: 23 Nov, 2007 - 03:04 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month