Thread: IF statement

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    Smile IF statement

    I have a problem with the IF statement can somebody help ?

    // C++

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    void main()
    {
    char buffer[10];
    char b;

    cout << "\n\nType a word: " << endl;
    cout << "\n:";
    cin.getline(buffer, 10);

    if(buffer == "hello")
    cout << "The typed word is the same as the embeded word in this program.\n\n";
    else
    cout << "The typed word is not the same as the embeded word in this program.\n\n";

    cout << "\nTyped word: " << buffer << endl;
    cout << "\n\nEnter to Exit.\n";

    cin.get(b);
    }
    Last edited by cyberbjorn; 04-08-2004 at 10:52 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    int main()

    >if(buffer == "hallo")
    This isn't doing what you think it is. To compare C-style strings you need to use strcmp:
    Code:
    if ( strcmp ( buffer, "hallo" ) == 0 )
    My best code is written with the delete key.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    char b;
    ...
    cin.get(b);
    that's just a waste of a byte... you can just use:
    Code:
    cin.get();
    at the end of the program to do the same thing
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Dear friends,

    Thanks for helping me I'm a Newbie but I'm learning verry well

    The Example code is working
    greetz Cyberbjorn

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Dear people,

    I want to give this Example A color background and the char's
    how can I make it so, here is the code:

    // C++

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    void main()
    {
    char buffer[10];
    char b;

    cout << "\n\nType a word: " << endl;
    cout << "\n:";
    cin.getline(buffer, 10);

    if ( strcmp ( buffer, "hallo" ) == 0 )
    cout << "The typed word is the same as the embeded word in this program.\n\n";
    else
    cout << "The typed word is not the same as the embeded word in this program.\n\n";

    cout << "\nTyped word: " << buffer << endl;
    cout << "\n\nEnter to Exit.\n";

    cin.get(b);
    }

    friendly greetz Cyberbjorn

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to give this Example A color background and the char's
    Read this.

    >void main()
    While you're at it, read this too.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Just wanted to get you to know you should use the standard template library.

    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>


    instead use these

    #include <iostream>
    #include <cstring>
    #include <cstdlib>

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM