Thread: I don't understand what is wrong...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    I don't understand what is wrong...

    Code:
    char inputchar;
    if(inputchar=="a"){...stuff here...}
    This gives me an error and I really don't understand why.

    " ANSI C++ forbids comparison between pointer and integer"

    No pointer anywhere here.

    ((EDIT: It is talking about the if statement; in my actual code the if(inputchar.. is on a line by itself and it is showing that line))
    Last edited by flaran; 01-17-2006 at 06:34 PM.
    "I've developed an easy debugging method. First I copy the entire error message, then paste it into Google."
    -Paul Stovell

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Confusing, like most error messages...

    "a" represents a string literal. It cannot be represented with a single char variable.

    'a' represents a single char (decimal value=97 on the ASCII chart).

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    Bah, thanks. If it would have even hinted at it in the error message maybe I would have gotten that.

    Yet again sorry for being so inept,

    -Flaran
    "I've developed an easy debugging method. First I copy the entire error message, then paste it into Google."
    -Paul Stovell

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I would like to add somthing else about that type of error.

    if you are using strings in a program

    you can use the quotation marks as it understands it if it has first been declared as a string

    variable type. example code:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string myname;     // declare a string type variable
    
    cout << "What is my name? ";
    cin >> myname;
    
    if ( myname == "doug" )  // ok to use quotation marks here
    {
    cout << "Yes well done!" << endl;
    }
    
    else
    {
    cout << "Sorry, that is not my name" << endl;
    }
    
    return 0;
    
    }
    Sorry if you already know this, but It just shows the differnece
    between character strings and single character letters

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Code:
    " ANSI C++ forbids comparison between pointer and integer"
    Code:
    Confusing, like most error messages...
    Well,strings are stored as pointers only.When you write
    Code:
    cout<<"Hello world";
    At compile time string "Hello world" is stored somewhere and a pointer is assigned
    Code:
    cout<<ptr;//ptr contains address of 'H'
    You may confirm this by following code
    Code:
    cout<<"Hello World"[3];
    cout<<&"Hello World"[3];
    Code:
    output=l
    second cout==llo World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM