string comparisons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Horn

    string comparisons

    I am having trouble comparing strings. When I run the program and I
    type in what I think is the right string, it goes to the else part of
    the program. This is for my computer science class in high school so
    excuse me if you think it's really easy. If you know what I should do
    then please send me a message at hornstar12@emai l.com

    //Britton Horn

    #include "stdafx.h"
    #include <iostream.h>
    #include <stdlib.h>
    #ifndef _OOSTRING_H
    #define _OOSTRING_H
    #include <string.h>

    int main()
    {
    char yourname[25];
    char myname[25] = "britton";
    int answer;

    cout << "What is your name?\n";
    cin.get(yournam e, 25);

    if (yourname == myname)
    {
    cout << "What is 2 + 2?\n";
    cin >> answer;
    cout << "Correct!\n ";
    }
    else
    {
    cout << "What is 2 + 2?\n";
    cin >> answer;
    if (answer == 4)
    {
    cout << "I'm sorry. The answer is 22.\n";
    }
    if (answer != 4)
    {
    cout << "I'm sorry. The answer is 4.\n";
    }
    }
    return 0;
    #endif
    }
  • WW

    #2
    Re: string comparisons

    Horn wrote:[color=blue]
    > I am having trouble comparing strings. When I run the program and I
    > type in what I think is the right string, it goes to the else part of
    > the program. This is for my computer science class in high school so
    > excuse me if you think it's really easy. If you know what I should do
    > then please send me a message at hornstar12@emai l.com[/color]


    Posted AND mailed. LAST TIME! Post here and read here. Answers to my
    mailbox will be deleted unread:


    [color=blue]
    > //Britton Horn
    >
    > #include "stdafx.h"[/color]

    Non-standard header
    [color=blue]
    > #include <iostream.h>[/color]

    Non-standard header
    [color=blue]
    > #include <stdlib.h>
    > #ifndef _OOSTRING_H
    > #define _OOSTRING_H[/color]

    WHY on Earth is this in a header???
    [color=blue]
    > #include <string.h>
    >
    > int main()
    > {
    > char yourname[25];
    > char myname[25] = "britton";
    > int answer;
    >
    > cout << "What is your name?\n";
    > cin.get(yournam e, 25);
    >
    > if (yourname == myname)[/color]

    Learn about strcmp. Or better: learn about std::string.







    In short: http://tinyurl.com/pm6r
    [color=blue]
    > {
    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > cout << "Correct!\n ";
    > }
    > else
    > {
    > cout << "What is 2 + 2?\n";
    > cin >> answer;
    > if (answer == 4)
    > {
    > cout << "I'm sorry. The answer is 22.\n";
    > }
    > if (answer != 4)
    > {
    > cout << "I'm sorry. The answer is 4.\n";
    > }
    > }
    > return 0;
    > #endif[/color]

    WHY on Earth is this (the main function) in a header???
    [color=blue]
    > }[/color]

    --
    WW aka Attila


    Comment

    • Aggro

      #3
      Re: string comparisons

      Horn wrote:

      [color=blue]
      > #include <string.h>
      >
      > int main()
      > {
      > char yourname[25];
      > char myname[25] = "britton";
      > int answer;
      >
      >
      > if (yourname == myname)[/color]

      Any particular reason why not using the std::string? If you must use
      char arrays, you can't compare them like this. You need to use strcmp()
      function, or write one by yourself. If you use std::string you can
      compare strings like that. Have a look at these two examples:

      -------------------------------
      #include <string>
      #include <iostream>

      int main()
      {
      std::string a = "hello";
      std::string b = "hello";

      if( a == b )
      {
      std::cout << "Strings are equal." << std::endl;
      }
      else
      {
      std::cout << "Strings are not equal." << std::endl;
      }

      return 0;
      }
      --------------------------

      ------------------------------
      #include <cstring>
      #include <iostream>

      int main()
      {
      char c[] = "hello";
      char d[] = "hello";

      if( strcmp( c,d ) == 0 )
      {
      std::cout << "Strings are equal." << std::endl;
      }
      else
      {
      std::cout << "Strings are not equal." << std::endl;
      }

      return 0;
      }
      --------------------------------

      Comment

      • WW

        #4
        Re: string comparisons

        Aggro wrote:[color=blue]
        > Any particular reason why not using the std::string? If you must use
        > char arrays, you can't compare them like this. You need to use
        > strcmp() function, or write one by yourself. If you use std::string
        > you can compare strings like that. Have a look at these two examples:[/color]

        Fish, or fishing?

        --
        WW aka Attila


        Comment

        • Kris Wempa

          #5
          Re: string comparisons


          "Horn" <hornstar12@ema il.com> wrote in message
          news:6e420ba5.0 310030949.3c629 499@posting.goo gle.com...[color=blue]
          > int main()
          > {
          > char yourname[25];
          > char myname[25] = "britton";
          > int answer;
          >
          > cout << "What is your name?\n";
          > cin.get(yournam e, 25);
          >
          > if (yourname == myname)[/color]

          You can't compare character strings like this. You may be able to do this
          for C++ string data types, but not for traditional C character strings. You
          need to do this:

          if (strcmp(yournam e,myname) == 0)
          [color=blue]
          > cout << "What is 2 + 2?\n";
          > cin >> answer;
          > if (answer == 4)
          > {
          > cout << "I'm sorry. The answer is 22.\n";
          > }
          > if (answer != 4)
          > {
          > cout << "I'm sorry. The answer is 4.\n";
          > }
          > }[/color]

          Is this part working correctly ?


          Comment

          • Kevin Goodsell

            #6
            Re: string comparisons

            Horn wrote:
            [color=blue]
            > I am having trouble comparing strings. When I run the program and I
            > type in what I think is the right string, it goes to the else part of
            > the program. This is for my computer science class in high school so
            > excuse me if you think it's really easy. If you know what I should do
            > then please send me a message at hornstar12@emai l.com[/color]

            I won't, and it's rude to ask. Post here, read here.
            [color=blue]
            >
            > //Britton Horn
            >
            > #include "stdafx.h"[/color]

            Incomplete code makes it difficult to diagnose problems. You haven't
            given us the contents of this header file.
            [color=blue]
            > #include <iostream.h>[/color]

            This is not a standard C++ header. Standard C++ uses <iostream>.
            [color=blue]
            > #include <stdlib.h>[/color]

            This header is deprecated in favor of <cstdlib>.
            [color=blue]
            > #ifndef _OOSTRING_H
            > #define _OOSTRING_H[/color]

            This is illegal. You may not use identifiers beginning with an
            underscore followed by either another underscore or an uppercase letter.
            They are reserved for the implementation' s use. Even if your compiler
            fails to diagnose this (most won't), it is an error. A compiler may
            reject the code, or may even accept it with a dramatically altered meaning.

            The simplest rule of thumb is *never use an identifier that begins with
            an underscore, or contains a sequence of two underscores.*

            Besides that, "include guards" make no sense in a non-header file.
            [color=blue]
            > #include <string.h>[/color]

            This header is deprecated in favor of <cstring>, but you should just use
            <string> and the std::string class instead (as others have already said).
            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Jonathan Mcdougall

              #7
              Re: string comparisons

              > I am having trouble comparing strings.




              Look at the end of the post.


              Jonathan


              Comment

              • Ashish

                #8
                Re: string comparisons


                "Horn" <hornstar12@ema il.com> wrote in message
                news:6e420ba5.0 310030949.3c629 499@posting.goo gle.com...[color=blue]
                > I am having trouble comparing strings. When I run the program and I
                > type in what I think is the right string, it goes to the else part of
                > the program. This is for my computer science class in high school so
                > excuse me if you think it's really easy. If you know what I should do
                > then please send me a message at hornstar12@emai l.com
                >
                > //Britton Horn
                >
                > #include "stdafx.h"
                > #include <iostream.h>
                > #include <stdlib.h>
                > #ifndef _OOSTRING_H
                > #define _OOSTRING_H
                > #include <string.h>
                >
                > int main()
                > {
                > char yourname[25];
                > char myname[25] = "britton";
                > int answer;
                >
                > cout << "What is your name?\n";
                > cin.get(yournam e, 25);
                >
                > if (yourname == myname)[/color]

                You cant compare string like this. This will compare the addresses pointed
                to by 'yourname' and 'myname'. To compare string, use...

                if(strcmp(yourn ame, myname) == 0)

                [color=blue]
                > {
                > cout << "What is 2 + 2?\n";
                > cin >> answer;
                > cout << "Correct!\n ";
                > }
                > else
                > {
                > cout << "What is 2 + 2?\n";
                > cin >> answer;
                > if (answer == 4)
                > {
                > cout << "I'm sorry. The answer is 22.\n";
                > }
                > if (answer != 4)
                > {
                > cout << "I'm sorry. The answer is 4.\n";
                > }
                > }
                > return 0;
                > #endif
                > }[/color]


                Comment

                Working...