why this code not playing voice of cat i.e. when selected CAT from ListBox and p

Started by Nabeel Nazir, 09 September 2013, 08:22:32

Nabeel Nazir

why this code not playing voice of cat i.e. when selected CAT from ListBox and press speak button ?


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
//button->setCallbackId(1);
//button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

tgui::ListBox::Ptr listBox(gui);
listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
listBox->addItem("LION");
listBox->addItem("GOAT");
listBox->setCallbackId(2);
//listBox->getSelectedItem();
//listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);
while (gui.pollCallback(callback))
{


/* listBox->bindCallback(tgui::ListBox::ItemSelected);
listBox->setCallbackId(2);*/

sf::SoundBuffer buffer;
if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{
if (!buffer.loadFromFile("cat.wav"))
{
std::cout << "Error while loading music" << std::endl;
}
else
{
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
}

}
}

}



    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
}
return EXIT_SUCCESS;
}






texus

You are recreating the SoundBuffer every frame.

The sound is temporary as well.
else {
    sf::Sound sound;        // <-  Sound object gets created
    sound.setBuffer(buffer);
    sound.play();
}   // <- Sound object gets destroyed as it goes out of scope


You should declare the SoundBuffer and Sound objects at the beginning of the main function.
So these lines should be moved to earlier in your program (anywhere above the main loop).
sf::SoundBuffer buffer;
sf::Sound sound;

Nabeel Nazir

#2
 why this code not playing voice of cat i.e. when selected CAT from ListBox and press speak button (its not working now agaain)!
I PLACE MY CAT.WAV FILE AT THIS PATH 
C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{
sf::SoundBuffer buffer;
sf::Sound sound;
sound.setBuffer(buffer);

    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
//button->setCallbackId(1);
//button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

tgui::ListBox::Ptr listBox(gui);
listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
listBox->addItem("LION");
listBox->addItem("GOAT");
listBox->setCallbackId(2);
//listBox->getSelectedItem();
//listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
if(event.key.code==sf::Keyboard::P)
sound.play();
            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
}

while (gui.pollCallback(callback))
{


/* listBox->bindCallback(tgui::ListBox::ItemSelected);
listBox->setCallbackId(2);*/

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{
if (!buffer.loadFromFile("cat.wav"))
{
std::cout << "Error while loading music" << std::endl;
}
else
{
sound.play();
}

}
}

}



return EXIT_SUCCESS;
}







texus

Does it print something in the command prompt? Its probably going to say that cat.wav can't be found.

If I remember correctly, then for Visual Studio you have to place the file in either "C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Debug" or "C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Release" depending on whether you are building in debug or release mode.

