Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #1
Jan 20th, 2009
I was wondering if it was possible to call a random image in c++. I'm using Dev C++.

  1. #include <time.h>
  2. #include <stddef.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <ctime>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. const string password("password");
  11.  
  12. int main()
  13. {
  14.  
  15. string x;
  16.  
  17. cout << "Type the password to gain access to the program: ";
  18. cin >> x;
  19. if(x==password){
  20. cin.clear();
  21. system("cls");
  22.  
  23. int num2 = 0;
  24. srand((unsigned)time(0));
  25. int random_integer;
  26. int lowest=1, highest=10;
  27. int range=(highest-lowest)+1;
  28. for(int index=0; index<2; index++){
  29. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  30. }
  31.  
  32. num2 = random_integer;
  33.  
  34. if (num2 == 1)
  35. {
  36. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1221344450654.jpg\"");
  37. }
  38.  
  39. if (num2 == 2)
  40. {
  41. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1232073306448.jpg\"");
  42. }
  43.  
  44. if (num2 == 3)
  45. {
  46. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1232073519518.jpg\"");
  47. }
  48.  
  49. if (num2 == 4)
  50. {
  51. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231650828321.jpg\"");
  52. }
  53.  
  54. if (num2 == 5)
  55. {
  56. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231631886317.jpg\"");
  57. }
  58.  
  59. if (num2 == 6)
  60. {
  61. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231631292685.jpg\"");
  62. }
  63.  
  64. if (num2 == 7)
  65. {
  66. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231625357305.jpg\"");
  67. }
  68.  
  69. if (num2 == 8)
  70. {
  71. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231617324935.jpg\"");
  72. }
  73.  
  74. if (num2 == 9)
  75. {
  76. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231611926870.jpg\"");
  77. }
  78.  
  79. if (num2 == 10)
  80. {
  81. system("\"C:\\Documents and Settings\\Administrator\\New Folder\\Images\\1231603324024.jpg\"");
  82. }
  83.  
  84. else{
  85. // cout << num2 << endl;
  86. }
  87.  
  88. }
  89. else{
  90. cout << "Wrong password!\n";
  91. system("pause");
  92. }
  93.  
  94.  
  95. // system("pause");
  96. return 0;
  97. }


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.
Last edited by Narue; Jan 22nd, 2009 at 9:22 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #2
Jan 20th, 2009
please use [code=language][/code] tags.

This is possible, create a string with the path and extension such as this
  1. string path = "C:\\";
  2. path += (rand()%5+1)+'0';
  3. path += ".jpg";
Build your entire system expression like that, and then pass it like this system(path.c_str())
Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #3
Jan 20th, 2009
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
Last edited by jdm; Jan 20th, 2009 at 5:02 pm. Reason: question
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Ran

 
0
  #4
Jan 20th, 2009
You probably forgot what you did in your original code.
  1. 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
  1. string path = "\"C:\\Documents and Settings\\Music\\My Documents\\";
  2. path += (rand()%5+1)+'0';
  3. path += ".jpg\"";
  4. 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 You'll get a bit of a surprise.

Chris
Last edited by Freaky_Chris; Jan 20th, 2009 at 5:32 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Ran

 
0
  #5
Jan 20th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #6
Jan 21st, 2009
Yes you would
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: jdm is an unknown quantity at this point 
Solved Threads: 0
jdm jdm is offline Offline
Newbie Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Ran

 
0
  #7
Jan 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #8
Jan 21st, 2009
The best way would be to use string streams
  1. #include <sstream>
  2. ...
  3. ostringstream oss;
  4. oss << "\"C:\\Documents and Settings\\";
  5. oss << rand()%1000 + 1;
  6. oss << ".jpg\"";
  7.  
  8. system(oss.str().c_str());
Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #9
Jan 21st, 2009
Some of your header files are missing .h extensions
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random

 
0
  #10
Jan 21st, 2009
Originally Posted by ajay.krish123 View Post
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC