| | |
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:
Solved Threads: 0
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++.
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.
C++ Syntax (Toggle Plain Text)
#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.
Last edited by Narue; Jan 22nd, 2009 at 9:22 am. Reason: added code tags
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 Build your entire system expression like that, and then pass it like this
Chris
This is possible, create a string with the path and extension such as this
C++ Syntax (Toggle Plain Text)
string path = "C:\\"; path += (rand()%5+1)+'0'; path += ".jpg";
system(path.c_str()) Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
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
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
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.
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 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
C++ Syntax (Toggle Plain Text)
string path = "\"C:\\Documents and Settings\\My Documents\\";
C++ Syntax (Toggle Plain Text)
string path = "\"C:\\Documents and Settings\\Music\\My Documents\\"; path += (rand()%5+1)+'0'; path += ".jpg\""; system(path.c_str());
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
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Ran
0
#5 Jan 20th, 2009
Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random
0
#6 Jan 21st, 2009
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
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.
random generate num : 2
"" 0
add them together so they are 20.
Any ideas?
Thanks for all the help and being patient with me.
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 Chris
C++ Syntax (Toggle Plain Text)
#include <sstream> ... ostringstream oss; oss << "\"C:\\Documents and Settings\\"; oss << rand()%1000 + 1; oss << ".jpg\""; system(oss.str().c_str());
Knowledge is power -- But experience is everything
•
•
Join Date: Nov 2008
Posts: 90
Reputation:
Solved Threads: 8
Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random
0
#9 Jan 21st, 2009
Re: Random Image + Calling The Image, Wondering If It Is Possible And How To Do A Random
0
#10 Jan 21st, 2009
Knowledge is power -- But experience is everything
![]() |
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





