Thread: Comparing two Strings

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Comparing two Strings

    Hello everybody I'm new in c++ programming and i have a problem with strings.Actually I'm trying to do this:

    I need to create a string called countryOfOrigin
    double basicPrice
    and double importPrice
    and double overallPrice;
    and if the countryOfOrigin is Yugoslavia the importprice will be 0;
    else the importPrice is 5000 and it's added to overallPrice;


    #include<iostream>
    #include <string>
    using namespace std;


    int main(){
    double basicPrice;
    double importPrice;
    string countryOfOrigin;


    basicPrice=15000;

    and now i need to do this
    if the countryOfOrigin is Yugoslavia the importPrice will be 0;
    else the importPrice is 5000 and it's added to overallPrice;

    I really can't fix this problem can anybody show me how to check that countryOfOrigin is Yugoslavia or not.
    Best regards

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you are using std::strings you can use the comparison operator== to compare the two strings.

    Jim

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by jimblumberg View Post
    Since you are using std::strings you can use the comparison operator== to compare the two strings.

    Jim
    Do not do that with std::strings. Use the std::string.compare() function instead.

    Code:
    if (countryOfOrigin.compare("yugoslavia") == 0)
        cout << "You were born in Yugoslavia";
    I forget whether case sensitivity is a factor but you get the idea.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do not do that with std::strings. Use the std::string.compare() function instead.
    Why, the comparison operator== has been overloaded for just this purpose.

    Jim

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Note that Rodaloleux describes himself as a "C++ troll" .....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    i definitely got the point problem solved thanks guys.

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I've always been told to use compare(). And looking it up from here, I don't see an std::string:: operator==, but then again. There's a lot of stuff I don't know.

    Also
    Quote Originally Posted by grumpy View Post
    Note that Rodaloleux describes himself as a "C++ troll" .....
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Rodaxoleaux View Post
    I've always been told to use compare(). And looking it up from here, I don't see an std::string:: operator==, but then again. There's a lot of stuff I don't know.
    That's because operator==() is not a member function of std::string. It is a global function. The page you are using only lists member functions.

    One of the things you don't know, apparently, is how to employ information you have access to.

    From that that link you use, look to the left of the screen. Scroll down to a box entitled "String library". In that box, look in the list below "global functions:". One of them is a link to (drum roll) "comparison operators".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help comparing strings
    By busdude in forum C Programming
    Replies: 2
    Last Post: 12-02-2009, 09:00 PM
  2. comparing strings
    By Frantic- in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2004, 12:38 PM
  3. comparing strings
    By GanglyLamb in forum C Programming
    Replies: 5
    Last Post: 11-03-2002, 08:01 AM
  4. Comparing Strings
    By face_master in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2002, 10:42 AM
  5. comparing strings
    By gammacad in forum C Programming
    Replies: 3
    Last Post: 06-15-2002, 06:07 PM

Tags for this Thread