A beginner in need

This is a discussion on A beginner in need within the C++ Programming forums, part of the General Programming Boards category; I know very little about C++. I would like to use an if command in this way: Ask user to ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    Question A beginner in need

    I know very little about C++. I would like to use an if command in this way:



    Ask user to enter a word

    if word is "something!"

    cout "bleah!"

    else

    if word is "other!"

    cout "miscellaneous!"

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    if and else dont work with character arrays which is what im assuming what you mean by a "string". you could use std::string, but since youve just started, i dont suggest it. you could use strcmp()...

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    aha

    You can use either if else statements or witch statement.
    Code:
     if( word == " word1")
    cout<< res1;
    else  if( word == " word2")
    cout<< res2;
    else 
    cout<< res3;
    
    //OR
    
       switch word {
        word1: {cout<< res1;  break;}
        word2: {cout<< res2;  break;}
        word3: {cout<< res3;  break;}
    
       }
    I think this is good code.
    C++
    The best

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    7

    re: if statement

    please note that if u have more than one line of code following your if statement these lines must be enclosed in matching {}

    example:

    if(word==word1)
    {cout<<"abc";
    cout<<"xyz";
    }

    else
    abc

    if u dont put the {} your compiler will only read the line immediately following the if statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me with this beginner C++ Program!
    By ClearSights in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 11:22 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 12:48 PM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 03:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

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