Results 1 to 28 of 28

Thread: Help in string

  1. #1
    WongEdan
    Guest

    Help in string

    i'm newbie in c++, and i wrote code which prompt user to input something ( in string), and i want to evaluate the input. like this
    Code:
    #include "iostream.h"
    
    void main()
    {
    	char name[25];
    		
    	cout << "Enter Your Name ";
    	cin >> name;
    		
    	if (name == 'ILO') 
                    {
                         cout << "Handsome";
                    }
                    else
                    {
                        cout << "Ugly";
                    }
    }
    i try to compile the program , but the compiler result an error.
    the message is :

    "C:\BelajarCE++\LatihString\LatihString.cpp(13) : error C2446: '==' : no conversion from 'const int' to 'char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    C:\BelajarCE++\LatihString\LatihString.cpp(13) : error C2040: '==' : 'char [25]' differs in levels of indirection from 'const int'
    Error executing cl.exe."

    Please help me how to handle this. thank's in advice .

  2. #2
    WongEdan
    Guest
    Anyone ???????........

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    First, try putting the header file like this:

    PHP Code:
    #include <iostream.h> 
    Baaaaaaaaah

  4. #4
    WongEdan
    Guest
    I still got the same error.

  5. #5
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    What compiler are you using?
    Baaaaaaaaah

  6. #6
    WongEdan
    Guest
    Microsoft Visual C ++

  7. #7
    Use "double quotes" for strings longer than one char, use 's'ingle quotes for one char or for escape chars like '\n'.

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    aaahhhh...did not notice that. Maybe, time to sleep
    Baaaaaaaaah

  9. #9
    WongEdan
    Guest
    it works filburt1, but whatever i type ( include ILO) , it produce Ugly. why ?

  10. #10
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    How about this:

    PHP Code:
    #include "iostream.h"

    void main()
    {
        
    char name[25];
            
        
    cout << "Enter Your Name ";
        
    cin >> name;
                   
    cout<<IIF(name == "ILO","Handsome","Ugly");
    return 
    0;        

    Baaaaaaaaah

  11. #11
    WongEdan
    Guest
    I got error C2065: 'IIF' : undeclared identifier

  12. #12
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    Re: Help in string

    Code:
    #include <iostream.h>
    #include <string.h>
    
    void main()
    {
    	char name[25];
    		
    	cout << "Enter Your Name ";
    	cin >> name;
    		
    	if (strcmp(name,"ILO")==0) 
                    {
                         cout << "Handsome";
                    }
                    else
                    {
                        cout << "Ugly";
                    }
    }
    I usually do it this way. If you want to do it (if (name=="ILO"))this way , you got to use a string class, I think. I never use a string class, I am lazy, I just use the easiest knowledge available(i.e. my brain)

    By the way, is your surname "Wong"?
    I'm a VB6 beginner.

  13. #13
    Originally posted by abdul
    How about this:

    PHP Code:
    #include "iostream.h"

    void main()
    {
        
    char name[25];
            
        
    cout << "Enter Your Name ";
        
    cin >> name;
                   
    cout<<IIF(name == "ILO","Handsome","Ugly");
    return 
    0;        

    Almost LOL, Iif is only in one language: VB. There's that funky inline if deal in C++ (condition ? true : false or condition : false ? true, I forget which).

  14. #14
    WongEdan
    Guest
    Thank's transcendental.

    I will try it.

    that's not My surename, it just a nick name. is this have special mean for you ?

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Cstrings don't have an overloaded operator == to compare them.
    Immediate if is equivalent with ternary operator ?:

    I suggest you learn the language basics first before you continue:
    Here's a good tutorial, Sam's teach yourself C++ in 21 days:
    http://newdata.box.sk/bx/c/
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by WongEdan
    that's not My surename, it just a nick name. is this have special mean for you ?
    Curious just asking.
    I'm a VB6 beginner.

  17. #17
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by kedaman
    Cstrings don't have an overloaded operator == to compare them.
    Immediate if is equivalent with ternary operator ?:

    I suggest you learn the language basics first before you continue:
    Here's a good tutorial, Sam's teach yourself C++ in 21 days:
    http://newdata.box.sk/bx/c/
    Hi, kedaman, does the ANSI C++ have a string class? Curious. When I say string class, I am not refering to MFC's CString class.

    Practical C++ book uses it all the time. small capital s in string class. #include<string>
    I'm a VB6 beginner.

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yeah, there's a class template basic_string which string is instantiated from, i've never used it though, Cstrings have been good enough for me since i don't do anything advanced with them.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Originally posted by filburt1


    Almost LOL, Iif is only in one language: VB. There's that funky inline if deal in C++ (condition ? true : false or condition : false ? true, I forget which).
    Totally totally messed up...damn I have seen this "ratio" if in javascript too but I was thinking of vb one
    Baaaaaaaaah

  20. #20
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    This code below compiles and works with Dev-C++ and Free Borland compiler.

    Code:
    #include<string>
    #include<iostream>
    
    using namespace std;
    
    int main(void)
    {
          string holder;
          holder="myname";
          if (holder=="myname")
              cout<<"Yes"<<endl;
          else
              cout<<"No"<<endl;
          return 0;
    }
    This is working VC code below.
    Code:
    CTest_CStringDlg::OnOK() 
    {
    	// TODO: Add extra validation here
    	CString holder;
    	holder="myname";
    	if (holder=="myname")
        	MessageBox("Yes");
    	else
    		MessageBox("No");
    	CDialog::OnOK();
    }
    Last edited by transcendental; Nov 7th, 2001 at 02:13 AM.
    I'm a VB6 beginner.

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh my god............................................
    Immediate if is
    condition ? true : false;
    C-Strings don't have a == operator. To use == you must either use string from the header <string> or the MFC CString (what you do ONLY in MFC apps). To compare C-strings, always use strcmp. It returns 0 if the strings are equal. It is declared in <strings.h> or (real C++) <cstring>. If you use the == operator with C-strings you compare the MEMORY ADDRESSES!
    the string class is ANSI C++.
    Use
    #include <iostream>
    using namespace std;

    this is ANSI C++, iostream.h may disappear someday.
    the <string> header also contains a class named wstring for UNICODE strings.

    trans: the reason why the codes works is because the == operator is overloaded in CString and string.
    But even this code would work:
    Code:
    char* str = "Hello":
    if(str == "Hello") // ...
    I'm not 100% sure, but I think this would work, because:
    In every exe, the hardcoded strings are stored at a specific region. Every string is stored only once, if you use exactly the same string twice, both will refer to the same location.
    If you write:
    char* str = "Hello";
    the address of the string "Hello" in the exe is assigned to str. Now you can work with the string.
    If you later write:
    if(str == "Hello")
    you compare the memory adderss in str and the address of the string "Hello". Those are the same, so the statement returns true. I'm not sure, but I think this code will still enter the if:
    Code:
    char* str = "Hello";
    str[2] = t;  // string is now "Hetlo"
    if(str == "Hello")
    { // I think execution will get here...
    }
    keda or parksie, do you think this works? I'm too lazy to try...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You're right, a string is char* and pointer aritmethic applies on all pointers, except for str[2] = t; the code compiles
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    does it only compile or does the if fire too?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It fires, and i guess the optimizer placed both reference to "Hello"at the same place
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  25. #25
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I think every compiler does it, not only when optimized, but also when in debug mode.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    OK it should be
    str[2] = 't';
    and this produces an access violation because I'm trying to change the exe, which is write-protected.
    But if I remove that, it even works in debug mode.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's in the exe of course, since it's a constant, otherways it wouldn't have optimized it to use the same string.
    I got an access violation
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  28. #28
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    me too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width