Storing the values of a switch statement in a variable C++?

Status
Not open for further replies.

Tech2011

Solid State Member
Messages
13
Hello gang!
I have another thought provoking question about a C++ program I have designed.
I have made a C++ program in which it uses a switch case statement along with a seeded PRNG to make a random string of symbols, integers, and letters. I want to store the random string in a file, but I need to store the strings in a variable. My code shown below:

srand((unsigned)time(0));
int bit=rand() % 9 + 1;
cout << "Random string: ";
switch (bit){
case 1:
cout << "S" << rand() % 99999 << "/" << rand() % 999999 <<"A~D" << "\n" ;
break;
case 2:
cout << "PoP" << rand() % 99999 << "7(Y" << rand() % 99999 << "\n" ;
break;
case 3:
cout << rand() % 99999 << "Tk-i" << rand() % 99999<< "\n" ;
break;
case 4:
cout << "&" << rand() % 99999 << "V" << rand() % 99999 <<"#W"<<"\n" ;
break;
case 5:
cout << "@R" << rand() % 99999 << rand() % 99999 <<"yK"<< "\n" ;
break;
case 6:
cout << rand() % 99999 << "PhP/d" << rand() % 99999<< "\n" ;
break;
case 7:
cout << rand() % 99999 <<"J" << rand() % 99999 <<"Fl"<< "\n" ;
break;
case 8:
cout << rand() % 99999 << ";" << rand() % 99999 <<"W!-]" << "\n" ;
break;
case 9:
cout << "*Q" << rand() % 99999 << "mR=" << rand() % 99999 << "\n" ;
break;
default:
cout << "String could not be generated because the default switch was reached." <<bit;
break;
}
ofstream a_file("Random strings.txt",ios::app);

Is there anyway to possibly store the random string in a variable so I could write out the string into a file? Anybody's thoughts would be extremely helpful!
Thanks in advance!
Tech2011
 
You would need to convert the int values to string values, then you could concatenate them all together and put them in a string variable.

Here's one possible way to convert the ints to strings:
Code:
#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
 
Status
Not open for further replies.
Back
Top Bottom