atoi and const char not being very friendly

This is a discussion on atoi and const char not being very friendly within the C++ Programming forums, part of the General Programming Boards category; 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. #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
    2,915
    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,620
    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, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    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,185
    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-29-2004, 12:18 AM
  5. Binary/Hexadecimal
    By Trauts in forum C++ Programming
    Replies: 24
    Last Post: 09-14-2002, 12:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21