c++ Random linked strings
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: c++ Random linked strings

  1. #1
    Join Date
    Feb 2013
    Posts
    5

    Unhappy c++ Random linked strings

    Hey people, i'm new to this forum. I want to make a funny and simple game in cmd with visual studio and here is my plan, here are the steps that I wanna accomplish:

    The program asks the person for
    1: 4 girl names
    2: 4 boy names
    3: What they do? (4 actions)
    4: Where they do this? (4 locations)
    5: Random linked things

    And from here I want to randomly get 1 line of a random girl name + random boy name + random action + random place.
    and followed by the second, third, and fourth line of random chosen things.

    Like this:
    Girl 1 - Boy 3 - Action 2 - Location 4;
    Girl 3 - Boy 4 - Action 4 - Location 1;
    Girl 2 - Boy 1 - Action 3 - Location 3;
    Girl 4 - Boy 2 - Action 1 - Location 2;

    And it can get funny like: Amber and John are jumping from a bridge. a game where you can play with your friends and have fun.

    Here is my code so far:

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

    int main (void)
    {

    using std::cin;
    using std::cout;
    using std::string;
    using std::srand;
    using std::rand;
    using std::time;

    srand((unsigned int)time(0));
    cout << "Welcome!\n\n";
    cout << "Type 4 girl names.\n";
    string g1, g2, g3, g4;
    cin >> g1;
    cin >> g2;
    cin >> g3;
    cin >> g4;

    cout << "Type 4 boy names.\n";
    string b1, b2, b3, b4;
    cin >> b1;
    cin >> b2;
    cin >> b3;
    cin >> b4;

    cout << "\nWhat they do?\n";
    string action1, action2, action3, action4;
    fflush(stdin);
    getline(cin, action1);
    fflush(stdin);
    getline (cin, action2);
    fflush(stdin);
    getline (cin, action3);
    fflush(stdin);
    getline (cin, action4);

    cout << "\nWhere is happening?\n";
    string loc1, loc2, loc3, loc4;
    fflush(stdin);
    getline(cin, loc1);
    fflush(stdin);
    getline (cin, loc2);
    fflush(stdin);
    getline (cin, loc3);
    fflush(stdin);
    getline (cin, loc4);

    int randomNr1 = rand() % 120 +1;

    }

    And from here I don't know what to do!!
    please help me
    Also I'm a beginner at c++ so my code could be poor made.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: c++ Random linked strings

    And from here I don't know what to do!!
    Format your code and use code tags when posting code! Click Go Advanced, select code then click '#'

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: c++ Random linked strings

    You should store your data in arrays that can be indexed via a random number.

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int main (void)
    {
    string	g[4],
    	b[4],
    	a[4],
    	l[4];
    
    	srand((unsigned int)time(0));
    
    	cout << "Welcome!\n\n";
    	cout << "Type 4 girl names.\n";
    	for (int gi = 0; gi < 4; gi++) 
    		cin >> g[gi];
    
    	cout << "Type 4 boy names.\n";
    	for (int bi = 0; bi < 4; bi++)
    		cin >> b[bi];
    
    	cout << "\nWhat they do (enter 4 actions)?\n";
    	for (int ai = 0; ai < 4; ai++)
    		getline(cin, a[ai]);
    
    	cout << "\nWhere is happening (enter 4 locations)?\n";
    	for (int li = 0; li < 4; li++)
    		getline(cin, l[li]);
    
    	for (int c = 0; c < 4; c++)
    		cout << g[rand() % 4] << " and " << b[rand() % 4] << " are " << a[rand() % 4] << " from a " << l[rand() % 4] << endl;
    
    	return (0);
    }

  4. #4
    Join Date
    Feb 2013
    Posts
    5

    Re: c++ Random linked strings

    BIG THANK YOU! you saved my game ) I spent hours trying to combine different things and you revealed the right way to do this!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center