Thread: my comparison isn't working

  1. #1
    Registered User
    Join Date
    Oct 2005

    my comparison isn't working

    ok i'm just starting C++ and i did a little program to get if statements down but when i run it i get an error that says "ISO C++ forbids comparison between pointer and integer" and highlights this line:
    if (age >= 18 && localQuestion == "yes") {
    here is my entire code:

    #include <iostream>
    using namespace std;
    int main()
        int age;
        char localQuestion;
        cout << "This program determines if you are eligeble to vote\n";
        cout << "How old are you? ";
        cin >> age;
        cout << "Do you live in the U.S? ";
        cin >> localQuestion;
        if (age >= 18 && localQuestion == "yes") {
                cout << "You can vote\n";
        else {
             cout << "Sorry you can not vote\n";
    Last edited by Rubiks14; 10-02-2005 at 07:39 PM.

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    char only hold one character. So all it is getting is the y.
    You can either make it an array
    Or strings
    Or just check to see if it is y:
     if (age >= 18 && localQuestion == 'y')

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    this is the offending bit.....

    localQuestion == "yes"

    You cannot do that in c/c++. firstly localQuestion is only a char. that means it can hold one character. Secondly even if it was a char* c-style string you would need to use strcmp() to compare it with the string "yes".

    Instead. #include<string> and make localQuestion a string not a char then suddenly that bit of code becomes legal.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  4. #4
    Registered User
    Join Date
    Oct 2005
    thank you. i used string and got it working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM