1,105,229 Community Members

comparing strings

Member Avatar
Prisms
Light Poster
31 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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)
Member Avatar
triumphost
Practically a Master Poster
674 posts since Oct 2009
Reputation Points: 81 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 6 [?]
 
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..
}
Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
0
 

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/

Member Avatar
Prisms
Light Poster
31 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Prisms
Light Poster
31 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!!!

Member Avatar
mike_2000_17
21st Century Viking
4,067 posts since Jul 2010
Reputation Points: 2,244 [?]
Q&As Helped to Solve: 798 [?]
Skill Endorsements: 72 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
Prisms
Light Poster
31 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: