Stop Repeating Random Numbers/Strings

As of late I have decided to write a program that stores the name of tunes I have memorized for The Great Highland Bagpipe. I know there is a while loop set up that will remember which number has been displayed. This way it will not display it twice. You can see how this would get irritating if I am trying to practice all of these, and have to repeat one I have already done. The problem is I do not know how to set this all up using strings. How could I do that?
/*
Written By: Smjprogrammer 6/23-27/2015
Revision on 7/20/2015

Note: No music is actually played.
No music is owned by me.
*/

#include <iostream>
#include <ctime>
#include <string>
#include <Windows.h>
using std::cout;
using std::cin;
using std::string;

const string Song_List[25] =
{
	"A Man's A Man for A' That - 2/4 March 90bpm",
	"Amazing Grace - 3/4 Slow Air 60bpm",
	"Captain Horne - C Strathspey 90bpm",
	"Hag at the Churn - 6/8 Jig 80bpm",
	"Highland Laddie - 2/4 March 90bpm",
	"Inverness Rant - C Strathspey 90bpm",
	"MacCrimmon's Lament - 3/4 Lament 80bpm",
	"Mist Covered Mountains - 6/8 Slow March 46bpm",
	"Scotland The Brave - 4/4 March 90bpm",
	"Scot's Wha Hae - C Slow March 40bpm",
	"Skye Boat Song - 6/8 Slow Air 46bpm",
	"The 79'th Farwell To Gibralter - 2/4 March 90bpm",
	"The Battle of The Somme - 9/8 Retreat 90bpm",
	"The Brown Haired Maidan - 2/4 March 90bpm",
	"The Carles Wi' The Breeks - 6/8 Slow Air 50bpm",
	"The Dark Island - 6/8 Slow March 40bpm",
	"The Earl of Mansfield - 2/4 March 90bpm",
	"The Flowers of The Forest - 6/8 Lament 50bpm",
	"The Green Hills of Tyrol - 3/4 Retreat 90bpm",
	"The High Road To Gairloch - 2/4 March 90bpm",
	"The Old Hag at the Kiln - 6/8 Jig 120bpm",
	"The Piper of Drummond - \233 Reel 90bpm",// I use the cent character for the cut time. 
	"The Rowan Tree - 4/4 March 90bpm",
	"The Thistle of Scotland - C March 90bpm",
	"Woodside - C Reel 90bpm"
};

class Bagpipe_Tunes
{
public:
	Bagpipe_Tunes()// Constructor.
	{
		srand((unsigned int)time(nullptr));

		SetConsoleTitle(TEXT("Can You Play This Random Bagpipe Tune"));
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0A);
	}

	void full_list()// Full Printable List.
	{
		for (int a = 0; a <= 25; a++)
		{
			cout << Song_List[a] << '\n';
		}
	}

	void tune()// Random Tune Pick.
	{
		string a; 

		for (int b = 25; b > 0; b--)
		{
			a = Song_List[rand() % 25];

			cout << a << '\n';
			cin.get();
		}
	}
};

int main()
{
	Bagpipe_Tunes display;

	//display.full_list();

	display.tune();
}// main.cpp

Comments

  • It is usually easier to randomly swap values in your array once, when you start your program, and then play the array in sequence.
  • I have no clue how to do that, and is it going to be random each time I run the program if done that way?
  • Do this in the constructor.
                    for (int i = 0; i < 20; i++) // 20 here is arbitrary because it determines the number of swaps to make.
    		{
                            int idx1 = rand() % 25;
                            int idx2 = rand() % 25;
    
                            string a = Song_List[idx1];
                            Song_List[idx1] = Song_List[idx2];
                            Song_List[idx2] = a;
     		}
    

    Then in tune remove the rand() and just use:
                    a = Song_List[b];
    
  • Tried that this is all that happened.

    Build started: Project: my_main, Configuration: Debug Win32
    my_main.cpp
    c:\users\smjprogrammer\documents\visual studio 2013\projects\my_main\my_main\my_main.cpp(59): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(1017): could be 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(_Elem)'
    with
    [
    _Elem=char
    ]
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(1012): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(const _Elem *)'
    with
    [
    _Elem=char
    ]
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(996): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)'
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(957): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::initializer_list<_Elem>)'
    with
    [
    _Elem=char
    ]
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(901): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&) throw()'
    while trying to match the argument list '(const std::string, const std::string)'
    c:\users\smjprogrammer\documents\visual studio 2013\projects\my_main\my_main\my_main.cpp(60): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(1017): could be 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(_Elem)'
    with
    [
    _Elem=char
    ]
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(1012): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(const _Elem *)'
    with
    [
    _Elem=char
    ]
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(996): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)'
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(957): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::initializer_list<_Elem>)'
    with
    [
    _Elem=char
    ]
    c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(901): or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&) throw()'
    while trying to match the argument list '(const std::string, std::string)'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • Oh. Didn't see you made SongList a const. You'll need to remove that.
  • Ok it works now, but if I add more tunes do I need to change the for loop with the comment at the end that says this? Yes I know it says arbitrary just checking. Also why does it need me to press enter in order to get the first tune printed?

    // 20 here is arbitrary because it determines the number of swaps to make.
  • Another thing I noticed is that the full_list() is random. It needs to be in order. How is it going out of order?
  • Because the loop randomizes it.
  • Sounds like it would be easier to get rid of the full_list(), and forget it existed.
  • Would it be wrong to create a new class that contains the full_list() even thou it works just fine?
  • Of course not. Why would it be wrong?
  • Who knows back in the old days when I started around here someone would have told me how dumb it was. Then 400 pages later I would get a real answer. :)
Sign In or Register to comment.