comparing strings

 
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.3 KB)
Ahri	380	80	50	mage	mid	taunt	none
Akali	425	85	53	hybrid	mid	none	slow
Alistar	442	102	55	tank	early	stun	none
Amumu	472	84	47	tank	late	stun	none
Anivia	350	70	48	mage	mid	slow	slow
Annie	348	76	48	mage	all	stun	slow
Ashe	395	79	46	ranged	late	stun	slow
Blitzcrank	468	100	55	bruiser	mid	stun	silence
Brand	380	76	52	mage	all	stun	none
Caitlyn	390	80	47	ranged	mid	snare	slow
Cassiopia	380	75	47	mage	mid	stun	slow
Chogath	440	80	54	tank	late	knock-up	silence
Corki	375	82	48	ranged	late	none	none
Dr.Mundo	433	89	56	tank	late	none	slow
Evelynn	414	86	48	hybrid	mid	none	slow
Ezreal	350	80	47	hybrid	mid	none	none
Fiddlesticks	390	80	46	mage	mid	fear	none
Fiora	450	85	55	dps	late	none	none
Fizz	414	86	53	mage	mid	knock-up	slow
Galio	435	85	56	tank	mid	taunt	slow
Gangplank	495	81	54	dps	late	none	slow
Garen	455	96	58	dps	late	none	silence
Gragas	404	89	56	mage	mid	knock-up	slow
Graves	410	84	51	ranged	late	none	slow
Heimerdinger	350	75	49	mage	early	stun	slow
Irelia	430	85	53	bruiser	late	stun	slow
Janna	356	78	49	support	mid	knock-up	slow
Jarvan_IV	420	90	50	bruiser	late	knock-up	slow
Jax	463	88	56	hybrid	late	stun	none
Karma	410	86	50	support	mid	none	slow
Karthus	390	75	42	mage	mid	none	slow
Kassadin	433	78	52	mage	mid	none	slow,silence
Katarina	395	83	53	mage	late	none	none
Kayle	418	93	53	hybrid	mid	none	slow
Kennen	403	79	51	mage	mid	stun	none
Kogmaw	440	84	46	ranged	late	none	slow
Leblanc	390	75	51	mage	mid	none	silence
LeeSin	428	85	56	bruiser	late	knock-up	slow
Leona	430	87	55	tank	mid	stun	slow
Lux	345	79	50	mage	mid	snare	slow
Malphite	423	82	56	tank	late	stun	slow
Malzahar	380	80	52	mage	late	supression	none
Maokai	421	90	58	tank	mid	snare	none
Master_Yi	444	86	55	dps	late	none	none
Miss_Fortune	435	85	47	ranged	late	none	slow
Mordekaiser	421	80	52	mage	late	none	none
Morgana	403	86	52	mage	late	none	none
Nasus	410	90	53	dps	late	none	slow
Nautilus	432	86	52	tank	late	stun,knock-up	slow
Nidalee	370	90	49	hybrid	mid	none	none
Nocturne	440	85	54	dps	late	fear	none
Nunu	437	108	51	tank	mid	none	slow
Olaf	441	93	54	dps	late	none	slow
Orianna	385	79	44	mage	mid	knock-up	slow
Pantheon	433	87	51	dps	late	stun	none
Poppy	423	81	56	dps	mid	stun	none
Rammus	420	86	55	tank	late	taunt	none
Renekton	426	87	53	bruiser	late	stun	none
Riven	414	86	54	bruiser	late	stun	none
 
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..
}
 
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/

 
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

 
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!!!

 
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.

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: