Thread: (int) casting

  1. #1
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65

    (int) casting

    When I try to typecast a user-input value, I'm getting the address (I believe) instead of the actual value. Can someone help?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
    	if (argc < 1) {
    		cout << "ERROR: You must supply a number." << endl;
    		return 0;
    	}
    
    	int number = (int)argv[1];
    
    //more code after this...
    }
    \0

  2. #2
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Maybe it's easier to ask how I get an INT from the user input. Even a CIN would work... help?
    \0

  3. #3
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    I figured out CIN ... but I'm still wondering how I get the number from the command-line arguments. Anyone know?
    \0

  4. #4
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    argv[1] is a char-string, not a number.
    To convert it to an int look up atoi().

    /btq
    ...viewlexx - julie lexx

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    if (argc < 1) {
    Should be:
    Code:
    if (argc < 2) {
    Otherwise simply running the program without an argument (argc will always equal at least 1) will attempt to access an invalid memory location, i.e. argv[1]. As you have it, the ERROR message will never get output.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    don't you have to dereference it?
    Code:
    	int number = (int)*argv[1];
    
    //more code after this...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    int main(int argc, char *argv[]) {
      
      if (argc < 2) {
    
        cout << "ERROR: You must supply a number." << endl;
        return 0;
    
      }
      
      int number;
      if( sscanf( argv[ 1 ], "%d", &number ) == 1 ) {
        
        //number now holds the integer value of argv[ 1 ]
        
      } else {
        
        //there was an error
        
      }
      
    //more code after this...
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM