Thread: newbie question, should be easy

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    1

    Red face newbie question, should be easy

    ok welll, i need to know why..

    if (yesno=="yes") {
    cout<<"Thats quite interesting, thanks for the imput.";
    }

    will not work. it would really really help me to find out. thanks a lot.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That expression is trying to compare the address of a char* (I am assuming that is what 'yesno' is) to a literal string. This does not work. Instead, you need to use the strcmp function in <cstring>. Here is an example:

    Code:
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        // Some code...
    
        char* yesno;
        cin >> yesno;
    
        if(strcmp(yesno, "yes") == 0)
            cout<<"Thats quite interesting, thanks for the imput.";
    
        // More code...
    
        return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming that yesno is a character array, you can't compare strings with ==, you have to use strcmp() in the string.h header file. Like this:

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main(){
      char yesno[] = "yes";
      if (strcmp(yesno, "yes")==0) { 
        cout<<"Thats quite interesting, thanks for the imput."; 
      }
      return 0;
    }
    -Prelude

  4. #4
    BubbleMan
    Guest

    Post What is..

    What is the problem you come to?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM
  3. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  4. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM
  5. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 09:58 PM