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;
}

Recommended Answers

All 6 Replies

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..
}

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/

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

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.