Nabeel Nazir

 :(  :(

texus

Seems like the wav file is currupted. Try with another file and see if it works.

Nabeel Nazir

i have placed cat.wav file now at both addresses!! Still its not working !
C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Debug
C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Release


Nabeel Nazir

now i have downloaded another new wave file from this source https://soundbible.com/1287-Cat-Meow.html
now my program is giving this kind of error ! :(

texus

You'll have to wait till someone responds on the sfml forum, because I can't help with that.

Nabeel Nazir

hmmmmmmmmm !  :-[

Nabeel Nazir

 i didn't get the last reply of SFML developers team ! the link for the reply is as follows.
https://en.sfml-dev.org/forums/index.php?topic=12904.msg90497#msg90497

texus

I guess you understand it by now with the other reply on the sfml forum?

Nabeel Nazir

yes i got that .... NOW THERE IS NO ERROR ON CONSOLE ... BUT STILL IT IS NOT PLAYING THE VOICE OF CAT WHEN CAT ITEM IS SELECTED FROM LISTBOX AND SPEAK BUTTON HAS BEEN PRESSED ! DONT KNOW WHAT DO TO ! :(

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}


int main()
{

sf::SoundBuffer buffer;
sf::Sound sound;
//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
//button->setCallbackId(1);
//button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

tgui::ListBox::Ptr listBox(gui);
listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
listBox->addItem("LION");
listBox->addItem("GOAT");
listBox->setCallbackId(2);
//listBox->getSelectedItem();
//listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
/*if(event.key.code==sf::Keyboard::P)
sound.play();*/
            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
}

while (gui.pollCallback(callback))
{


/* listBox->bindCallback(tgui::ListBox::ItemSelected);
listBox->setCallbackId(2);*/

//std::cout<<"heloooo!";
if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{
//std::cout<<"heloooo!";
if (!buffer.loadFromFile("Cat Meow.wav"))
{
sound.setBuffer(buffer);
std::cout << "Error while loading file" << std::endl;
}
else
{
sound.play();
}

}
else
{
std::cout<<"aceesing CAT failed!"<<std::endl;

}
}
else
{
std::cout<<"id is not valid!!!!"<<std::endl;


}

}


//getch();
return EXIT_SUCCESS;
}







texus

Please post on either the sfml forum or on this forum but not on both.
There is no need to ask the same question twice, by the time one replies you might already have a reply on the other forum.

I read both forums anyway.


Nabeel Nazir

hi, now i have to add an edit box and a button namely "Add Animal " , my requirement is to write any name of animal in the edit box and press "Add Animal " button and that particular animal should be added in the list box ! ... ALSO--> is there any way that when new animal will be added it's voice i.e.  .wav file will be added automatically at back end and when speak button is pressed it's voice will be played ?

texus

Adding an edit box is the same as adding a button or a list box:
tgui::EditBox::Ptr editBox(gui);
editBox->load("...");

And then look at the documentation for the other functions you can use. But you probably only need setPosition and setSize.

When the "Add animal" button gets pressed, you can use the addItem function to add the contents of the edit box to the list box (editBox->getText() for getting text in edit box).

The animal will be added to the back of the list box, but it still has to be selected.
listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

----------
What follows below is a suggestion of what you can do to play the sound. I didn't test it as I wrote it so it might contain mistakes, and it definately isn't the only way. But you might get some ideas from it.

Now you can even make the sound load dynamically. At the beginning of the program you define this:
std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;


When adding a new item to the list box, e.g. with the "Add animal" button then you load the sound. This has the limitation that the sound needs to have the exact name as the animal though (if CAT is entered in the edit box then it will load CAT.wav).
buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);


And then when the speak button is pressed, you play the correct sound. There are just as many sounds as there are items in the list box, and e.g. the third item in the list box matches the third sound in the list, so you can use listBox->getSelectedItemIndex().
auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();

Nabeel Nazir

what i am doing wrong here, i am adding the new animal in the edit box and when press add animal button it will be added in the list (good with it !) but when i close my window and compile my code again it will not show the recently added animal in the list box !

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{
char name[50];
sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);


tgui::ListBox::Ptr listBox(gui);
listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
    listBox->addItem("COW");
    listBox->addItem("CAT");
    //listBox->addItem("DONKEY");
listBox->addItem("LION");
listBox->addItem("GOAT");
listBox->setCallbackId(2);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);

tgui::EditBox::Ptr editBox(gui);
    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("Lion Roar.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("Goat Bah.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("Cat Meow.wav");
buffer3.loadFromFile("Single Cow.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}

}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
else if(callback.id==2)
{
//editBox->getText();
listBox->addItem(editBox->getText());
listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box
}

}


//////////////////////////////////////////////////////
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}





//getch();
return EXIT_SUCCESS;
}






texus

Quotebut when i close my window and compile my code again it will not show the recently added animal in the list box !
Obviously. What you want to do is save a the contents of the list in a text file and load it again when starting the program (look at std::ifstream and std::ofstream).


Nabeel Nazir

can i store the selected item from listBox in a character array or any character variable so that i may pass this character array in addItem() function of listBox, and it will store new animal name in the listBox !?

texus

Quotehmmm, i didn't get you !
You want to be able to keep your added animals even after the program was quit, right?

So at the beginning of the program you have something like this:
std::ifstream ifile("animals.txt");
if (ifile.is_open())
{
    // Read the contents of the file and put the read animals in the listbox with addItem
}


At the end of the program you save the animals that you have in your listbox to a file, so that the next time you start the program you will still have all animals.
std::ofstream ofile("animals.txt");
if (ifile.is_open())
{
    auto& items = listBox->getItems();
    for (auto& item : items)
    {
        ofile << item.toAnsiString() << std::endl;
    }
}


Again, this code is only for guidance and may contain mistakes. I also deliberately left out the way to load the text file so that I don't give you the complete solution.

Edit: just realized that that for loop is only supported since VS2012. It will be like this for you:
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
    ofile << it->toAnsiString() << std::endl;
}

texus

Quotecan i store the selected item from listBox in a character array or any character variable so that i may pass this character array in addItem() function of listBox, and it will store new animal name in the listBox !?
I have no idea what you mean with this, but lets break it down in smaller parts.

Quotecan i store the selected item from listBox in a character array
std::string selectedItem = listBox->getSelectedItem();

Quoteso that i may pass this character array in addItem() function of listBox
listBox->addItem(selectedItem);

Nabeel Nazir

sorry i wrote it little bit wrong i should have ask like this "can i store the animal name written in editBox in a string and then pass this string to addItem() function of listBox so that it may be permanently store in the listBox as an item" !
I do it like this in my code , but its not saving the item in the listBox permanently i.e. when compiling my code second time the newly added animal name disappears from listBox ! ...


