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.

please use [code=language] [/code] tags.

This is possible, create a string with the path and extension such as this

string path = "C:\\";
path += (rand()%5+1)+'0';
path += ".jpg";

Build your entire system expression like that, and then pass it like this system(path.c_str()) Chris

Thanks for the help. It works your way. The only thing is that if I put the file path as I had it, it can't seem to do anything with that.

I did create a folder though on at C:\Images and it worked, but if I tried docuemtns and settings it didn't work.

Any ideas why?

Thanks for the help.

P.S. Could you explain this part of your code?

path += (rand()%5+1)+'0';

thanks

You probably forgot what you did in your original code.

string path = "\"C:\\Documents and Settings\\My Documents\\";

You need to encompass the address in quote marks, which means escaping them. Note the final quote will be after the extension so your code would be like this

string path = "\"C:\\Documents and Settings\\Music\\My Documents\\";
path += (rand()%5+1)+'0';
path += ".jpg\"";
system(path.c_str());

The segmant of code you asked about generates a number between 1 and 5, which i'm sure you are aware about. But since we wish to add the character value of that number to the string not the numerical value, we need to increaes its ascii code by 48 or '0' so that we get its character value. If that makes sense, try removing the +'0' and see what happens :P You'll get a bit of a surprise.

Chris

Thanks that helped a lot. I just want to make sure on the random numbers part. If I wanted thew random numbers from 1 to 20 then I would change that 5 to a 20, right?

Thanks

It works, the only thing is that on the ascii scale there is no 20 or so. It goes into the alpha and symbols instead of doing numbers. I haven't messed with the ascii scale too often so I guess to fix this I just need to add something or break the code up and have it do something like this.

random generate num : 2
"" 0

add them together so they are 20.

Any ideas?

Thanks for all the help and being patient with me.

The best way would be to use string streams

#include <sstream>
...
ostringstream oss;
oss << "\"C:\\Documents and Settings\\";
oss << rand()%1000 + 1;
oss << ".jpg\"";

system(oss.str().c_str());

Chris

Some of your header files are missing .h extensions

This is C++ not C
Infact he has extra header file that he shouldn't have such as time.h

Please try to get things reasonbly accurate before making a point of it

Chris

Thanks a lot Freaky_Chris

That worked like a charm. The only thing is that if you run it more than once it is always the same number, so I added a line of code so it would change.

#include <sstream>
using namespace std;

int main() 
{ 
      srand((unsigned)time(0)); 
/*
    int random_integer; 
    int lowest=1, highest=10; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<20; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
       // cout << random_integer << endl; 
   }
*/
ostringstream oss;
oss << "\"C:\\Images\\";
//oss << "\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\";
oss << rand()%25 + 1;
oss << ".jpg\"";

system(oss.str().c_str());
system("pause");
return 0;
}

Yea, I know about my heading. Sorry for the bad heading stuff, but I just been copying and pasting from other programs and trying stuff out.

Thanks for all of the help. I really appreciate it.

jdm

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.