6 Replies - 579 Views - Last Post: 15 October 2010 - 02:04 PM Rate Topic: -----

#1 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 01:36 PM

I have a game that im making and i need to figure out how to create a generator that will create random strings of characters such as ! @ # $ % ^ & * ( ) and probably at random lengths. Please help. Im teaching myself C++ and i donnot know how to create functions cause i think that might have to be included.
Is This A Good Question/Topic? 0
  • +

Replies To: How do i set up a generator to create ASCii strings

#2 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 01:42 PM

if your needing proof that i do have some work done here is what i have.


#include <iostream>
#include <vector>
#include <algorithm>
#include<ctime>
#include<cstdlib>
#include<string>

using namespace std;
//characters required #, @, %, ?, !, ^, $, &, *, (, )
int main()
{
	int choice;
	char again = 'y';
	while (again == 'y')
	{
	srand(time(0));//seeding the random number generator

	const int MAX_WRONG = 10;  // maximum number of incorrect guesses allowed

//	vector<string> store;
//	vector<string> collection;
	vector<string>::iterator iter;

	vector<string> lvl1;
	vector<string> lvl2;
	vector<string> lvl3;
	vector<string> lvl4;
	vector<string> lvl5;
	vector<string> lvl6;
	vector<string> lvl7;
	vector<string> lvl8;
	
	
	int choice;
	string input;

	/*store.push_back("snake");
	store.push_back("cat");
	store.push_back("dog");
	store.push_back("horse");
	store.push_back("rabbit");*/

	//difficulty 1 3char
	lvl1.push_back("!!?");
	lvl1.push_back("@?!");
	lvl1.push_back("?%#");
	lvl1.push_back("!!!"); //use this to test in the program
	lvl1.push_back("#@%");
	lvl1.push_back("$))");

	//difficulty 2 4 char
	lvl2.push_back("#@%!");
	lvl2.push_back("?@%#");
	lvl2.push_back("!!!!"); //use this to test in the program

	//difficulty 3 5char
	lvl3.push_back("#@%%%");
	lvl3.push_back("#@#?%");
	lvl3.push_back("!!!!!"); //use this to test in the program
	lvl3.push_back("^^/>&@)");
	
	//Difficulty 4 6char
	lvl4.push_back("??@@$?");
	lvl4.push_back("!!!!!!"); //use this to test in the program
	lvl4.push_back("*@*#$)");
	
	//Difficulty 5 7char
	lvl5.push_back("#@@?@?#");
	lvl5.push_back("!!!!!!!");  //use this to test in the program
	lvl5.push_back("??@^$#%");
	
	//Difficulty 6 8char
	lvl6.push_back("?!#%#@@^");
	lvl6.push_back("!!!!!!!!"); //use this to test in the program
	lvl6.push_back("#!@?%?#!");
	lvl6.push_back("#!@?##?@");
	//diffiulty 7 9char
	lvl7.push_back(")!(#^%$*@");
	lvl7.push_back("!!!!!!!!!"); //use this to test in the program
	lvl7.push_back("*#*$)!^#(");

	//difficulty 8 10char
	lvl8.push_back("&%&&@()@$%");
	lvl8.push_back("!!!!!!!!!!");//use this to test in the program
	lvl8.push_back("&%(!%^@*)@");

	

	  srand(time(0));
      random_shuffle(lvl1.begin(), lvl1.end());
      const string THE_SYMBOL = lvl1[0];            // symbols to guess
      int wrong = 0;                               // number of incorrect guesses
      string soFar(THE_SYMBOL.size(), '-');          // symbols guessed so far
      string used = "";                            // symbols already guessed

	cout << "Try to guess the symbols useing # @ % ? ! ^ $ & * ( )\n";
	while(true)
	{

		system("CLS");
		cout << "Enter the appropriate menu option" << endl;
		cout << "1. Difficulty 1" << endl;
		cout << "2. Difficulty 2" << endl;
		cout << "3. Difficulty 3" << endl;
		cout << "4. Difficulty 4" << endl;
		cout << "5. Difficulty 5" << endl;
		cout << "6. Difficulty 6" << endl;
		cout << "7. Difficulty 7" << endl;
		cout << "8. Difficulty 8" << endl;
		cout << "9. quit" << endl;
		cin >> choice;

		system("CLS");
		switch(choice)
		{

			case 1:
					cout << "Theres a total of 3 characters" << endl;
				cin >> input;
				iter = find(lvl1.begin(), lvl1.end(), input);
				if(iter == lvl1.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
			
			break;
			case 2: 
				cout << "Theres a total of 4 characters" << endl;
				cin >> input;
				iter = find(lvl2.begin(), lvl2.end(), input);
				if(iter == lvl2.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
			
			break;
			case 3: 
					cout << "Theres a total of 5 characters" << endl;
				cin >> input;
				iter = find(lvl3.begin(), lvl3.end(), input);
				if(iter == lvl3.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
			
				/*cout << "what pet would you like to donate" << endl;
				cin >> input;
				iter = find(collection.begin(), collection.end(), input);
				if(iter == collection.end())
					cout << "name not found" << endl;
				else 
				{
					collection.erase(iter);
					
					cout << "\nName donated" << endl;
				}
					*/
			break;
			case 4: 
					cout << "Theres a total of 6 characters" << endl;
				cin >> input;
				iter = find(lvl4.begin(), lvl4.end(), input);
				if(iter == lvl4.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
//				sort(collection.begin(), collection.end());
//				cout << "list sorted" << endl;

			break;
			case 5: 
					cout << "Theres a total of 7 characters" << endl;
				cin >> input;
				iter = find(lvl5.begin(), lvl5.end(), input);
				if(iter == lvl5.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
				/*for(iter = collection.begin(); iter != collection.end(); iter++)
				{
					cout << *iter << endl;
				}*/
			break;
			case 6: 
					cout << "Theres a total of 8 characters" << endl;
				cin >> input;
				iter = find(lvl6.begin(), lvl6.end(), input);
				if(iter == lvl6.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
			case 7:
					cout << "Theres a total of 9 characters" << endl;
				cin >> input;
				iter = find(lvl7.begin(), lvl7.end(), input);
				if(iter == lvl7.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
			break;

			case 8:
					cout << "Theres a total of 10 characters" << endl;
				cin >> input;
				iter = find(lvl8.begin(), lvl8.end(), input);
				if(iter == lvl8.end())
					cout << "You are incorrect" << endl;
				else 
				{
					cout << " start guessing" << endl;
					cin >> input;
					*iter = input;
					cout << "\You are correct" << endl;
				}
			break;
			case 9:
			
			
			return 0;
			default:
				cout << "incorrect input" << endl;
		}
	}
	if (wrong == MAX_WRONG)
        cout << "\nYou failed now the world is going to end!";
    else
        cout << "\nHoly Crap You got it!";
    
    cout << "\nThe symbols were " << THE_SYMBOL << endl;

		cout << "\n**Played an exciting game**";
		cout << "\nDo you want to play again (y/n): ";
		cin >> again;
	}
		cout << "\nSure theres better games outt here anyways.";
		cout << endl;
		system("PAUSE");
	}



Was This Post Helpful? 0
  • +
  • -

#3 SamAllmon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 27-August 09

Re: How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 01:42 PM

View Postblaxarbush, on 15 October 2010 - 12:36 PM, said:

I have a game that im making and i need to figure out how to create a generator that will create random strings of characters such as ! @ # $ % ^ & * ( ) and probably at random lengths. Please help. Im teaching myself C++ and i donnot know how to create functions cause i think that might have to be included.


If you're making a game, and teaching yourself C++, and don't know functions yet, you're WAY out of order.
Set your game to the side. Come back to it.
Work through things that start small and work up. Like tutorials on functions, and dynamic allocation, and random values. All very low level things that need to be addressed before you even think of the word "Game". I was in your shoes not too long ago, and I didn't want to hear this, either. But starting small and mastering the basics is incredibly important.
Was This Post Helpful? 0
  • +
  • -

#4 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 01:51 PM

View PostSamAllmon, on 15 October 2010 - 12:42 PM, said:

View Postblaxarbush, on 15 October 2010 - 12:36 PM, said:

I have a game that im making and i need to figure out how to create a generator that will create random strings of characters such as ! @ # $ % ^ & * ( ) and probably at random lengths. Please help. Im teaching myself C++ and i donnot know how to create functions cause i think that might have to be included.


If you're making a game, and teaching yourself C++, and don't know functions yet, you're WAY out of order.
Set your game to the side. Come back to it.
Work through things that start small and work up. Like tutorials on functions, and dynamic allocation, and random values. All very low level things that need to be addressed before you even think of the word "Game". I was in your shoes not too long ago, and I didn't want to hear this, either. But starting small and mastering the basics is incredibly important.



Ok i know what functions are they are small groups of code that can be called through a prototype. But as you create a function you have to create a prototype first using void and giving the function an int. I know those. But i just dont know how to set it up. I can finish the function and the prototype with no problem. Im just needing help creating the function. And the generator part im needing help on setting that up so i can creat a function/prototype with a generater and use that instead of have so many vector strings.

so in the i need some help setting up a fcunction and a generater
Was This Post Helpful? 0
  • +
  • -

#5 SamAllmon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 27-August 09

Re: How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 01:57 PM

View Postblaxarbush, on 15 October 2010 - 12:51 PM, said:

View PostSamAllmon, on 15 October 2010 - 12:42 PM, said:

View Postblaxarbush, on 15 October 2010 - 12:36 PM, said:

I have a game that im making and i need to figure out how to create a generator that will create random strings of characters such as ! @ # $ % ^ & * ( ) and probably at random lengths. Please help. Im teaching myself C++ and i donnot know how to create functions cause i think that might have to be included.


If you're making a game, and teaching yourself C++, and don't know functions yet, you're WAY out of order.
Set your game to the side. Come back to it.
Work through things that start small and work up. Like tutorials on functions, and dynamic allocation, and random values. All very low level things that need to be addressed before you even think of the word "Game". I was in your shoes not too long ago, and I didn't want to hear this, either. But starting small and mastering the basics is incredibly important.



Ok i know what functions are they are small groups of code that can be called through a prototype. But as you create a function you have to create a prototype first using void and giving the function an int. I know those. But i just dont know how to set it up. I can finish the function and the prototype with no problem. Im just needing help creating the function. And the generator part im needing help on setting that up so i can creat a function/prototype with a generater and use that instead of have so many vector strings.

so in the i need some help setting up a fcunction and a generater


http://www.cplusplus...rial/functions/
http://www.cplusplus.../tutorial/ntcs/
http://www.cpprefere...ki/c/other/rand
Was This Post Helpful? 1
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 01:59 PM

You are way over complicating things.

All you have to do is create an array of characters with what you want in them. Then generate a random length. Then generate random numbers for every character.

<edit> Added function
An example:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

string getRandom()
{
	char possible[11] = {"!@#$%^&*()"}; // allow for null terminator
	// you could write it all out by hand too possible[0] = '!';	

	// get length between 5 and 20
	// equation for getting random numbers
	// rand() % (HIGH - LOW + 1) + LOW
	int strLength = rand() % (20 - 5 + 1) + 5;

	string randomStr = "";

	for(int i = 0; i < strLength; i++)
	{
		randomStr += possible[rand() % 10]; // get character from 0-9 indices
	}

	return randomStr;
}
int main()
{
	//initialize random number seed
	srand(time(NULL));

	for(int i = 0; i < 10; i++)
		cout << getRandom() << endl;

	return 0;
}


Put the proper code into a function and you could call it over and over.

This post has been edited by eker676: 15 October 2010 - 02:03 PM

Was This Post Helpful? 1
  • +
  • -

#7 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: How do i set up a generator to create ASCii strings

Posted 15 October 2010 - 02:04 PM

Thank you both for your help. And thanks for the links ill check them out now so i can get a better understanding. thank a bunch.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1