CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Oct 2016
    Posts
    39

    turn user input into scramble

    I need to take user input of a word and make the word scrambled for however long it is. so if it was 4 letters 4 scrambles, 5 letters 5 scrambles... etc.

    here is what i have so far

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int mix(string thing);
    
    int main()
    {
        string word;
    	int wait;
        cout << "Enter Something [< 5 Letters (faster)]\n";
        cin >> word;
        mix (word);
            cin >> wait;
    }
    
    int mix(string thing)
    {
    	int l = thing.length();
    	int hold, taken[l];
    	string scramble = "";
    
    	cout << "Please Wait ";
    
    	for (int i(1); i <= l; i++)
    	{
    		taken[i] = 0;
    	}
    	for (int q(1); q <= l; q++)
    	{
    		while (true)
    		{
    			srand((unsigned)time(NULL));
    			hold = (rand() % (l)) + 0;
    
    			if (taken[hold] == 0)
    			{
    				scramble += thing[hold];
    				taken[hold] = 1;
    				cout << ".";
    				break;
    			}
    		}
    	}
    	cout << "\n" << scramble;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    Code:
    int hold, taken[l];
    With c++, the size of an array has to be known to the compiler at compile time. So the size of an array can't be set using a variable whose value is only known at run-time.

    You can however, do this
    Code:
    vector<int> taken(l, 0);
    Which sets taken to a length of l with each element initialised to 0. See http://www.cplusplus.com/reference/v...vector/vector/

    Also you would usually only use srand() once at the beginning of a program - as it just seeds the random number generator.
    Last edited by 2kaud; October 21st, 2016 at 03:59 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    You actually don't need taken. If scramble is defined as
    Code:
    string scramble(l, 0);
    then scramble can be directly indexed.

    Code:
    for (int i(1); i <= l; i++)
    	{
    		taken[i] = 0;
    	}
    Note that this is not correct. An array index starts at 0 and its largest value is one less than the number of elements in the array. So if taken is an array with l elements, then
    Code:
    for (int i(0); i < l; i++)
    		taken[i] = 0;
    Last edited by 2kaud; October 21st, 2016 at 04:34 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Oct 2016
    Posts
    39

    Re: turn user input into scramble

    here is the function that i have for this and i still cant seem to get it to work, nor do i know what i would need to call inside my main

    Code:
    unsigned int scramble(string word)
    {
    	int l = word.length();
    	int hold, taken[l];
    	string scramble = "";
    
    	cout << "Please Wait ";
    
    	for (int i(0); i < l; i++)
    	{
    		taken[i] = 0;
    	}
    	for (int q(1); q <= l; q++)
    	{
    		while (true)
    		{
    			srand((unsigned)time(NULL));
    			hold = (rand() % (l)) + 0;
    
    			if (taken[hold] == 0)
    			{
    				scramble += scramble[hold];
    				taken[hold] = 1;
    				cout << ".";
    				break;
    			}
    		}
    	}
    	//cout << endl << scramble;
    }
    Last edited by 2kaud; October 23rd, 2016 at 10:04 AM.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    Did you read and understand any of my comments in post #2 as apart from name changes, the code in post #6 is the same as that in post #1
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Oct 2016
    Posts
    39

    Re: turn user input into scramble

    i sort of understand it, i realized i copy-pasted the wrong thing.
    Does this look right?


    Code:
    unsigned int scramble(string word)
    {
    	int l = word.length();
    	string scramble(l, 0);
    	string scramble = "";
    	unsigned int hold = 0;
    
    	cout << "Please Wait ";
    
    	for (int i(0); i < l; i++)
    	{
    		scramble [i] = 0;
    	}
    	for (int q(1); q <= l; q++)
    	{
    		while (true)
    		{
    			srand((unsigned)time(NULL));
    			hold = (rand() % (l)) + 0;
    
    			if (scramble[hold] == 0)
    			{
    				scramble += scramble[hold];
    				scramble[hold] = 1;
    				cout << ".";
    				break;
    			}
    		}
    	}
    	cout << endl << scramble;
    }

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    No. You don't need
    Code:
    string scramble = "";
    as scramble has been defined above.

    You don't need
    Code:
    for (int i(0); i < l; i++)
    	{
    		scramble [i] = 0;
    	}
    as the definition of scramble assigns 0 to each element.

    Code:
    srand((unsigned)time(NULL));
    This should be done only once usually at the start of main()

    If scramble[hold] is not 0 then the character from word can be placed direct into the element.

    Code:
    for (int q(1); q <= l; q++)
    As the first index of a string is 0, q should be set to 0 and not 1. Also the last element is one less than the number of elements the condition should be less than.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Oct 2016
    Posts
    39

    Re: turn user input into scramble

    okay awesome on the line I'm getting an error saying that it needs to be a pointer to type, does this mean I need to change it to a string?
    Code:
    int l = word.length();
    Last edited by 2kaud; October 23rd, 2016 at 11:58 AM.

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    That statement looks OK. It must be something else that is causing an error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Oct 2016
    Posts
    39

    Re: turn user input into scramble

    here's the code for the scramble, it isn't working still and I'm not sure why it isn't since you said the line was correct



    Code:
    unsigned int scramble(unsigned int palword)
    {
    	string palword;
    	int l = palword.size();
    	string scramble(l, 0);
    	unsigned int hold = 0;
    
    	cout << "Please Wait ";
    
    	for (int q(1); q <= l; q++)
    	{
    		while (true)
    		{
    			srand((unsigned)time(NULL));
    			hold = (rand() % (l)) + 0;
    
    			if (scramble[hold] == 0)
    			{
    				scramble += scramble[hold];
    				scramble[hold] = 1;
    				cout << ".";
    				break;
    			}
    		}
    	}
    	cout << endl << scramble;
    	return palword;
    }



    heres the call in the main

    Code:
    cout << "enter in a word to get scrambled" << endl;
    	cin >> palword;
    	unsigned int scramble(palword);
    	cout << "Scrambles of word is: " << palword << endl;

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    Code:
    unsigned int scramble(unsigned int palword)
    {
    	string palword;
    If you want to return the scrambled word then the function definition should be
    Code:
    string scramble(const string& palword)
    {
       int l = palword.size;
    palword is the string passed to the function to scramble so it needs to be of type string and not re-defined with the function.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    You're probably suffered enough over this. Consider
    Code:
    string scramble(const string& word)
    {
    	srand((unsigned)time(NULL));
    
    	const int l = word.length();
    	string scramble(l, 0);
    
    	cout << "Please Wait ";
    
    	for (int i = 0, hold = 0; i < l; ++i) {
    		while (scramble[(hold = (rand() % l))]);
    		scramble[hold] = word[i];
    	}
    
    	cout << endl << scramble << endl;
    	return scramble;
    }
    Code:
    cout << "enter in a word to get scrambled" << endl;
    cin >> palword;
    cout << "Scramble of " << palword << " is: " << scramble(palword) << endl;
    Try to understand the code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Oct 2016
    Posts
    39

    Re: turn user input into scramble

    i cannot thank you enough for all of the help you've given me!!

  14. #14
    Join Date
    Oct 2016
    Posts
    39

    Re: turn user input into scramble

    for the code you gave me, could i make it scramble a word more then once by adding a loop?

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: turn user input into scramble

    I would suggest you put the loop around the call. Consider

    Code:
    cout << "enter in a word to get scrambled" << endl;
    cin >> palword;
    
    for (int s = 0; s < 5; ++s)
        cout << "Scramble of " << palword << " is: " << scramble(palword) << endl;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured