Jumble Word(Leaving end letters)
Hi I am trying to get the words given by the user to jumble and it is not working for me. What am I missing?
Here's my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
int main()
{
srand(time(0));
std::string userInput;
std::string word = userInput;
std::cout << "Enter a word to jumble \n";
std::cin >> userInput;
if(word.size() > 3)
{
std::random_shuffle(word.begin() + 1, word.end() - 1);
std::cout << word << '\n';
}
system("pause");
return 0;
|
Thank you
You may want to print out the values of both word and userInput after you retrieve userInput from your user.
I would also make the definition of std::string word = userInput come after cin >> userInput
@ rcast, that worked, thanks.
I'm having trouble not having it use the same number more than once;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <ctime>
using namespace std;
#define ENTER 13
int main(int nNumberofArgs,char* pszArgs[])
{
int c;
int I = 0;
int ran;
char ch;
char word[1024];
char jumble[1024];
cout << "Enter a word to jumble(Press 'enter' to quit): ";
for(int i = 0;i < 1024;i++)
{
ch = getch();
c = ch;
if(c == ENTER)
{
break;
}
word[i] = ch;
cout << word[i];
I++;
}
cout << endl;
srand((unsigned)time(0));
for(int i = 0;i < I;i++)
{
ran = rand() % (I);
jumble[i] = word[ran];
cout << word[i] << " " << jumble[i] << endl;
}
cout << "Jumbled word: ";
for(int i = 0;i < I;i++)
{
cout << jumble[i];
}
cout << endl;
system("PAUSE");
return 0;
}
|
Topic archived. No new replies allowed.