I was wondering if it was possible to call a random image in c++. I'm using Dev C++.
#include <time.h>
#include <stddef.h>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const string password("password");
int main()
{
string x;
cout << "Type the password to gain access to the program: ";
cin >> x;
if(x==password){
cin.clear();
system("cls");
int num2 = 0;
srand((unsigned)time(0));
int random_integer;
int lowest=1, highest=10;
int range=(highest-lowest)+1;
for(int index=0; index<2; index++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
num2 = random_integer;
if (num2 == 1)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1221344450654.jpg\"");
}
if (num2 == 2)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1232073306448.jpg\"");
}
if (num2 == 3)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1232073519518.jpg\"");
}
if (num2 == 4)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231650828321.jpg\"");
}
if (num2 == 5)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231631886317.jpg\"");
}
if (num2 == 6)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231631292685.jpg\"");
}
if (num2 == 7)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231625357305.jpg\"");
}
if (num2 == 8)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231617324935.jpg\"");
}
if (num2 == 9)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231611926870.jpg\"");
}
if (num2 == 10)
{
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231603324024.jpg\"");
}
else{
// cout << num2 << endl;
}
}
else{
cout << "Wrong password!\n";
system("pause");
}
// system("pause");
return 0;
}
This is what I have so far. I know I can do random images with a bunch of else if loops, but I'm wondering if it is possible to do random images without all of this else if loops.
What I was wondering about is to generate a random number and insert it into the call.
For example:
Random Number is 2
system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\2.jpg\"");
instead of doing an if else statement for that, just have the 2 be place into there some how.
Is this possible or am I just crazy?
Thanks for the help.