Author Topic: problem with a project in c++  (Read 850 times)

0 Members and 1 Guest are viewing this topic.

Offline eliaou

  • Serf
  • *
  • Posts: 28
  • Cookies: 0
    • View Profile
problem with a project in c++
« on: August 13, 2015, 04:42:02 pm »
Hello guys, :)

I have been learning c++ alone for a week now (not very impressive i know) .

I have been trying to create a function that takes a string as input and shuffles the order of the string without repeating any of the characters.

It is part of a bigger program that is basically a guessing game between two players :

player 1 inputs a word >> computer shuffles the word >> computer prints shuffled word >> player 2 needs to guess what was the original word and input it .



i cant figure out what the problem is

Moreover I would like to know if you could possibly give me some feedback about my style (what can I improve)

(By the way its not a function yet because i needed to test it)

Thanks

this is what i came up with :

Code: [Select]

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

/*
this function takes a string as argument and shuffles it */



using namespace std;

int main()
{
    string inputWord("MYSTERE");  // I wanted to test it with 1 word before generalizing it
    string shuffledWord;




for (int i=0 ; i <= inputWord.size() ; i++)

{

 int position(0) ;


        srand(time(0));

        position = rand() % inputWord.size();

        shuffledWord += inputWord[position];


inputWord.erase(5, 1);  // Retire la lettre n°5





}


  cout << "shuffled word :" << shuffledWord<<endl;


    return 0;
}




Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: problem with a project in c++
« Reply #1 on: August 13, 2015, 04:50:49 pm »
First of all, I'd advise you to describe what your problem is *exactly*. Otherwise no one is willing to help you.
But because I have a good day today, I'll just tell you this: you delete always the letter number five, but you
need to delete letters at the position you calculated.

IMPORTANT EDIT: You calculate the string size when checking the loop condition. And since you delete chars from
the string, it gets shorter and shorter, so the loop runs only 4 times.

Fix this by doing it like this:
Code: (C++) [Select]
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

int main(){
       string inputWord("MYSTERE");
        string shuffledWord;
    int size(inputWord.size());
    for (int i=0 ; i < size; i++){
        int position(0) ;
        srand(time(0));
        position = rand() % inputWord.size();
        shuffledWord += inputWord[position];
        inputWord.erase(position, 1);
    }
      cout << "shuffled word : " << shuffledWord<<endl;
        return 0;
}


I changed the <= in the loop condition to a <, otherwise your loop will run 1 times too often, since indexing starts at 0.
Try to see all changes and understand why they were necessary.
« Last Edit: August 13, 2015, 05:14:44 pm by TheWormKill »
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline eliaou

  • Serf
  • *
  • Posts: 28
  • Cookies: 0
    • View Profile
Re: problem with a project in c++
« Reply #2 on: August 13, 2015, 04:54:55 pm »
Thank you for the advice :) .

So, the problem is that i get a bug even if i delete the letter at position .

the bug is that the console prints out only 4 letters instead of 7.

*I did as you advised (advice 1) now it prints out different string size each time.

Code: [Select]

using namespace std;

int main()
{
    string inputWord("MYSTERE");  // I wanted to test it with 1 word before generalizing it
    string shuffledWord;
    const int wordSize(inputWord.size());




for (int i=0 ; i <= wordSize ; i++)

{

 int position(0) ;


        srand(time(0));

        position = rand() % wordSize;

        shuffledWord += inputWord[position];


inputWord.erase(position, 1);





}


  cout << "shuffled word :" << shuffledWord<<endl;


    return 0;
}
« Last Edit: August 13, 2015, 05:05:31 pm by eliaou »

Offline eliaou

  • Serf
  • *
  • Posts: 28
  • Cookies: 0
    • View Profile
Re: problem with a project in c++
« Reply #3 on: August 13, 2015, 05:33:46 pm »
Wow..what a subtlety...nice !

« Last Edit: August 13, 2015, 06:26:51 pm by eliaou »