#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>
#include <cstdio>
#include <fstream>
//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
// {
// //std::cout << "Hello world" << std::endl;
// }
//
//
//
//}

int main()
{
std::string name;
/*tgui::ListBox::Ptr listBox(gui);
std::string name;
std::ifstream ifile("animals.txt");
if(ifile.is_open())
{
while(!ifile.eof())
{
ifile>>name;
ifile.ignore(10,'\n');
listBox->addItem(name);

}

}
else
{
std::cout<<"ifile.txt does not open properly!"<<std::endl;
}*/
//std::string name;
/*int n=20;
char *name=new char[n];*/
std::list<sf::SoundBuffer> buffers;
std::list<sf::Sound> sounds;

sf::SoundBuffer buffer;
sf::SoundBuffer buffer1;
sf::SoundBuffer buffer2;
sf::SoundBuffer buffer3;
sf::SoundBuffer buffer4;
sf::SoundBuffer buffer5;
sf::Sound sound;

//sound.setBuffer(buffer);
sound.setPitch(1.5f);
sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");



    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button->setPosition(280, 250);
    button->setText("SPEAK");
    button->setSize(200, 40);


tgui::ListBox::Ptr listBox(gui);
listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 30);
    listBox->addItem("COW");
    listBox->addItem("CAT");
    //listBox->addItem("DONKEY");
listBox->addItem("LION");
listBox->addItem("GOAT");
listBox->setCallbackId(2);

tgui ::Callback callback;

button->bindCallback(tgui::Button::LeftMouseClicked);
button->setCallbackId(1);

tgui::EditBox::Ptr editBox(gui);
    editBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
    editBox->setBorders(6, 4, 6, 4);
    editBox->setPosition(230, 350);
    editBox->setText("Add Animal Name Here!");
editBox->setSize(360, 40);

tgui::Button::Ptr button1(gui);
    button1->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
button1->setPosition(280, 450);
    button1->setText("Add Animal");
    button1->setSize(200, 40);

button1->bindCallback(tgui::Button::LeftMouseClicked);
button1->setCallbackId(2);

buffer.loadFromFile("Lion Roar.wav");
//sound.setBuffer(buffer);
buffer1.loadFromFile("Goat Bah.wav");
//sound.setBuffer(buffer1);
buffer2.loadFromFile("Cat Meow.wav");
buffer3.loadFromFile("Single Cow.wav");
//sound.setBuffer(buffer2);

//   //tgui::Button::Ptr button(gui);
//   button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black .conf");
//button->setPosition(380, 350);
//   button->setText("Add");
//   button->setSize(200, 40);


    while (window.isOpen())
    {
/////////////////////////////////////////////////////

while (gui.pollCallback(callback))
{

if(callback.id==1)
{
if (listBox->getSelectedItem()=="CAT")
{

sound.setBuffer(buffer2);
sound.play();

}

else if (listBox->getSelectedItem()=="LION")
{

sound.setBuffer(buffer);
sound.play();

}
else if (listBox->getSelectedItem()=="GOAT")
{
sound.setBuffer(buffer1);
sound.play();
}
else if(listBox->getSelectedItem()=="COW")
{
sound.setBuffer(buffer3);
sound.play();

}

}
/*else
{
std::cout<<"id is not valid!!!!"<<std::endl;
}*/
else if(callback.id==2)
{
//editBox->getText();

//listBox->setSelectedItem(listBox->getItems().size()-1); // Select the last item in the list box

name=editBox->getText();
listBox->addItem(name);


/*buffers.push_back(sf::SoundBuffer());
buffers.back().loadFromFile(editBox->getText() + ".wav");

sounds.push_back(sf::Sound());
sounds.back().setBuffer(buffers.back());
sounds.back().setPitch(1.5f);
sounds.back().setVolume(80);

auto it = sounds.begin();
std::advance(it, listBox->getSelectedItemIndex());
it->play();*/


}


}


//////////////////////////////////////////////////////
       
sf::Event event;
        while (window.pollEvent(event))
        {

if (event.type == sf::Event::Closed)
window.close();

// Pass the event to all the widgets (if there would be widgets)
gui.handleEvent(event);
}

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();

}
/*std::ofstream ofile("animals.txt");

if(ifile.is_open())
{

while(!ofile.eof())
{
ofile << .toAnsiString() << std::endl;
}
}*/





//getch();
return EXIT_SUCCESS;
}





Nabeel Nazir

Second solution you told me was difficult for me to code .. i have to complete this assignment as soon as possible .. is there any way other then file handling! ?
You can see in my above code i tried it (reading/writing from file i.e. file handling).. But it was not running well!