#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{

char ch, play_or_no_play='y';
while (play_or_no_play == 'y')

  {
    cout << "Enter a character: ";
    cin >> ch;

  while (ch != 'c')
  {
	cout << "Wrong Character, try again: ";
	cin >> ch;
  }

    cout << "\a\a\a\a bingo you got it!" << endl;
    cout << "Do you want to play again? ";
    cin >> play_or_no_play;

  }



    system("Pause");
    return 0;
}

I need to make it where the character 'c' now becomes a random character a-z

i was looking at this thread http://www.daniweb.com/forums/thread1769.html

trying to change the code to use char instead of int which I have done, but though how to limit it to a-z is little beyond my skill:
yes it is for school, but had a house fire and doing independent study aka do the work and turn it in to get a grade though semester ended or take F I am just not sure if they were at the point of using arrays or if they is some cut and dry one line change that does the trick.

"original question was: In the following program find how to change the character 'c' dynamically to keep the game running"

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    char random_char = rand();
    cout << random_char << endl;

    system("pause");
    return 0;
}

Recommended Answers

All 4 Replies

You should leave random_char as int because rand() will return values that overflow (dont fit) char.

Here is a hint how to generate a random number between 'a' and 'z'. Note that any standrd ascii chart will tell you the decimal values of characters.

Ok i understand that if i use the int rand-char() and then add either 65 to get a random number for uppercase letter or i think 85 for a random lower case. I am suppose to be working with the first bit of code the second was an exampe i was trying to tweat then fit back into my original loop. though even useing

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int random_char = rand();
    random_char+=85;
    cout << random_char << endl;

    system("pause");
    return 0;
}

I still keep commming out with the same letter each time. though point being i dont know what he meant by dynamically i pretty sure it means random and since no longer in class I cant go ask. I've seen post about impelenting srand(time) but I dont remember any type of srand() command so Im not sure if i can complete this without more detail from an instructor i've been trying to use most basic functions and commands as per the week givin. ie using if ... else 20 times instead of switch because we had not discussed switch case. I think i've just backed myself into a corner trying to work with too little information

You need to seed :

#include <iostream>
#include <ctime>

char getRandomLowerCasedLetter(){
 return 'a' + (rand() % ('z'-'a'));
}

int main(){
 //seed the random number generator
 srand( time(0) );
 bool keepPlaying = true;
 char winningLetter= getRandomLowerCasedLetter();
 while(keepPlaying){
     cout << "\nGuess a letter : ";
     char guessLetter = 0;
     cin >> guessLetter;
     if(guessLetter == winningLetter){
        cout << "You Win\n";
        cout << "Play again <'y' = yes, 'n' = no> : ";
        char play;
        cin >> play;
        if(play == 'y') keepPlaying = true;
        else keepPlaying = false;
        winningLetter = getRandomLowerCasedLetter();
     }
 }
}

thanks for the help both of you have shown me the inner workings of the random feature. I think i was suppose to maybe make a page long program witch put a-z into an array and as each cycle counted use a loop to use the next letter in a built array to be the next "secret" letter. I was not suppose to be efficent as srand() but maybe write 2 pages of code where a simple 20 to 30 lines would have simplified. It was more a do the long way and then i introduce you to another method so that all that pain and suffering will never let you forget "the new way"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.