my comparison isn't working

This is a discussion on my comparison isn't working within the C++ Programming forums, part of the General Programming Boards category; ok i'm just starting C++ and i did a little program to get if statements down but when i run ...

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

    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:
    Code:
    if (age >= 18 && localQuestion == "yes") {
    here is my entire code:

    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;
        cin.ignore();
        cout << "Do you live in the U.S? ";
        cin >> localQuestion;
        cin.ignore();
        
        if (age >= 18 && localQuestion == "yes") {
                cout << "You can vote\n";
        }
        else {
             cout << "Sorry you can not vote\n";
        }
        cin.get();
    }
    Last edited by Rubiks14; 10-02-2005 at 08:39 PM.

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    char only hold one character. So all it is getting is the y.
    You can either make it an array
    http://www.codersource.net/c++_arrays_tutorial.html
    Or strings
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
    Or just check to see if it is y:
    Code:
     if (age >= 18 && localQuestion == 'y')

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    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, 04: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, 08: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, 05:08 AM

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