1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c++ random code and small bit of extra help needed

Discussion in 'C++' started by zendet, Mar 27, 2010.

  1. zendet

    zendet New Member

    Joined:
    Mar 27, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys:happy:

    Im doing a project thats due pretty soon, and im on easter break so i dont have much access to help.


    This is my program here, please remember we are at a pretty simple stage so if you could use simple code please do.

    This is what i have so far, i have // at the front of the part i need help with, thanks!

    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    using namespace std;
    int main()
    {
     
     
     
     
     
    int numofchar;
        
    string words [12] = ("blue","red","green","yellow","white","black","pink","orange","purple","brown");
     
    
    //get a random word fromthe array each time the program starts
    //find the random word picked from array then put that into its own variable
    //then find numofchar of that, then find wordlength of numofchar
    numofchar = wordlength.length();
     
    cout<<wordlenght.length();
     
     
    cout<<"\n\n";   
    system ("pause");    
    return 0;    
    }
     
     
    
     
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    see the code below

    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    using namespace std;
    int main()
    {
     
    int numofchar;
     [COLOR=Red]srand(time(NULL));[/COLOR]
     [COLOR=Red]int which=rand()%10;   [/COLOR]
    string words [[COLOR=Red]10[/COLOR]] = [COLOR=Red]{[/COLOR]"blue","red","green","yellow","white","black","pink","orange","purple","brown"[COLOR=Red]}[/COLOR];
     string wordpicked=[COLOR=Red]words[which];[/COLOR]
     cout<<"word picked="<<wordpicked<<endl;
     numofchar=wordpicked.length();
     cout<<"selected index="<<which<<endl;
     cout<<"length="<<numofchar;
    cout<<"\n\n";   
    system ("pause");    
    return 0;    
    }
    
     
    shabbir likes this.
  3. zendet

    zendet New Member

    Joined:
    Mar 27, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply and help, im guessing it gets a new random word every minute.

    My question is what is the selected index, its different for every colour, so im guessing they all have there own one? or is that just the name of the variable that the random word is placed into?
     
  4. zendet

    zendet New Member

    Joined:
    Mar 27, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0

    edit

    Its ok i worked it out.
    My next question is this:

    example

    yellow would be:

    ******

    red would be

    ***

    basically i must have the random word blanked out and have the user guess the letters one by one. If a letter is guessed thats in the word then that letter is reveled example:

    red
    *e*

    I was thinking i could do it by having lots of if statments, saying if word.length() = 3
    {cout<< "***";}

    and so on for all the possibilites of the random words.

    but im guessing there is a faster way, like:


    "*" multiplied by word.length() so if word.length() was 5 then it would output *****
    obviously this code isnt the real code but its just how i would visualise it.
     
  5. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    one way to do so is this

    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    using namespace std;
    int main()
    {
     
    int numofchar;
     srand(time(NULL));
     int which=rand()%10;   
    string words [10] = {"blue","red","green","yellow","white","black","pink","orange","purple","brown"};
     string wordpicked=words[which];
     numofchar=wordpicked.length();
     
     [COLOR=Red]char buffer[20];
     memset(buffer,'*',numofchar);
     buffer[numofchar]='\0';
      string wordBlanc(buffer);[/COLOR]
     cout<<"word picked="<<wordpicked<<endl;
    
     cout<<"selected index="<<which<<endl;
     cout<<"length="<<numofchar<<endl;
     [COLOR=Red]cout<<"word blanced is:"<<wordBlanc;[/COLOR]
     cout<<"\n\n";   
    system ("pause");    
    return 0;    
    }
    
    
     
  6. zendet

    zendet New Member

    Joined:
    Mar 27, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    ok thanks. the final part is basically, it wil be

    cout<<wordBlanc

    which would be

    ******

    than the user would guess

    cin>> guess

    then if the letter guessed is in the word it will be revealed

    example:

    if word was yellow
    ******

    cin>> e

    *e****

    and when all letters are guessed correctly then the game says you win

    cout<<"you win";
    then its over =)


    a string can be treated like an array of chars.
    each char in the string can be accessed through the [] operation

    example:
    char firstChar; // first character of the name

    firstChar = firstName[0];



    the code i have for finding chars in strings is:

    find and replace characters in a string

    Code:
    
    str("ftp.hellocollege.com");
    
    cout<<"Before: "<<str<<endl;
    int size = str.size();
    
    for (int i=0; i< size; ++i)
    {if (str[i]==".")
    str[i] = "_";
    
    }
    cout<<"After: " <<str << endl;
    
    replace(str.begin(), st.end(), "_", "_");
    
    
    
    
    
    cout<<" After replacing for a second time: << str << endl;
    system("pause");
    return 0;
    }
    
    
     

Share This Page