0

i cant seem to figure out what wrong

for some reason it wont compile and it think theres a problem on my jumbleString function

#include <iostream>
#include <iomanip>
#include <string>
#include <istream>

using namespace std;

int main ()
{
    int lengthofstring, x, countWords(string str), countConsonant(string str, int), consonant, jumbleString(string str);
    string str, str2, wordone;

    char options;
    cout << "Please enter a word, a sentence, or a string of numbers." << endl;

    getline(cin, str);

    //cin >> str;

    lengthofstring = str.length(); 
    str2=str;

    bool another= true;

    while (another) 
    {
        cout << '\n' << "USE THIS MENU TO MANIPULATE YOUR STRING" << endl;
        cout << "---------------------------------------" << endl;
        cout << "1) Inverse String" << endl;
        cout << "2) Reverse String" << endl;
        cout << "3) To Uppercase" << endl;
        cout << "4) Jumble String" << endl;
        cout << "5) Count Number Words" << endl;
        cout << "6) Count Consonants" << endl;
        cout << "7) Enter a Different String" << endl;
        cout << "8) Print the String" << endl;
        cout << "Q) Quit" << endl;

        cin >> options;

        switch (options)
        {
        case '1':
            for (x = 0; x < lengthofstring; x++)
            {
                if (islower(str[x]))
                    str[x] = toupper(str[x]);
                else if (isupper(str[x]))
                    str[x] = tolower(str[x]);

            }
            cout<< str;
            break;
        case '2':
            for (x = 0; x < lengthofstring; x++)
            {
                str2[x] = str[lengthofstring-1-x];
            }
            cout<< str2;
            break;
        case '3':
            {
                for (x = 0; x < lengthofstring; x++)
                { 
                    if (islower(str[x]))
                        str[x] = toupper(str[x]);
                }
                cout<< str;
            }
            break;
        case '4':
            jumbleString(str);
            break;

        case '5':
            cout << countWords(str);
            break;
        case '6': 
            consonant = 0;
            cout<< countConsonant(str, consonant);
            break;
        case '7':
            cout << "Please enter another word, a sentence, or a string of numbers." << endl;
            cin.ignore();
            getline(cin, str);
            cout << str <<endl;
            break;
        case '8':
            cout<< str2;
            break;
        case 'q':
            another = false;
            break;
        }
    }

    cin.get();
    cin.get();
    return 0;
}

void jumbleString(string str)
{
    int length = str.length();
    int  j, k;

    for(int i = 0; i < length; j++)
    {
        k = rand() % length;
        j = rand() % length;
        char c = str[j];
        str[j] = str[k];
        str[k] = c;
    }

    cout << str<<endl;
}

int countWords(string str)
{
    int length = str.length();
    int words = 1;
    for(int size = 1; length > size; size++)
    {
        if (str[size] == ' ' && str[size-1] != ' ')
            words++;
    }
    if (str[0] == ' ')
        words--;
    return words;
}
int countConsonant(string str, int consonant)
{
    int length = str.length();
    consonant = 0;

    for (int i = 0; i < length; i++)
    {
        if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && 
            str[i] != 'o'&& str[i] != 'u' && str[i] != 'A' && str[i] != 'E' 
            && str[i] != 'I' && str[i] != 'O' && str[i] != 'U' && str[i] != ' '&& str[i] != '1'
            && str[i] != '2' && str[i] != '3' && str[i] != '4' && str[i] != '5' && str[i] != '6'
            && str[i] != '7' && str[i] != '8' && str[i] != '9' && str[i] != '0')
            consonant = consonant + 1;
    }
    return consonant;
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
johny tran
  • 31
  • 1
  • 6
  • `i = rand() % length;`. Maybe you mean `k = rand() % length;`? – beduin May 08 '11 at 06:29
  • 1
    paul, please say what the actual problem is? Does it give a compiler error? Or does it not do what you expect? On which input? What do you expect? – Howard May 08 '11 at 06:52
  • Which one? I assume you're missing the `#include `. – Howard May 08 '11 at 07:18
  • paul, we cannot help you if you just say "there is still a problem". At least five people suggested what is wrong with your code. We still are only guessing what you mean. If you can't provide more information there can be no help from us. – Howard May 08 '11 at 07:29

4 Answers4

1

the problem is changing i inside the loop (I guess you meant to change k):
if you did mean to set k, change i = rand() % length; into k = rand() % length;
also, your question is a variant of the permutation problem, which Fisher-Yates solves. I would suggest looking at it, you will probably get better "randomness" by using it.

amit
  • 175,853
  • 27
  • 231
  • 333
1

You are mistakenly using the loop variable, i , twice here. Also you might want to seed the random number generator if you want truly random jumbling of the strings.

For an idiomatic way of doing this in c++ you can use the standard algorithms to do this as follows:

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

int main(void){
        srand ( unsigned ( time (NULL) ) );//seed the random shuffle

        std::string test = "abcdef";
        std::cout << "original string: " << test << std::endl;
        std::random_shuffle(test.begin(),test.end());
        std::cout << "shuffled string: " << test << std::endl;
        return 0;
}
shuttle87
  • 15,466
  • 11
  • 77
  • 106
0

You are using i as loop variable but at the same time assign a random value to it within the loop.

A possible solution would be not to use two randoms at all but instead the iterating variable i itself [online example].

for(int i = 0; i < length; i++)
{
    j = i + (rand() % (length-i));
    char c = str[j];
    str[j] = str[i];
    str[i] = c;
}
Howard
  • 38,639
  • 9
  • 64
  • 83
  • this solution is also biased have a look: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Implementation_errors – amit May 08 '11 at 06:57
  • p.s. if you are intrested about how biased is the naive method comparing to Fisher-Yates (your fixed algorithm) have a look: http://stackoverflow.com/questions/5131341/what-distribution-do-you-get-from-this-broken-random-shuffle I found it very intresting. – amit May 08 '11 at 07:09
0

You are using i, j for your two random indices whereas these should be j, k.

It should be:

j = rand() % length;
k = rand() % length;
Paul R
  • 208,748
  • 37
  • 389
  • 560