string & if statement

This is a discussion on string & if statement within the C++ Programming forums, part of the General Programming Boards category; This is a basic question so here goes. I'm trying to perform a logic operation on a string (an if ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1

    string & if statement

    This is a basic question so here goes. I'm trying to perform a logic operation on a string (an if statement on a persons name). I've tried with quotation marks "if (name == "Joe")" and with apostrophes "if (name == 'Joe')" but neither seems to work. Name is declared as "char name[25]." The statement with quotation marks it thinks even if I type the name right it is false. With apostrophes I get a "warning: multi-character character constant" error. Since I don't know anything I don't know where to start looking for answers. I've tried getting the string with "cin" and "cin.getline" but both give the same results. Also is there a way to make this statement not caps sensative, so "joe", "Joe", or "JOe" all end up being true in the statement?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    look up strcmp().

  3. #3
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    Quote Originally Posted by curacao
    Also is there a way to make this statement not caps sensative, so "joe", "Joe", or "JOe" all end up being true in the statement?
    yes, use toupper or tolower (i think its tolower, but i know toupper is right)
    example:
    Code:
    char name[25];
    cin.getline(name,25);
    if (name (toupper) == "JOE")
    {
      cout <<"your name is joe";
    }
    please excuse my syntax on toupper, i havent used it in a while and i think i may have messed up. im sure someone will correct if i am wrong

    or you could just do what alpha said....

    and btw, wut did i do wrong with the quote tag?
    Last edited by Geo-Fry; 05-02-2003 at 04:40 PM.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Geo-Fry

    please excuse my syntax on toupper, i havent used it in a while and i think i may have messed up. im sure someone will correct if i am wrong


    and btw, wut did i do wrong with the quote tag?
    you did mess it up, it's supposed to be used in a for loop since it takes one character, and this is a char array.

    i.e.

    Code:
    int i;
    for(i=0; i < SIZE; i++) {
      name[i] = tolower(name[i]);
      //you could also use toupper
      // name[i] = toupper(name[i]);
    }
    tolower() and toupper() are in cctype.

    I think there may be one that does the whole string, not sure.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Using a string object might be helpful...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    	string name;
    
    	getline(cin, name);
    
    	if(name == "Joe") 
    		cout <<"My name is Joe too! (ok, not really)" <<endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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