Thread: atoi and const char not being very friendly

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    atoi and const char not being very friendly

    isbn.cpp: In function 'int isValid(const char*)':
    isbn.cpp:25: error: invalid conversion from 'const char' to 'const char*'
    isbn.cpp:25: error: initializing argument 1 of 'int atoi(const char*)'
    isbn.cpp:25: error: ISO C++ forbids comparison between pointer and integer
    isbn.cpp:25: error: ISO C++ forbids comparison between pointer and integer

    Code:
    Function
    int isValid(const char str[])
    Code:
    Line giving errors
    /* determine if string contains non-numeric 
    		characters (aside from check digit being an 'X') */
    		for (int i = 0; i < 10; i++)
    			/* fail check if atoi returns fail to convert (0),
    			character being checked is not 0 and last digit is 
    			not an "X" */
    			if (atoi(str[i]) == 0 and str[i] != "0" and (i != 9 and str[i] != "X"))
    				validNumerals = false;
    Now, these errors go away if I put const char* str[] rather than const char str[] but then strlen does not work.

    Any advice?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    if (atoi(str[i]) == 0 and str[i] != "0" and (i != 9 and str[i] != "X"))
    This line contains several errors:

    First atoi() converts a C-string (a NULL terminated character string) to an integer. You are not supplying a C-String, you are supplying an character.

    Second C has no operator named "and" you would need "&&".

    Third you can not compare a character to a character string
    Code:
    str[i] != "X"
    You would need to replace the quotation marks with the single quote 'X'.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jimblumberg View Post
    Second C has no operator named "and" you would need "&&".
    This is a C++ forum though ..... "and" is a reserved keyword in C++ and specified as an alternative representation of &&.

    The 1999 C standard does support "and" as a macro in standard header <iso646.h>. That macro expands to &&.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This is a C++ forum though ..... "and" is a reserved keyword in C++ and specified as an alternative representation of &&.
    I stand corrected. That's good to know.



    Jim

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Whoops, thanks for the corrections. You've saved my assignment
    Last edited by flaris; 02-04-2011 at 05:16 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    rewind is in <cstdio>, although you're not supposed to use it with an <iostream> stream (use seekg instead)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. Help with outputing char in int
    By xephyr in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2004, 11:18 PM
  5. Binary/Hexadecimal
    By Trauts in forum C++ Programming
    Replies: 24
    Last Post: 09-13-2002, 11:48 PM