We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

comparing strings

I am trying to compare a string from my text file to a user input i was wondering how I would do this I have tried the == operator but that seems to be getting me no where. I am currently trying to use the .compare() but it doesn't seem to be working can someone help me out it would be much appreciated.

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

class The_League
{

    string champ_type;
    string fav_stage;
    string disable;
    int hitpoints;
    string crwd_ctrl;

    int health;
    int damage;
    int health_perlvl;

    string name;
    string type;
    string stage;
    string disables;
    string crowd_ctrl;
    string weakness;


    public:
        The_League();
        The_League(string type, string stage, string disables, int health, string cc);
        void sort_Champions();
        void In_Out_Stage1();
        void screen_Output();
        void dps_champions();

};


The_League::The_League()
{

}

The_League::The_League(string type, string stage, string disables, int health, string cc)
{
    champ_type = type;
    fav_stage = stage;
    disable = disables;
    hitpoints = health;
    crwd_ctrl = cc;

}


void The_League::sort_Champions()
{
    ifstream roster("LolRoster.txt");

    while(roster>>name>>health>>damage>>health_perlvl>>type>>stage>>disables>>crowd_ctrl)
    {
        if(type == champ_type)
        {
          cout<<type;
        }
    }

}

void The_League::In_Out_Stage1()
{

}

void The_League::screen_Output()
{

}

int main()
{
    string champ_type;
    string fav_stage;
    string disable;
    int hitpoints;
    string crwd_ctrl;

    The_League obj(champ_type, fav_stage, disable, hitpoints, crwd_ctrl);



    cout<<"What type of champ do you like? \n1. dps\n2. bruiser\n3. mage\n4. support\n5. tank\n6. ranged"<<endl;
    cin>>champ_type;

    cout<<"2. What stage of the game is your favorite? \n1. ealry\n2. mid\n3. late"<<endl;
    cin>>fav_stage;

    cout<<"3. what is your favorite disable? \n1. stun\n2. snare\n3. taunt\n4. fear\n5. knock-up"<<endl;
    cin>>disable;

    cout<<"4. Do you prefer champs with MOAR Health (1. yes, 0.no)?"<<endl;
    cin>>hitpoints;

    cout<<"5. Do you prefer champions with crowd control (1. yes, 0.no)"<<endl;
    cin>>crwd_ctrl;

    obj.sort_Champions();


    return 0;
}
Attachments LolRoster.txt (2.3KB)
4
Contributors
6
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
7
Views
Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
bool StringsEqual(string Str1, string Str2)
{
    if (Str1.size() != Str2.size())              //If the sizes of the strings are different.. then they are of course not the same..
        return false;

    for (unsigned I = 0; I < Str1.size(); I++)  //We established they are the same size if we got this far..
    {
        if (Str1[I] != Str2[I])                 //If at least 1 character in the strings are different, then they are of course not the same..
            return false;
    }
    return true;                               //If all else succeeded then the strings are definitely the same..
}
triumphost
Practically a Master Poster
625 posts since Oct 2009
Reputation Points: 59
Solved Threads: 55
Skill Endorsements: 1

I am currently trying to use the .compare() but it doesn't seem to be working can someone help me out it would be much appreciated.

which part are you trying to compare?
Did you check already how compare works?
if not this link might help http://www.cplusplus.com/reference/string/string/compare/

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

which part are you trying to compare?
Did you check already how compare works?
if not this link might help http://www.cplusplus.com/reference/string/string/compare/

I'm trying to compare the user input of champ_type with the type on the text file (dps, tank, mage, etc.) and when the user enters the champ_type it will only print out champs with that type

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
bool StringsEqual(string Str1, string Str2)
{
    if (Str1.size() != Str2.size())              //If the sizes of the strings are different.. then they are of course not the same..
        return false;

    for (unsigned I = 0; I < Str1.size(); I++)  //We established they are the same size if we got this far..
    {
        if (Str1[I] != Str2[I])                 //If at least 1 character in the strings are different, then they are of course not the same..
            return false;
    }
    return true;                               //If all else succeeded then the strings are definitely the same..
}

Thank you thank you thank you!!!! works perfect!!!

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The reason why the code that you posted doesn't work is because the champ_type string that you obtain from the user and the one that is inside the object "obj" are not the same. The one you obtain from the user is a local variable in the main() function, which is not the one inside the object "obj".

If you move the obj initialization (line 85) to after you get the input from the user (after line 102), all should work as expected.

mike_2000_17
21st Century Viking
Moderator
3,144 posts since Jul 2010
Reputation Points: 2,062
Solved Threads: 626
Skill Endorsements: 41

The reason why the code that you posted doesn't work is because the champ_type string that you obtain from the user and the one that is inside the object "obj" are not the same. The one you obtain from the user is a local variable in the main() function, which is not the one inside the object "obj".

If you move the obj initialization (line 85) to after you get the input from the user (after line 102), all should work as expected.

Thanks for the help I eventually figured it out but reassurance is always appreciated

Prisms
Light Poster
30 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0842 seconds using 2.71MB