Thread: How to get a different output when you type in different words.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    How to get a different output when you type in different words.

    Code:
    
    
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
      char mitch;
      char alex;
    
    
      cout<<"Please type your name: ";
      cin>> mitch;                       
      cin>> alex;
      cin.ignore();
      if ( mitch ) {
         cout<<"You are cool!\n";
      }
      else if ( alex ) {
         cout<<"You are not cool!/n";
      }
      else {
        cout<<"You are ok...\n";
      }
      cin.get();
    }
    Basically, what I want the program to do is ask the person to type their name (which I've already accomplished). Then, I want it to give a different response depending on what name they type in. If they type in "mitch", I want it to respond "You are cool!". If they type in "alex", I want it to respond "You are not cool!". And if they type in anything else, I want it to respond "You are ok...". I have it set up using an if, else if, else statement right now. The program compiles fine, but when it runs, no matter what you type in, the response is "You are cool!". Does anyone have any tips on how to get it to work right? Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try something like

    #include <string>

    string name;

    cin >> name;

    if ( name == "mitch" )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    I tried that but it wouldn't compile and now it says " no match for 'operator==' in 'name == mitch' " , and " no match for 'operator==' in 'name == alex' ". Do I maybe have to set mitch = to a variable?
    Last edited by Mhesseltine; 03-17-2012 at 09:57 AM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Haha never mind I put ' if ( name == mitch) ' not ' if ( name == "mitch" ) '. So it's working now. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2011, 04:54 AM
  2. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  3. output a data type to createwindow..??????
    By Anddos in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2006, 01:41 PM
  4. Output of complex data type
    By MB1 in forum C++ Programming
    Replies: 5
    Last Post: 04-06-2006, 02:12 PM
  5. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM

Tags for this Thread