show pictures randomly

i was making a quiz and i want to show some pictures as questions to recognize i but i don't know how to show different pictures without listing the names of the files in the program, i mean i will generate a random no and based on the no the file name should change in the code like if the no is 1 it should find '1.jpg' and show it and if the no next time comes 23 then it should find '23.jpg'. can someone tell me how to do that?
For random numbers see http://www.cplusplus.com/reference/random/

For converting the random number to string http://www.cplusplus.com/reference/string/to_string/

For assigning values and string contact, to be able to generate 1.jpg etc read about strings http://www.cplusplus.com/reference/string/string/

Actually loading the images and showing them is different game altogether
ya, but i know that i am doing it with sfml...
k thanks..i ll read the above pages...
closed account (D80DSL3A)
...i don't know how to show different pictures without listing the names of the files in the program

This problem seems separate from the random display issue. If I have a list of file names needed in a program I will put those names in one file, then hard code the name of that one file in the program. Although, if the file names really are as you have shown, you could generate the names in the program as codewalker outlined above.

Taking the 1st approach, as it's more general. The filenames can be any legal name.

Suppose we have a fileNames.txt with the following content
1.jpg
2.jpg
3.jpg


Put no newline following the last line, OK?

1
2
3
4
5
std::vector<std::string> imageFileNames;
std::ifstream fin("fileNames.txt");
std::string temp;
while( fin >> temp )
   imageFileNames.push_back( temp );


That should get all the image file names loaded into a vector.

I assume you can generate your random #'s OK (say, unsigned int randomIndex). I'll assume that they fall in the range 0 to imageFileNames.size() - 1, inclusive.

You can postpone loading the images until you need to display them.
Assuming we also have a vector of default constructed sf::Image:
std::vector<sf::Image> imageVec( imageFileNames.size(), sf::Image() );// create vector with all needed sf::Images in it already

Then, when it's time to draw an image:
1
2
3
4
5
6
7
8
if( imageVec[randomIndex].getPixelsPtr() == NULL )// image has not been loaded yet. Assuming SFML v2.1 here
    imageVec[randomIndex].loadFromFile(  imageFileNames[randomIndex].c_str() );

theSprite.setTexture( imageVec[randomIndex] );// theSprite is whatever sprite you're using to draw an image through

// Draw it. rw is your sf::RenderWindow instance
rw.draw( theSprite );


Last edited on
it shows some error in the line
 
while( fin >> temp )

fin is an ifstream and temp is a string??
what can i do?
You shouldn't get errors. Will you be more specific please?
OK...i got it...
can u tell me which line says 'trying to access the pixels of an empty image'?
it shows two images and then it shows an error:
'vector subscript out of range'
help me...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// show pic random.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<SFML\Graphics.hpp>
using namespace std;

int main()
{
	sf::RenderWindow window(sf::VideoMode(800, 600, 32),"Title");
	sf::Image Image;
	sf::Sprite Sprite;
	
	std::vector<std::string> imageFileNames;
	std::ifstream fin("filename_photo.txt");
	std::string temp;
	while( fin >> temp )
		imageFileNames.push_back( temp );

	std::vector<sf::Image> imageVec( imageFileNames.size(), sf::Image() );

	int min=1,max=5;
	for(int i=1; i<=5; i++)
	{
		fin.seekg(0);
	int randNum = (rand()%(max-min + 1)) + min;

	if( imageVec[randNum].GetPixelsPtr() == NULL )// image has not been loaded yet. Assuming SFML v2.1 here
		imageVec[randNum].LoadFromFile(  imageFileNames[randNum].c_str() );

	if(!Image.LoadFromFile(imageFileNames[randNum].c_str()))
		cout << "error" << endl;

	Sprite.SetImage( imageVec[randNum] );// theSprite is whatever sprite you're using to draw an image through

	// Draw it. rw is your sf::RenderWindow instance
	window.Draw( Sprite );

	
	//Sprite.SetImage(Image);
	//window.Draw(Sprite);
	window.Display();
	sf::Sleep(10);
	//system("pause");
	}
	return 0;
}

//sf::Image Image; 
You do realise that vectors start at position 0? You may want to change your random number generation to something like this:
1
2
3
4
5
for (int i = 0; i < 5; ++i) {
    int randNum = rand() % imageFileNames.size(); // never get an overflow

    // ...
}


Just a note - the way you will do it will display images 5 times, and not necessarily all the images (unless there is only one image). Was this what you wanted?
i got what i wanted thanks...
Topic archived. No new replies allowed.