I once again am having trouble with this program. I really thought i understood this right off the bat but once i got into it, there is a lot of little technical things my professor wants in it. Like the functions and generating random numbers. Any help would be awesom, i just got done with my other assignment now i have this one due friday....i have an attached file of what the output should look like...

//Assignment 5: Guessing game with random numbers and functions
//By Curtis Davenport 2/17/08

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <math>

using namespace std;

int GuessFunction (int)

int main()
{

    int guess;
    int target;
    int chancesLeft;
    bool foundTarget = false;
    bool endGame = false;

    chancesLeft=5  //I need to give a random target for the user to hit and do
                           //a guess function for it.
    target =????  //I dont know how to set up for a user to input a number so he/she
                       //can hit a random target.

    do    //Overall I need to give the user 5 guesses and give options after.
    {
        cout << "Guess a number (0-100): ";
        cin  >> guess;

        if (guess < target)
        {
            cout << "Your Guess is too LOW!!! \n";
        }
        if (guess > target)
        {
            cout << "Your Guess is too HIGH!!! \n";
        }

		if (

        chancesLeft--;

        if (guess == target)
        {
            foundTarget = true;
            endGame = true;
        }
        if (chancesLeft == 0)
        {
            endGame = true;
        }

    } while (!endGame);
		
		if (foundTarget)
		{
        cout << "You got it right!!! \n";
		}
		else
		{	
        cout << "You ran out of chances. \n";

	return 0;
}
int GuessFunction(int)
{  // I am supposed to use a function here, do not know how to set it up.
    int result;

    srand( time(0) );

    result = 
    
    return result;
}
//I am supposed to 
//	(1)Write a program that generates a random integer number
//  between 1 and 100 and allows the user to make a maximum of 
//  five 5 attempts to guess the number.
//	(2)The program must let the user know whether the current guess is too high or too low.
//	(3)The program must notify the user if he/she made the right guess or ran out of chances.
//	(4)At the end of the cycle the program must ask the user for a choice of 
//  trying again 1 or exiting 2. This answer must be validated (only 1 or 2 are acceptable).
//	(5)Seed the random number generation sequence at the beginning of the program using
//  the current time in seconds.
//	(6)The random target value must be generated inside the main function and passed  
//  as an input parameter to a Guess function.
//	(7)The Guess function must ask the user to enter the guess value, compare it 
//  with the target, notify the user if the guess is too high or too low, accumulate
//  the number of attempts, and check if the maximum number of attempts has been reached.
//	(8)The Guess function must return the number of attempts by the user to the main 
//  function and the main function must use this value to notify the user whether he/she 
//  ran out of chances or made the right guess.

That should solve your problem with the rand function.

int GuessFunction(int)
{  
    int result;
    srand( time(NULL) );
    result = rand()% 99 + 1   // range for rand() = (max - min) + min
    return result;
}
//I am supposed to 
//	(1)Write a program that generates a random integer number
//  between 1 and 100

After a more thorough reading I corrected your code. The comments are in the file. Feel free to post any issues you might have.

Hey knight fyre, can you post the code you have improved upon in daniweb rather than an attachmet. I am having trouble opening up your document. Anyways thanks so much for your input on this assignment. You have helped me out a bunch, if you could post your improvements to my code in daniweb id like to see what changes you have made and see if i can actually learn this stuff!!! thanks again

I'm not verse with posting actual code on daniweb but here goes:

//Assignment 5: Guessing game with random numbers and functions

//By Curtis Davenport 2/17/08


#include <iostream>
#include <ctime>
#include <cstdlib>
#include <math.h>

using namespace std;

int GuessFunction (int);   

int main()
{
    int target, attempts, choice; // can be declared in the same line if the data types are the same

    do // needed for requirement (4)
    {
        srand( time(NULL));                           //   maximum    + minumum
        target = rand()% 99 + 1;    // range for rand() = (max - min) + min
                                    // for range between 100 to 1 (100 - 1) + 1 = rand() % 99 + 1
                                    // the result of rand() is stored in target

        attempts = GuessFunction(target);   // GuessFunction accepts the int variable target and returns attempts to main

        // Tells the user whether they were sucessful or not after five or less tries
        if (attempts <= 5)
        {
            cout << "You got it right in " << attempts << " attempts" << "!!! \n";
        }

        if (attempts > 5)
        {
            cout << "You ran out of chances. \n";
        }

        cout << "\n\nDo you want to play again? 1 for Yes - 2 for No: ";
        cin >> choice;
        system("PAUSE"); // pauses screen and prints message. I do not recommend this method in favour of cin.get();
} while(choice == 1); // end of dowhile for (4)

return 0;
}

/*The Guess function must ask the user to enter the guess value, compare it
with the target, notify the user if the guess is too high or too low, accumulate
the number of attempts, and check if the maximum number of attempts has been reached.

NOT THE MAIN!!!*/

// GuessFunction implementation
int GuessFunction(int target)
{
bool foundTarget = false;
bool endGame = false;
int guess, chancesLeft=5; 

do 
{
    cout << "Guess a number (0-100): ";
    cin >> guess;

    if (guess < target)
    {
        cout << "Your Guess is too LOW!!! \n";
        chancesLeft = chancesLeft - 1; // decrement chances left by one to represent attempt
    }

    if (guess > target)
    {
        cout << "Your Guess is too HIGH!!! \n";
        chancesLeft = chancesLeft - 1; // decrement chances left by one to represent attempt
    }

    if (guess == target)
    {
        chancesLeft = chancesLeft - 1;  // decrement chances left by one to represent attempt
        foundTarget = true;
        endGame = true;
    }

    if (chancesLeft == 0)
    {
        endGame = true;
    }

} while (!endGame);

if (foundTarget)
{
    return (5 - chancesLeft); // returns a value that represents the number of attempts to main
}                                      // 5 represents the total allowed chances    
else                                   // 5 less the # of chances left equals the # of chances used
{
    return 6;     // any value greater that 5 would suffice. use to activate the second if function in main
}

} // end of function

It looks better when you click "Toggle Plain Text" and cut and paste it to a c++ compiler.

I made some of the changes you made and it worked great. I see how you used the function and how it was implemented. It just takes such a visionary to come up with even a simple program like this. I just can not see in my head how it should look in the compiler, it makes sense once i read and see how the order of the code is. But on my own i have a difficult time coming up with this off the top of my head. But thanks so much for your help, this class is so hard for me and i am putting in many hours to try to learn it. Thanks again for your help i really appreciate it!!!

Programming takes reading and plenty of practice!

Before you even open the compiler spend 2 minutes reading through the question and 3 minutes analyzing it. While analyzing ask yourself:

What information is my program going to accept from the user and what is their data type? It could be a persons name: type string/char[x], their age: type int, or their salary: type float, and so on.

After that determine the functions you are going to need to create based on the requirements of the question then ask yourself:

What is my function going to accept to do the job? It could be s person's name which is of type char or string, a user's salary of type int or float, and a user's income tax: type float or int in order to calculate their net salary.

Then ask:

Is it going to return anything? If nothing is a procedure and it might just print something to the screen, something like

salary = salary - tax;
printf("salary is %d", salary);

or it could return a value to main or even another function!

salary = salary - tax;
return salary;

Then ask:

Do you need a loop (while or for) to accomplish xyz task or a decision (switch/case, if, if else) in the function to accomplish xyz task or both.

As a beginner it's best break down each task into a function.

I made some of the changes you made and it worked great. I see how you used the function and how it was implemented. It just takes such a visionary to come up with even a simple program like this. I just can not see in my head how it should look in the compiler, it makes sense once i read and see how the order of the code is. But on my own i have a difficult time coming up with this off the top of my head. But thanks so much for your help, this class is so hard for me and i am putting in many hours to try to learn it. Thanks again for your help i really appreciate it!!!

commented: Good advice +1

Take a close look at line 22. Modulo returns a number from 0...N-1. So modulo 99 + 1 returns a value from 1 to 99 not 100.

Also, look at lines 68, 74, 79. Shouldn't this be one line outside the if()s?

A closer look at my rand() and some debugging revealed it to be flawed. This is a working formula for rand:

rand () % (high - low + 1) + low;

Lines 68, 74, and 79 does the job but 1 (ONLY ONE)

chancesLeft = chancesLeft - 1;

is needed if placed outside the if statements and must be placed in the do...while loop.

Take a close look at line 22. Modulo returns a number from 0...N-1. So modulo 99 + 1 returns a value from 1 to 99 not 100.

Also, look at lines 68, 74, 79. Shouldn't this be one line outside the if()s?

Thanks so much for your help. I think i am starting to sorta get a grip on this stuff. We are now talking about arrays and these seem a bit overwhelming. If i have any more problems ill post something. But thanks again for all of the help and direction in programming.

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.