Thread: Help with a troublesome C++ program

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    Help with a troublesome C++ program

    Its me again. I'm stuck once again with another program that I don't fully understand, this "Prof." sucks! But of course I'm not asking anyone to write a program for me. The assignment is posted below...

    Loops and Random Numbers
    Write a program to play a game of scramble. The scrambled word is displayed and the user should have 3 tries to guess the correct word. For example, the word RAMPRGIONMG and user should guess the correct word. When the user guesses 3 times and does not get the correct word, you should display the correct word. For example, PROGRAMMING. If the user guesses correctly within 3 tries, congratulate the user. At the end of the first game, ask the user if they would like to play the game again. If they do, display another word and repeat the above steps. Otherwise quit the game.

    Use at least 2 different types of loops.
    You should make available at least 3 words.
    Hint:
    1. IF the number of times is divisible by 1, then set the correct word to the first word.
    2. IF the number of times is divisible by 2, then set the correct word to the second word.
    3. IF the number of times is divisible by 3, then set the correct word to the third word.
    4. Copy the correct word to a new string object.
    5. Use a loop and random numbers to scramble the correct word in the new string object.
    6. Display the scrambled word.
    7. Give the user 3 tries to guess the correct word.
    8. If the user guesses correct within 3 tries, congratulate the user.
    9. Otherwise after 3 tries, display the correct word.
    10. Ask the user if they would like to guess another word.
    11. If they do, return to the start of the loop.
    12. Otherwise, quit the program.

    First of all, he has not mentioned or went over random numbers, nor do I understand the first 3 "hints." I keep getting errors because I have not initialized my characters, I don't want to initialize them! Is there a way around this? I have no idea what this means >#include <cstdlib>, he told us to put it in our program, so I did. Sorry my posts are sooo dang long. Anyways, here is what I have so far

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

    void main()
    {
    char exitCharacter = 'c';
    string a("money");
    string b("yeonm");
    string c("shopping");
    string d("pgiopshn");
    string e("school");
    string f("hocols");

    cout << "Welcome to Word Guess! The fun game where you try to unscramble the word." << endl;
    cout << "Good luck, and have fun." << endl;


    cout << "Here is the first word:" << endl;
    cout << "yeonm" << endl;

    if (yeonm == money)
    cout << "Great!" << endl;
    else
    cout << "Better luck next time." << endl;

    cout << "Here is the second word:" << endl;
    cout << "pgiopshn" << endl;

    if (pgiopshn == shopping)
    cout << "Great!" << endl;
    else
    cout << "Better luck next time." << endl;

    cout << "Here is the third and final word:"<< endl;
    cout << "hocols" << endl;

    if (hocols == school)
    cout << "Good job! Would you like to play again?" << endl;
    else
    cout << "Better luck next time." << endl;

    while (exitCharacter =='c' || exitCharacter == 'C' ) {
    cout << "Hit c to exit, any other key will terminate the program: " << endl;
    }
    }

    Thanks for any and all help!
    Kristina

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    two things:
    post this in the c++ forum
    //edit: just realized where i was... ignore that one
    and use code tags: type [code] int main() { a=e;...... } [/code]

    for starters, #include <cstdlib> includes a library of function calls. these calls do a bunch of stuff, like system calls, for instance.

    for random numbers:
    include math.h
    then mod rand() by a number to get a random number between 0 and that number
    ie:
    Code:
    int x = rand() % 32;
    cout << x; //x is some number between zero and thirty one
    add srand(time(NULL)); if you want to make your number truly random
    Last edited by ygfperson; 05-15-2002 at 08:39 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >then mod rand() by a number to get a random number between 0 and that number
    This isn't the best way to get a random number with rand:
    http://www.cprogramming.com/cboard/s...hlight=RANDMAX

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    10
    Ok, I'll try that and see if it helps. I am no where near finished. I still have some work to do but thank you for your help.
    Kristina

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    63
    One more thing for random numbers.

    include time.h

    and in main()
    type: srand(time(0));
    to get a new random number each time you call rand();
    else it will be the same random numbers each time you call it.

    I hope this helps.

    I will post again when I finish reading the program.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM