C ++比较两个字符串坏代码

时间:2015-11-22 12:46:38

标签: c++ compare

我的代码有问题..我不知道有什么问题。我需要比较两个字符串。错误信息: 错误:'outputFile'没有命名类型。

我正在处理文件..在其中写入一些文本...我的比较代码:

   public Menu()
    {
        menu();
    }

    public void menu()
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please type in a number and press enter");
        System.out.println("1. Add animal");
        System.out.println("2. Show animals");

        if (sc.hasNextInt())
        {
            int input = sc.nextInt();

            if (input >= 0 && input < 10)
            {
                if (input == 1)
                {
                    addAnimal();
                }
                else if (input == 2)
                {
                    showAnimals();
                }

            }
            else
            {
                System.out.println("Please enter a number between the range");
                menu();
            }
        }
        else
        {
            System.out.println("Wrong input, please input a number!");
            menu();
            System.out.println();
        }

    }

    private void addAnimal()
    {
        System.out.println("ADD ANIMAL");
        System.out.println();

        Scanner sc = new Scanner(System.in);

        try
        {
        System.out.println("Name: ");
        String name = sc.nextLine();

        System.out.println("Kind of animal: ");
        String kindOfAnimal = sc.nextLine();


        System.out.println("Current age: ");
        int currentAge = sc.nextInt();

        System.out.println("Expected age: ");
        int expectedAge = sc.nextInt();

        Animal animal = new Animal(name, kindOfAnimal, currentAge, expectedAge);

//        animals.add(animal.toString());

        animal.setAnimalList(animal);
        }

        catch (Exception e)
        {
            System.out.println("Please enter the correct information");
        }
        System.out.println();
        menu();
    }

    public void showAnimals()

    {

            Animal animal = new Animal(null, null, 1, 1);

        if (animal.getAnimals().isEmpty())
        {
            System.out.println("There is no animals yet!");
            menu();
        }
        else
        {
        for (int i = 0; i < animal.getAnimals().size(); i++)
        {
            System.out.println(animal.getAnimals());

        }
        menu();
        }
    }

功能:

for (int i = 0; i < size; i++){
            if (PorovnaniKategorie(ArrayOfWorlds[i],"mzda")==true){
//do some code
            }
            }
        }

它正在单独工作,当我从主要功能中删除循环时,一切正常......我不知道如何解决这个问题..

实际上我只需比较索引i的值是否与“mzda”

相比较

编辑:

bool PorovnaniKategorie(string s1, string s2){
    bool porovnani1=true;
    int vel1 = s1.size();
    int vel2 = s2.size();
if (vel1 == vel2)
                    {
                    for ( int i = 0; i < vel1; i++)
                    {
                        if(tolower(s1[i])!= tolower(s2[i]))
                           porovnani1 = false;
                          }
                    }
else{
    porovnani1 = false;
}
return porovnani1;
}

我不认为错误在这里..(此代码正在进行中)。当我删除所有正在工作

1 个答案:

答案 0 :(得分:1)

纠正一个。请在谷歌“std string compare ignore case”中询问。

bool PorovnaniKategorie( string s1, string s2 )
{
    if( s1.size() == s2.size() ) {
        for( std::string::size_type i = 0; i < s1.size(); i++ ) {
            if( tolower( s1[i] ) != tolower( s2[i] ) ) {
                return false;
            }
        }
        return true;
    }
    return false;
}