Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   c++ random code and small bit of extra help needed (http://www.go4expert.com/forums/cpp-random-code-small-bit-extra-help-t21543/)

zendet 27Mar2010 16:15

c++ random code and small bit of extra help needed
 
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;   
}


virxen 28Mar2010 03:57

Re: c++ random code and small bit of extra help needed
 
see the code below

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];
 cout<<"word picked="<<wordpicked<<endl;
 numofchar=wordpicked.length();
 cout<<"selected index="<<which<<endl;
 cout<<"length="<<numofchar;
cout<<"\n\n"; 
system ("pause");   
return 0;   
}


zendet 28Mar2010 18:46

Re: c++ random code and small bit of extra help needed
 
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?

zendet 28Mar2010 19:18

Re: c++ random code and small bit of extra help needed
 
Quote:

Originally Posted by zendet (Post 66352)
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?


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.

virxen 28Mar2010 20:46

Re: c++ random code and small bit of extra help needed
 
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();
 
 char buffer[20];
 memset(buffer,'*',numofchar);
 buffer[numofchar]='\0';
  string wordBlanc(buffer);

 cout<<"word picked="<<wordpicked<<endl;

 cout<<"selected index="<<which<<endl;
 cout<<"length="<<numofchar<<endl;
 cout<<"word blanced is:"<<wordBlanc;
 cout<<"\n\n"; 
system ("pause");   
return 0;   
}


zendet 28Mar2010 21:06

Re: c++ random code and small bit of extra help needed
 
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;
}



All times are GMT +5.5. The time now is 11:20.