Compare text string to a specific character.

This is a discussion on Compare text string to a specific character. within the C++ Programming forums, part of the General Programming Boards category; Hello. I recently started out with C++ and I was going to make a piece of code compare a letter ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    30

    Question Compare text string to a specific character.

    Hello.

    I recently started out with C++ and I was going to make a piece of code compare a letter in a text string to a specific letter, Now I have made a code that can do this, but it has some problems that I'm sure you will understand when you see it :

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    char *text_string = "abcddcba";
    char *compare = "aaaaaaaa"; 
    char *compare1 = "bbbbbbbb";
    char *compare2 = "cccccccc";
    char *compare3 = "dddddddd";
    
    int Length = strlen(text_string);
    
    for (int i = 0; i < Length; ++i){
     if(text_string[i] == compare[i]){
    	 cout <<i+1<< "=a"<< endl;
    }else
    if(text_string[i] == compare1[i]){
    	 cout <<i+1<< "=b"<< endl;
    }else
    	if(text_string[i] == compare2[i]){
    	 cout <<i+1<< "=c"<< endl;
    }else
    	if(text_string[i] == compare3[i]){
    	 cout <<i+1<< "=d"<< endl;
    }
    }
    cout << "Length of text is: " << Length;
    cin.get();
    return 0;
    }
    The program prints what I want it to:
    1=a
    2=b
    3=c
    etc.


    Now the problem is that if I use this code my compare variables must be over 20 letters long.
    int compare = "20xa".

    Before I got this code to even work I tried this code that would have been alot better:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    char *text_string = "abcddcba";
    
    
    int Length = strlen(text_string);
    
    for (int i = 0; i < Length; ++i){
     if(text_string[i] == "a"){
    	 cout <<i+1<< "=a"<< endl;
    }else
    if(text_string[i] == "b"){
    	 cout <<i+1<< "=b"<< endl;
    }else
    	if(text_string[i] == "c"){
    	 cout <<i+1<< "=c"<< endl;
    }else
    	if(text_string[i] == "d"){
    	 cout <<i+1<< "=d"<< endl;
    }
    }
    cout << "Length of text is: " << Length;
    cin.get();
    return 0;
    }
    But ofcourse that didnt work.
    If anyone have any better solution to this I would appreciate if you could post it here.

    ps: I hope you understand what I mean, my english isnt the best.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    When you make a comparison, the two things being compared ought to be the same type, or either it won't work at all, because there is no type conversion possible to make things the same type, or the conversion could happen implicitly by mistake. In this case, no conversion can take place

    Code:
    text_string[i] == "a"
    text_string[i] == "b"
    text_string[i] == "c"
    text_string[i] == "d"
    because we have a character on the left and a string on the right.

    Code:
    text_string[i] == 'a'
    text_string[i] == 'b'
    text_string[i] == 'c' 
    text_string[i] == 'd'

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    text_string should be declared as
    const char *

    since it is pointing to the constant string that cannot be modified
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    Well, in the final program that string is going to be changed before it gets to the for loop.
    And I know that the comarision of different types doesnt work.

    Edit: using const char didnt work. I got the "operand types are incompatible" when I compared const char *text to "a"
    Last edited by Danne; 01-08-2011 at 05:26 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Danne View Post
    Well, in the final program that string is going to be changed before it gets to the for loop.
    And I know that the comarision of different types doesnt work.

    Edit: using const char didnt work. I got the "operand types are incompatible" when I compared const char *text to "a"
    You'll want to be aware, then, that "a" and 'a' are way way way way different and if you use the first when you want the second you'll get "operand types are incompatible".

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    30
    Quote Originally Posted by tabstop View Post
    You'll want to be aware, then, that "a" and 'a' are way way way way different and if you use the first when you want the second you'll get "operand types are incompatible".
    Oh, I had no idea about that. I tried using the 'a' and it works perffectly now.
    But what is the difference between "a" and 'a'? "a" is a char and 'a' is a string?
    Anways Thank you very much.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    almost, "a" is a nullterminated string, while 'a' is just a single character a.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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